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
-
.deoptimize(*classes) ⇒ Object
Turn off Oj rails optimization on the specified classes.
-
.encode(obj, opts = nil) ⇒ Object
Encode obj as a JSON String.
-
.mimic_JSON ⇒ Object
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.
-
.optimize(*classes) ⇒ Object
Use Oj rails optimized routines to encode the specified classes.
-
.optimized?(clas) ⇒ Boolean
Returns true if the specified Class is being optimized.
-
.set_decoder ⇒ Object
Sets the JSON.parse function to be the Oj::parse function which is json gem compatible.
-
.set_encoder ⇒ Object
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.
Class Method Details
.deoptimize(*classes) ⇒ Object
Turn off Oj rails optimization on the specified classes.
-
classes [Class] a list of classes to deoptimize
586 587 588 589 590 591 |
# File 'ext/oj/rails.c', line 586 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]
738 739 740 741 742 743 744 745 746 747 748 |
# File 'ext/oj/rails.c', line 738 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, &, 0, NULL); } else { return encode(*argv, NULL, &, argc - 1, argv + 1); } } |
.mimic_JSON ⇒ Object
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.
548 549 550 551 552 553 554 555 556 557 558 559 560 561 |
# File 'ext/oj/rails.c', line 548 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); // TBD 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
535 536 537 538 539 540 |
# File 'ext/oj/rails.c', line 535 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.
616 617 618 619 620 621 622 623 624 |
# File 'ext/oj/rails.c', line 616 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_decoder ⇒ Object
Sets the JSON.parse function to be the Oj::parse function which is json gem compatible.
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 |
# File 'ext/oj/rails.c', line 835 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_encoder ⇒ Object
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.
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 |
# File 'ext/oj/rails.c', line 792 static VALUE rails_set_encoder(VALUE self) { VALUE active; VALUE json; VALUE encoding; VALUE pv; VALUE verbose; 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, "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"); .sec_prec = NUM2INT(pv); rb_undef_method(encoding, "time_precision="); rb_define_module_function(encoding, "time_precision=", rails_time_precision, 1); rb_gv_set("$VERBOSE", verbose); return Qnil; } |