Skip to content

Commit

Permalink
Make date replacement test in order
Browse files Browse the repository at this point in the history
  • Loading branch information
ohler55 committed Feb 13, 2022
1 parent ea10b81 commit 28a53cf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion ext/oj/dump_object.c
Expand Up @@ -446,7 +446,7 @@ static void dump_odd(VALUE obj, Odd odd, VALUE clas, int depth, Out out) {
assure_size(out, size);
name = rb_id2name(*idp);
nlen = strlen(name);
if (0 != *fp) {
if (NULL != *fp) {
v = (*fp)(obj);
} else if (0 == strchr(name, '.')) {
v = rb_funcall(obj, *idp, 0);
Expand Down
14 changes: 11 additions & 3 deletions ext/oj/odd.c
Expand Up @@ -14,7 +14,6 @@ static ID to_f_id;
static ID numerator_id;
static ID denominator_id;
static ID rational_id;
static VALUE rational_class;

static void set_class(Odd odd, const char *classname) {
const char **np;
Expand All @@ -23,7 +22,9 @@ static void set_class(Odd odd, const char *classname) {
odd->classname = classname;
odd->clen = strlen(classname);
odd->clas = rb_const_get(rb_cObject, rb_intern(classname));
rb_gc_register_address(&odd->clas);
odd->create_obj = odd->clas;
rb_gc_register_address(&odd->create_obj);
odd->create_op = rb_intern("new");
odd->is_module = (T_MODULE == rb_type(odd->clas));
odd->raw = 0;
Expand Down Expand Up @@ -55,7 +56,6 @@ void oj_odd_init() {
numerator_id = rb_intern("numerator");
denominator_id = rb_intern("denominator");
rational_id = rb_intern("Rational");
rational_class = rb_const_get(rb_cObject, rational_id);

memset(_odds, 0, sizeof(_odds));
odd = odds;
Expand Down Expand Up @@ -183,15 +183,23 @@ void oj_reg_odd(VALUE clas,
ID * ap;
AttrGetFunc *fp;

for (odd = odds + odd_cnt - 1; odds <= odd; odd--) {
rb_gc_unregister_address(&odd->clas);
rb_gc_unregister_address(&odd->create_obj);
}
if (_odds == odds) {
odds = ALLOC_N(struct _odd, odd_cnt + 1);

memcpy(odds, _odds, sizeof(struct _odd) * odd_cnt);
} else {
REALLOC_N(odds, struct _odd, odd_cnt + 1);
}
for (odd = odds + odd_cnt - 1; odds <= odd; odd--) {
rb_gc_register_address(&odd->clas);
rb_gc_register_address(&odd->create_obj);
}
odd = odds + odd_cnt;
odd->clas = clas;
rb_gc_register_address(&odd->clas);
if (NULL == (odd->classname = strdup(rb_class2name(clas)))) {
rb_raise(rb_eNoMemError, "for attribute name.");
}
Expand Down
13 changes: 6 additions & 7 deletions test/test_object.rb
Expand Up @@ -221,6 +221,7 @@ def setup

def teardown
Oj.default_options = @default_options
GC.verify_compaction_references(double_heap: true, toward: :empty)
end

def test_nil
Expand Down Expand Up @@ -948,6 +949,11 @@ def test_omit_nil

def test_odd_date
dump_and_load(Date.new(2012, 6, 19), false)

Oj.register_odd(Date, Date, :jd, :jd)
json = Oj.dump(Date.new(2015, 3, 7), :mode => :object)
assert_equal(%|{"^O":"Date","jd":2457089}|, json)
dump_and_load(Date.new(2012, 6, 19), false)
end

def test_odd_datetime
Expand All @@ -972,13 +978,6 @@ def test_odd_string
dump_and_load(s, false)
end

def test_odd_date_replaced
Oj.register_odd(Date, Date, :jd, :jd)
json = Oj.dump(Date.new(2015, 3, 7), :mode => :object)
assert_equal(%|{"^O":"Date","jd":2457089}|, json)
dump_and_load(Date.new(2012, 6, 19), false)
end

def test_odd_raw
Oj.register_odd_raw(Raw, Raw, :create, :to_json)
json = Oj.dump(Raw.new(%|{"a":1}|), :mode => :object)
Expand Down

0 comments on commit 28a53cf

Please sign in to comment.