Module: Oj::Rails

Defined in:
ext/oj/mimic_rails.c,
ext/oj/mimic_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



510
511
512
513
514
515
# File 'ext/oj/mimic_rails.c', line 510

static VALUE
rails_deoptimize(int argc, VALUE *argv, VALUE self) {
    optimize(argc, argv, &ropts, 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]



662
663
664
665
666
667
668
669
670
671
672
# File 'ext/oj/mimic_rails.c', line 662

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);
    }
}

.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



480
481
482
483
484
485
# File 'ext/oj/mimic_rails.c', line 480

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

    return Qnil;
}

.optimized?(clas) ⇒ Boolean

Returns true if the specified Class is being optimized.

Returns:

  • (Boolean)


540
541
542
543
544
545
546
547
548
# File 'ext/oj/mimic_rails.c', line 540

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.



753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
# File 'ext/oj/mimic_rails.c', line 753

static VALUE
rails_set_decoder(VALUE self) {
    VALUE	json;
    VALUE	json_error;
    
    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(json, "parse");
    rb_define_module_function(json, "parse", oj_mimic_parse, -1);
    
    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.



716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
# File 'ext/oj/mimic_rails.c', line 716

static VALUE
rails_set_encoder(VALUE self) {
    VALUE	active;
    VALUE	json;
    VALUE	encoding;
    VALUE	pv;
    
    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(active, "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, "escape_html_entities_in_json=");
    rb_define_module_function(encoding, "escape_html_entities_in_json=", rails_escape_html_entities_in_json, 1);

    pv = rb_iv_get(encoding, "@time_precision");
    oj_default_options.sec_prec = NUM2INT(pv);
    rb_undef_method(encoding, "time_precision=");
    rb_define_module_function(encoding, "time_precision=", rails_time_precision, 1);

    return Qnil;
}