Skip to content

Commit c29afad

Browse files
authored
Rails encoder fix (#984)
* Handle empty args to new encoder * Remove deprecated json tests * More test removal for 4.0
1 parent f28b0b7 commit c29afad

4 files changed

Lines changed: 22 additions & 18 deletions

File tree

ext/oj/rails.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,13 +661,15 @@ static VALUE encoder_new(int argc, VALUE *argv, VALUE self) {
661661
Encoder e = OJ_R_ALLOC(struct _encoder);
662662

663663
e->opts = oj_default_options;
664-
e->arg = Qnil;
665664
copy_opts(&ropts, &e->ropts);
666665

667666
if (1 <= argc && Qnil != *argv) {
668-
oj_parse_options(*argv, &e->opts);
669667
e->arg = *argv;
668+
} else {
669+
e->arg = rb_hash_new();
670670
}
671+
oj_parse_options(*argv, &e->opts);
672+
671673
return TypedData_Wrap_Struct(encoder_class, &oj_encoder_type, e);
672674
}
673675

test/json_gem/json_common_interface_test.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,13 @@ def test_state
5555
end
5656

5757
# This doesn't have anything to do with JSON parsing or generation. It seems
58-
# to be more of an internal tool that is exposed to users.
59-
def test_deep_const_get
60-
omit("mimic_JSON") unless REAL_JSON_GEM
61-
assert_raise(ArgumentError) { JSON.deep_const_get('Nix::Da') }
62-
assert_equal File::SEPARATOR, JSON.deep_const_get('File::SEPARATOR')
63-
end
58+
# to be more of an internal tool that is exposed to users. It also breaks in
59+
# ruby 4.0.0.
60+
# def test_deep_const_get
61+
# omit("mimic_JSON") unless REAL_JSON_GEM
62+
# assert_raise(ArgumentError) { JSON.deep_const_get('Nix::Da') }
63+
# assert_equal File::SEPARATOR, JSON.deep_const_get('File::SEPARATOR')
64+
# end
6465
##############################################################################
6566

6667
def test_create_id
@@ -142,11 +143,12 @@ def test_dump
142143
assert_equal too_deep, output.string
143144
end
144145

145-
def test_dump_should_modify_defaults
146-
max_nesting = JSON.dump_default_options[:max_nesting]
147-
JSON.dump([], StringIO.new, 10)
148-
assert_equal max_nesting, JSON.dump_default_options[:max_nesting]
149-
end
146+
# Removed after 3.0
147+
# def test_dump_should_modify_defaults
148+
# max_nesting = JSON.dump_default_options[:max_nesting]
149+
# JSON.dump([], StringIO.new, 10)
150+
# assert_equal max_nesting, JSON.dump_default_options[:max_nesting]
151+
# end
150152

151153
def test_JSON
152154
assert_equal @json, JSON(@hash)

test/json_gem/json_generic_object_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_parse_json
3333
:create_additions => true
3434
)
3535
ch = Hash
36-
# JSON after 2.10 no longer allows GenericObject creatable to be turned off or so it appears
36+
# JSON after 2.10 no longer allows GenericObject creatable to be turned off or so it appears.
3737
ch = JSON::GenericObject if REAL_JSON_GEM && ([2, 11, 0] <=> JSON::VERSION.split('.').map { |v| v.to_i }) <= 0
3838
assert_kind_of ch,
3939
JSON(

test/json_gem/json_parser_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,10 @@ def test_backslash
294294
json = '["\""]'
295295
data = ['"']
296296
assert_equal data, JSON.parse(json)
297-
#
298-
json = '["\\\'"]'
299-
data = ["'"]
300-
assert_equal data, JSON.parse(json)
297+
# no longer a valid escape sequence in ruby 4.0
298+
# json = '["\\\'"]'
299+
# data = ["'"]
300+
# assert_equal data, JSON.parse(json)
301301
end
302302

303303
class SubArray < Array

0 commit comments

Comments
 (0)