Module: Oj::Rails

Defined in:
ext/oj/rails.c,
ext/oj/rails.c

Overview

Module that provides rails and active support compatibility.

Defined Under Namespace

Classes: Encoder

Class Method Summary collapse

Class Method Details

.deoptimize(*classes) ⇒ Object

Turn off Oj rails optimization on the specified classes.

  • classes [Class] a list of classes to deoptimize



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

static VALUE rails_deoptimize(int argc, VALUE *argv, VALUE self) {
    optimize(argc, argv, &ropts, false);
    string_writer_optimized = false;

    return Qnil;
}

.encode(obj, opts = nil) ⇒ Object

Encode obj as a JSON String.

  • obj [Object|Hash|Array] object to convert to a JSON String

  • opts [Hash] options

Returns [String]



983
984
985
986
987
988
989
990
991
992
# File 'ext/oj/rails.c', line 983

static VALUE rails_encode(int argc, VALUE *argv, VALUE self) {
    if (1 > argc) {
        rb_raise(rb_eArgError, "wrong number of arguments (0 for 1).");
    }
    if (1 == argc) {
        return encode(*argv, NULL, &oj_default_options, 0, NULL);
    } else {
        return encode(*argv, NULL, &oj_default_options, argc - 1, argv + 1);
    }
}

.mimic_JSONObject

Sets the JSON method to use Oj similar to Oj.mimic_JSON except with the ActiveSupport monkey patches instead of the json gem monkey patches.



802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
# File 'ext/oj/rails.c', line 802

VALUE
rails_mimic_json(VALUE self) {
    VALUE json;

    if (rb_const_defined_at(rb_cObject, rb_intern("JSON"))) {
        json = rb_const_get_at(rb_cObject, rb_intern("JSON"));
    } else {
        json = rb_define_module("JSON");
    }
    oj_mimic_json_methods(json);
    // Setting the default mode breaks the prmoise in the docs not to.
    //oj_default_options.mode = RailsMode;

    return Qnil;
}

.optimize(*classes) ⇒ Object

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



789
790
791
792
793
794
# File 'ext/oj/rails.c', line 789

static VALUE rails_optimize(int argc, VALUE *argv, VALUE self) {
    optimize(argc, argv, &ropts, true);
    string_writer_optimized = true;

    return Qnil;
}

.optimized?(clas) ⇒ Boolean

Returns true if the specified Class is being optimized.

Returns:

  • (Boolean)


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

static VALUE rails_optimized(VALUE self, VALUE clas) {
    ROpt ro = oj_rails_get_opt(&ropts, clas);

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

.set_decoderObject

Sets the JSON.parse function to be the Oj::parse function which is json gem compatible.



1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
# File 'ext/oj/rails.c', line 1104

static VALUE rails_set_decoder(VALUE self) {
    VALUE json;
    VALUE json_error;
    VALUE verbose;

    if (rb_const_defined_at(rb_cObject, rb_intern("JSON"))) {
        json = rb_const_get_at(rb_cObject, rb_intern("JSON"));
    } else {
        json = rb_define_module("JSON");
    }
    if (rb_const_defined_at(json, rb_intern("JSONError"))) {
        json_error = rb_const_get(json, rb_intern("JSONError"));
    } else {
        json_error = rb_define_class_under(json, "JSONError", rb_eStandardError);
    }
    if (rb_const_defined_at(json, rb_intern("ParserError"))) {
        oj_json_parser_error_class = rb_const_get(json, rb_intern("ParserError"));
    } else {
        oj_json_parser_error_class = rb_define_class_under(json, "ParserError", json_error);
    }
    // rb_undef_method doesn't work for modules or maybe sometimes
    // doesn't. Anyway setting verbose should hide the warning.
    verbose = rb_gv_get("$VERBOSE");
    rb_gv_set("$VERBOSE", Qfalse);
    rb_undef_method(json, "parse");
    rb_define_module_function(json, "parse", oj_mimic_parse, -1);
    rb_gv_set("$VERBOSE", verbose);

    return Qnil;
}

.set_encoderObject

Sets the ActiveSupport.encoder to Oj::Rails::Encoder and wraps some of the formatting globals used by ActiveSupport to allow the use of those globals in the Oj::Rails optimizations.



1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
# File 'ext/oj/rails.c', line 1038

static VALUE rails_set_encoder(VALUE self) {
    VALUE active;
    VALUE json;
    VALUE encoding;
    VALUE pv;
    VALUE verbose;
    VALUE enc = resolve_classpath("ActiveSupport::JSON::Encoding");

    if (Qnil != enc) {
        escape_html = Qtrue == rb_iv_get(self, "@escape_html_entities_in_json");
        xml_time    = Qtrue == rb_iv_get(enc, "@use_standard_json_time_format");
    }
    if (rb_const_defined_at(rb_cObject, rb_intern("ActiveSupport"))) {
        active = rb_const_get_at(rb_cObject, rb_intern("ActiveSupport"));
    } else {
        rb_raise(rb_eStandardError, "ActiveSupport not loaded.");
    }
    rb_funcall(active, rb_intern("json_encoder="), 1, encoder_class);

    json     = rb_const_get_at(active, rb_intern("JSON"));
    encoding = rb_const_get_at(json, rb_intern("Encoding"));

    // rb_undef_method doesn't work for modules or maybe sometimes
    // doesn't. Anyway setting verbose should hide the warning.
    verbose = rb_gv_get("$VERBOSE");
    rb_gv_set("$VERBOSE", Qfalse);
    rb_undef_method(encoding, "use_standard_json_time_format=");
    rb_define_module_function(encoding,
                              "use_standard_json_time_format=",
                              rails_use_standard_json_time_format,
                              1);
    rb_undef_method(encoding, "use_standard_json_time_format");
    rb_define_module_function(encoding,
                              "use_standard_json_time_format",
                              rails_use_standard_json_time_format_get,
                              0);

    pv          = rb_iv_get(encoding, "@escape_html_entities_in_json");
    escape_html = Qtrue == pv;
    rb_undef_method(encoding, "escape_html_entities_in_json=");
    rb_define_module_function(encoding,
                              "escape_html_entities_in_json=",
                              rails_escape_html_entities_in_json,
                              1);
    rb_undef_method(encoding, "escape_html_entities_in_json");
    rb_define_module_function(encoding,
                              "escape_html_entities_in_json",
                              rails_escape_html_entities_in_json_get,
                              0);

    pv                              = rb_iv_get(encoding, "@time_precision");
    oj_default_options.sec_prec     = NUM2INT(pv);
    oj_default_options.sec_prec_set = true;
    rb_undef_method(encoding, "time_precision=");
    rb_define_module_function(encoding, "time_precision=", rails_time_precision, 1);
    rb_gv_set("$VERBOSE", verbose);

    return Qnil;
}