Skip to content

Commit

Permalink
Do not override Object#to_json with mimic_json
Browse files Browse the repository at this point in the history
  • Loading branch information
Watson1978 committed Aug 18, 2024
1 parent f5e928a commit 63df8d6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion ext/oj/mimic_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,9 @@ oj_define_mimic_json(int argc, VALUE *argv, VALUE self) {
}
oj_mimic_json_methods(json);

rb_define_method(rb_cObject, "to_json", mimic_object_to_json, -1);
if (!RTEST(rb_funcall2(rb_cObject, oj_method_defined_p_id, 1, &oj_to_json_sym))) {
rb_define_method(rb_cObject, "to_json", mimic_object_to_json, -1);
}

rb_gv_set("$VERBOSE", verbose);

Expand Down
7 changes: 7 additions & 0 deletions ext/oj/oj.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ ID oj_utc_id;
ID oj_utc_offset_id;
ID oj_utcq_id;
ID oj_write_id;
ID oj_method_defined_p_id;

VALUE oj_bag_class;
VALUE oj_bigdecimal_class;
Expand All @@ -98,6 +99,7 @@ VALUE oj_quirks_mode_sym;
VALUE oj_safe_sym;
VALUE oj_symbolize_names_sym;
VALUE oj_trace_sym;
VALUE oj_to_json_sym;

static VALUE allow_blank_sym;
static VALUE allow_gc_sym;
Expand Down Expand Up @@ -1887,6 +1889,8 @@ void Init_oj(void) {
oj_utcq_id = rb_intern("utc?");
oj_write_id = rb_intern("write");

oj_method_defined_p_id = rb_intern("method_defined?");

rb_require("oj/bag");
rb_require("oj/error");
rb_require("oj/mimic");
Expand Down Expand Up @@ -2061,6 +2065,9 @@ void Init_oj(void) {
xss_safe_sym = ID2SYM(rb_intern("xss_safe"));
rb_gc_register_address(&xss_safe_sym);

oj_to_json_sym = ID2SYM(oj_to_json_id);
rb_gc_register_address(&oj_to_json_sym);

oj_slash_string = rb_str_new2("/");
rb_gc_register_address(&oj_slash_string);
OBJ_FREEZE(oj_slash_string);
Expand Down
2 changes: 2 additions & 0 deletions ext/oj/oj.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ extern VALUE oj_space_before_sym;
extern VALUE oj_space_sym;
extern VALUE oj_symbolize_names_sym;
extern VALUE oj_trace_sym;
extern VALUE oj_to_json_sym;

extern VALUE oj_slash_string;

Expand Down Expand Up @@ -370,6 +371,7 @@ extern ID oj_utc_id;
extern ID oj_utc_offset_id;
extern ID oj_utcq_id;
extern ID oj_write_id;
extern ID oj_method_defined_p_id;

extern bool oj_use_hash_alt;
extern bool oj_use_array_alt;
Expand Down

0 comments on commit 63df8d6

Please sign in to comment.