Class: Oj::Rails::Encoder

Inherits:
Object
  • Object
show all
Defined in:
ext/oj/rails.c,
ext/oj/rails.c

Overview

The Oj ActiveSupport compliant encoder.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(options = nil) ⇒ Object

Creates a new Encoder.

  • options [Hash] formatting options


670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
# File 'ext/oj/rails.c', line 670

static VALUE encoder_new(int argc, VALUE *argv, VALUE self) {
    Encoder e = OJ_R_ALLOC(struct _encoder);

    e->opts = oj_default_options;
    // Detach from the match_string regexps owned by the defaults before the
    // options are parsed so that a :match_string option builds a chain of its
    // own that is freed with the encoder.
    e->opts.str_rx.head = NULL;
    e->opts.str_rx.tail = NULL;
    copy_opts(&ropts, &e->ropts);

    if (1 <= argc && Qnil != *argv) {
        e->arg = *argv;
    } else {
        e->arg = rb_hash_new();
    }
    oj_parse_options(e->arg, &e->opts);
    oj_options_take_ownership(&e->opts);

    return TypedData_Wrap_Struct(encoder_class, &oj_encoder_type, e);
}

Instance Method Details

#deoptimize(*classes) ⇒ Object

Turn off Oj rails optimization on the specified classes.

  • classes [Class] a list of classes to deoptimize


837
838
839
840
841
842
843
844
# File 'ext/oj/rails.c', line 837

static VALUE encoder_deoptimize(int argc, VALUE *argv, VALUE self) {
    Encoder e;
    TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);

    optimize(argc, argv, &e->ropts, false);

    return Qnil;
}

#encode(obj) ⇒ Object

  • obj [Object] object to encode

Returns encoded object as a JSON string.



976
977
978
979
980
981
982
983
984
985
986
# File 'ext/oj/rails.c', line 976

static VALUE encoder_encode(VALUE self, VALUE obj) {
    Encoder e;
    TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);

    if (Qnil != e->arg) {
        VALUE argv[1] = {e->arg};

        return encode(obj, &e->ropts, &e->opts, 1, argv);
    }
    return encode(obj, &e->ropts, &e->opts, 0, NULL);
}

#optimize(*args) ⇒ Object

Document-method optimize call-seq: optimize(*classes)

Use Oj rails optimized routines to encode the specified classes. This ignores the as_json() method on the class and uses an internal encoding instead. Passing in no classes indicates all should use the optimized version of encoding for all previously optimized classes. Passing in the Object class set a global switch that will then use the optimized behavior for all classes.

  • classes [Class] a list of classes to optimize


780
781
782
783
784
785
786
787
# File 'ext/oj/rails.c', line 780

static VALUE encoder_optimize(int argc, VALUE *argv, VALUE self) {
    Encoder e;
    TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);

    optimize(argc, argv, &e->ropts, true);

    return Qnil;
}

#optimized?(clas) ⇒ Boolean

  • clas [Class] Class to check

Returns:

  • (Boolean)

    true if the class is being optimized for rails and false otherwise



867
868
869
870
871
872
873
874
875
876
877
878
# File 'ext/oj/rails.c', line 867

static VALUE encoder_optimized(VALUE self, VALUE clas) {
    Encoder e;
    ROpt    ro;

    TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);
    ro = oj_rails_get_opt(&e->ropts, clas);

    if (NULL == ro) {
        return Qfalse;
    }
    return (ro->on) ? Qtrue : Qfalse;
}