oj 3.11.5 → 3.16.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1421 -0
- data/README.md +19 -5
- data/RELEASE_NOTES.md +61 -0
- data/ext/oj/buf.h +20 -6
- data/ext/oj/cache.c +329 -0
- data/ext/oj/cache.h +22 -0
- data/ext/oj/cache8.c +10 -9
- data/ext/oj/circarray.c +8 -6
- data/ext/oj/circarray.h +2 -2
- data/ext/oj/code.c +19 -33
- data/ext/oj/code.h +2 -2
- data/ext/oj/compat.c +27 -77
- data/ext/oj/custom.c +86 -179
- data/ext/oj/debug.c +126 -0
- data/ext/oj/dump.c +256 -249
- data/ext/oj/dump.h +26 -12
- data/ext/oj/dump_compat.c +565 -642
- data/ext/oj/dump_leaf.c +17 -63
- data/ext/oj/dump_object.c +65 -187
- data/ext/oj/dump_strict.c +27 -51
- data/ext/oj/encoder.c +43 -0
- data/ext/oj/err.c +2 -13
- data/ext/oj/err.h +24 -8
- data/ext/oj/extconf.rb +21 -6
- data/ext/oj/fast.c +149 -149
- data/ext/oj/intern.c +313 -0
- data/ext/oj/intern.h +22 -0
- data/ext/oj/mem.c +318 -0
- data/ext/oj/mem.h +53 -0
- data/ext/oj/mimic_json.c +121 -106
- data/ext/oj/object.c +85 -162
- data/ext/oj/odd.c +89 -67
- data/ext/oj/odd.h +15 -15
- data/ext/oj/oj.c +542 -411
- data/ext/oj/oj.h +99 -73
- data/ext/oj/parse.c +175 -187
- data/ext/oj/parse.h +26 -24
- data/ext/oj/parser.c +1600 -0
- data/ext/oj/parser.h +101 -0
- data/ext/oj/rails.c +112 -159
- data/ext/oj/rails.h +1 -1
- data/ext/oj/reader.c +11 -14
- data/ext/oj/reader.h +4 -2
- data/ext/oj/resolve.c +5 -24
- data/ext/oj/rxclass.c +7 -6
- data/ext/oj/rxclass.h +1 -1
- data/ext/oj/saj.c +22 -33
- data/ext/oj/saj2.c +584 -0
- data/ext/oj/saj2.h +23 -0
- data/ext/oj/scp.c +5 -28
- data/ext/oj/sparse.c +28 -72
- data/ext/oj/stream_writer.c +50 -40
- data/ext/oj/strict.c +56 -61
- data/ext/oj/string_writer.c +72 -39
- data/ext/oj/trace.h +31 -4
- data/ext/oj/usual.c +1218 -0
- data/ext/oj/usual.h +69 -0
- data/ext/oj/util.h +1 -1
- data/ext/oj/val_stack.c +14 -3
- data/ext/oj/val_stack.h +8 -7
- data/ext/oj/validate.c +46 -0
- data/ext/oj/wab.c +63 -88
- data/lib/oj/active_support_helper.rb +1 -3
- data/lib/oj/bag.rb +7 -1
- data/lib/oj/easy_hash.rb +4 -5
- data/lib/oj/error.rb +1 -2
- data/lib/oj/json.rb +162 -150
- data/lib/oj/mimic.rb +9 -7
- data/lib/oj/saj.rb +20 -6
- data/lib/oj/schandler.rb +5 -4
- data/lib/oj/state.rb +12 -8
- data/lib/oj/version.rb +1 -2
- data/lib/oj.rb +2 -0
- data/pages/Compatibility.md +1 -1
- data/pages/InstallOptions.md +20 -0
- data/pages/JsonGem.md +15 -0
- data/pages/Modes.md +8 -3
- data/pages/Options.md +43 -5
- data/pages/Parser.md +309 -0
- data/pages/Rails.md +14 -2
- data/test/_test_active.rb +8 -9
- data/test/_test_active_mimic.rb +7 -8
- data/test/_test_mimic_rails.rb +17 -20
- data/test/activerecord/result_test.rb +5 -6
- data/test/activesupport6/encoding_test.rb +63 -28
- data/test/{activesupport5 → activesupport7}/abstract_unit.rb +16 -12
- data/test/{activesupport5 → activesupport7}/decoding_test.rb +2 -10
- data/test/{activesupport5 → activesupport7}/encoding_test.rb +86 -50
- data/test/{activesupport5 → activesupport7}/encoding_test_cases.rb +6 -0
- data/test/{activesupport5 → activesupport7}/time_zone_test_helpers.rb +8 -0
- data/test/files.rb +15 -15
- data/test/foo.rb +16 -45
- data/test/helper.rb +11 -8
- data/test/isolated/shared.rb +3 -2
- data/test/json_gem/json_addition_test.rb +2 -2
- data/test/json_gem/json_common_interface_test.rb +8 -6
- data/test/json_gem/json_encoding_test.rb +0 -0
- data/test/json_gem/json_ext_parser_test.rb +1 -0
- data/test/json_gem/json_fixtures_test.rb +3 -2
- data/test/json_gem/json_generator_test.rb +56 -38
- data/test/json_gem/json_generic_object_test.rb +11 -11
- data/test/json_gem/json_parser_test.rb +54 -47
- data/test/json_gem/json_string_matching_test.rb +9 -9
- data/test/json_gem/test_helper.rb +7 -3
- data/test/mem.rb +34 -0
- data/test/perf.rb +22 -27
- data/test/perf_compat.rb +31 -33
- data/test/perf_dump.rb +50 -0
- data/test/perf_fast.rb +80 -82
- data/test/perf_file.rb +27 -29
- data/test/perf_object.rb +65 -69
- data/test/perf_once.rb +59 -0
- data/test/perf_parser.rb +183 -0
- data/test/perf_saj.rb +46 -54
- data/test/perf_scp.rb +58 -69
- data/test/perf_simple.rb +41 -39
- data/test/perf_strict.rb +74 -82
- data/test/perf_wab.rb +67 -69
- data/test/prec.rb +5 -5
- data/test/sample/change.rb +0 -1
- data/test/sample/dir.rb +0 -1
- data/test/sample/doc.rb +0 -1
- data/test/sample/file.rb +0 -1
- data/test/sample/group.rb +0 -1
- data/test/sample/hasprops.rb +0 -1
- data/test/sample/layer.rb +0 -1
- data/test/sample/rect.rb +0 -1
- data/test/sample/shape.rb +0 -1
- data/test/sample/text.rb +0 -1
- data/test/sample.rb +16 -16
- data/test/sample_json.rb +8 -8
- data/test/test_compat.rb +95 -43
- data/test/test_custom.rb +73 -51
- data/test/test_debian.rb +7 -10
- data/test/test_fast.rb +135 -79
- data/test/test_file.rb +41 -30
- data/test/test_gc.rb +16 -5
- data/test/test_generate.rb +5 -5
- data/test/test_hash.rb +5 -5
- data/test/test_integer_range.rb +9 -9
- data/test/test_null.rb +20 -20
- data/test/test_object.rb +99 -96
- data/test/test_parser.rb +11 -0
- data/test/test_parser_debug.rb +27 -0
- data/test/test_parser_saj.rb +337 -0
- data/test/test_parser_usual.rb +251 -0
- data/test/test_rails.rb +2 -2
- data/test/test_saj.rb +10 -8
- data/test/test_scp.rb +37 -39
- data/test/test_strict.rb +40 -32
- data/test/test_various.rb +165 -84
- data/test/test_wab.rb +48 -44
- data/test/test_writer.rb +47 -47
- data/test/tests.rb +13 -5
- data/test/tests_mimic.rb +12 -3
- data/test/tests_mimic_addition.rb +12 -3
- metadata +74 -128
- data/ext/oj/hash.c +0 -131
- data/ext/oj/hash.h +0 -19
- data/ext/oj/hash_test.c +0 -491
- data/test/activesupport4/decoding_test.rb +0 -108
- data/test/activesupport4/encoding_test.rb +0 -531
- data/test/activesupport4/test_helper.rb +0 -41
- data/test/activesupport5/test_helper.rb +0 -72
- data/test/bar.rb +0 -35
- data/test/baz.rb +0 -16
- data/test/zoo.rb +0 -13
data/test/test_compat.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
#
|
2
|
+
# frozen_string_literal: true
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
$LOAD_PATH << __dir__
|
5
|
+
@oj_dir = File.dirname(File.expand_path(__dir__))
|
6
6
|
%w(lib ext).each do |dir|
|
7
|
-
|
7
|
+
$LOAD_PATH << File.join(@oj_dir, dir)
|
8
8
|
end
|
9
9
|
|
10
10
|
require 'minitest'
|
@@ -29,18 +29,16 @@ class CompatJuice < Minitest::Test
|
|
29
29
|
end
|
30
30
|
alias == eql?
|
31
31
|
|
32
|
-
def to_json(*
|
33
|
-
%|{"json_class":"#{self.class
|
32
|
+
def to_json(*_a)
|
33
|
+
%|{"json_class":"#{self.class}","x":#{@x},"y":#{@y}}|
|
34
34
|
end
|
35
35
|
|
36
36
|
def self.json_create(h)
|
37
|
-
|
37
|
+
new(h['x'], h['y'])
|
38
38
|
end
|
39
39
|
end # Jeez
|
40
40
|
|
41
41
|
class Argy
|
42
|
-
def initialize()
|
43
|
-
end
|
44
42
|
|
45
43
|
def to_json(*a)
|
46
44
|
%|{"args":"#{a}"}|
|
@@ -48,10 +46,8 @@ class CompatJuice < Minitest::Test
|
|
48
46
|
end # Argy
|
49
47
|
|
50
48
|
class Stringy
|
51
|
-
def initialize()
|
52
|
-
end
|
53
49
|
|
54
|
-
def to_s
|
50
|
+
def to_s
|
55
51
|
%|[1,2]|
|
56
52
|
end
|
57
53
|
end # Stringy
|
@@ -61,20 +57,17 @@ class CompatJuice < Minitest::Test
|
|
61
57
|
module Three
|
62
58
|
class Deep
|
63
59
|
|
64
|
-
def initialize()
|
65
|
-
end
|
66
|
-
|
67
60
|
def eql?(o)
|
68
61
|
self.class == o.class
|
69
62
|
end
|
70
63
|
alias == eql?
|
71
64
|
|
72
|
-
def to_json(*
|
65
|
+
def to_json(*_a)
|
73
66
|
%|{"json_class":"#{self.class.name}"}|
|
74
67
|
end
|
75
68
|
|
76
|
-
def self.json_create(
|
77
|
-
|
69
|
+
def self.json_create(_h)
|
70
|
+
new()
|
78
71
|
end
|
79
72
|
end # Deep
|
80
73
|
end # Three
|
@@ -106,24 +99,30 @@ class CompatJuice < Minitest::Test
|
|
106
99
|
|
107
100
|
def test_fixnum
|
108
101
|
dump_and_load(0, false)
|
109
|
-
dump_and_load(
|
110
|
-
dump_and_load(-
|
102
|
+
dump_and_load(12_345, false)
|
103
|
+
dump_and_load(-54_321, false)
|
111
104
|
dump_and_load(1, false)
|
112
105
|
end
|
113
106
|
|
107
|
+
def test_fixnum_array
|
108
|
+
data = (1..1000).to_a
|
109
|
+
json = Oj.dump(data, mode: :compat)
|
110
|
+
assert_equal("[#{data.join(',')}]", json)
|
111
|
+
end
|
112
|
+
|
114
113
|
def test_float
|
115
114
|
dump_and_load(0.0, false)
|
116
115
|
dump_and_load(0.56, false)
|
117
116
|
dump_and_load(3.0, false)
|
118
|
-
dump_and_load(
|
117
|
+
dump_and_load(12_345.6789, false)
|
119
118
|
dump_and_load(70.35, false)
|
120
|
-
dump_and_load(-
|
119
|
+
dump_and_load(-54_321.012, false)
|
121
120
|
dump_and_load(1.7775, false)
|
122
121
|
dump_and_load(2.5024, false)
|
123
122
|
dump_and_load(2.48e16, false)
|
124
123
|
dump_and_load(2.48e100 * 1.0e10, false)
|
125
124
|
dump_and_load(-2.48e100 * 1.0e10, false)
|
126
|
-
dump_and_load(
|
125
|
+
dump_and_load(1_405_460_727.723866, false)
|
127
126
|
dump_and_load(0.5773, false)
|
128
127
|
dump_and_load(0.6768, false)
|
129
128
|
dump_and_load(0.685, false)
|
@@ -148,7 +147,7 @@ class CompatJuice < Minitest::Test
|
|
148
147
|
def test_encode
|
149
148
|
opts = Oj.default_options
|
150
149
|
Oj.default_options = { :ascii_only => true }
|
151
|
-
json = Oj.dump(
|
150
|
+
json = Oj.dump('ぴーたー')
|
152
151
|
assert_equal(%{"\\u3074\\u30fc\\u305f\\u30fc"}, json)
|
153
152
|
Oj.default_options = opts
|
154
153
|
end
|
@@ -170,7 +169,7 @@ class CompatJuice < Minitest::Test
|
|
170
169
|
end
|
171
170
|
|
172
171
|
def test_array_deep
|
173
|
-
dump_and_load([1,[2,[3,[4,[5,[6,[7,[8,[9,[10,[11,[12,[13,[14,[15,[16,[17,[18,[19,[20]]]]]]]]]]]]]]]]]]]], false)
|
172
|
+
dump_and_load([1, [2, [3, [4, [5, [6, [7, [8, [9, [10, [11, [12, [13, [14, [15, [16, [17, [18, [19, [20]]]]]]]]]]]]]]]]]]]], false)
|
174
173
|
end
|
175
174
|
|
176
175
|
def test_symbol
|
@@ -179,8 +178,8 @@ class CompatJuice < Minitest::Test
|
|
179
178
|
end
|
180
179
|
|
181
180
|
def test_time_xml_schema
|
182
|
-
t = Time.xmlschema(
|
183
|
-
#t = Time.local(2012, 1, 5, 23, 58, 7, 123456)
|
181
|
+
t = Time.xmlschema('2012-01-05T23:58:07.123456000+09:00')
|
182
|
+
# t = Time.local(2012, 1, 5, 23, 58, 7, 123456)
|
184
183
|
json = Oj.dump(t, :mode => :compat)
|
185
184
|
assert_equal(%{"2012-01-05 23:58:07 +0900"}, json)
|
186
185
|
end
|
@@ -199,8 +198,9 @@ class CompatJuice < Minitest::Test
|
|
199
198
|
def test_non_str_hash
|
200
199
|
json = Oj.dump({ 1 => true, 0 => false }, :mode => :compat)
|
201
200
|
h = Oj.load(json, :mode => :strict)
|
202
|
-
assert_equal({
|
201
|
+
assert_equal({ '1' => true, '0' => false }, h)
|
203
202
|
end
|
203
|
+
|
204
204
|
def test_hash
|
205
205
|
dump_and_load({}, false)
|
206
206
|
dump_and_load({ 'true' => true, 'false' => false}, false)
|
@@ -239,12 +239,12 @@ class CompatJuice < Minitest::Test
|
|
239
239
|
def test_invalid_escapes_handled
|
240
240
|
json = '{"subtext":"\"404er\” \w \k \3 \a"}'
|
241
241
|
obj = Oj.compat_load(json)
|
242
|
-
assert_equal({
|
242
|
+
assert_equal({'subtext' => '"404er” w k 3 a'}, obj)
|
243
243
|
end
|
244
244
|
|
245
245
|
def test_hash_escaping
|
246
246
|
json = Oj.to_json({'<>' => '<>'}, mode: :compat)
|
247
|
-
assert_equal(
|
247
|
+
assert_equal('{"<>":"<>"}', json)
|
248
248
|
end
|
249
249
|
|
250
250
|
def test_bignum_object
|
@@ -271,7 +271,7 @@ class CompatJuice < Minitest::Test
|
|
271
271
|
def test_bignum_compat
|
272
272
|
json = Oj.dump(7 ** 55, :mode => :compat)
|
273
273
|
b = Oj.load(json, :mode => :strict)
|
274
|
-
assert_equal(
|
274
|
+
assert_equal(30_226_801_971_775_055_948_247_051_683_954_096_612_865_741_943, b)
|
275
275
|
end
|
276
276
|
|
277
277
|
# BigDecimal
|
@@ -313,7 +313,7 @@ class CompatJuice < Minitest::Test
|
|
313
313
|
if x.is_a?(String)
|
314
314
|
assert_equal(orig.to_s, x)
|
315
315
|
else # better be a Hash
|
316
|
-
assert_equal({
|
316
|
+
assert_equal({'year' => orig.year, 'month' => orig.month, 'day' => orig.day, 'start' => orig.start}, x)
|
317
317
|
end
|
318
318
|
end
|
319
319
|
|
@@ -340,13 +340,13 @@ class CompatJuice < Minitest::Test
|
|
340
340
|
end
|
341
341
|
|
342
342
|
def test_io_file
|
343
|
-
filename = File.join(
|
344
|
-
File.
|
343
|
+
filename = File.join(__dir__, 'open_file_test.json')
|
344
|
+
File.write(filename, %{{
|
345
345
|
"x":true,
|
346
346
|
"y":58,
|
347
347
|
"z": [1,2,3]
|
348
348
|
}
|
349
|
-
})
|
349
|
+
})
|
350
350
|
f = File.new(filename)
|
351
351
|
obj = Oj.compat_load(f)
|
352
352
|
f.close()
|
@@ -410,7 +410,7 @@ class CompatJuice < Minitest::Test
|
|
410
410
|
|
411
411
|
# A child to_json should not be called.
|
412
412
|
def test_json_object_child
|
413
|
-
obj = {
|
413
|
+
obj = { 'child' => Jeez.new(true, 58) }
|
414
414
|
assert_equal('{"child":{"json_class":"CompatJuice::Jeez","x":true,"y":58}}', Oj.dump(obj))
|
415
415
|
end
|
416
416
|
|
@@ -431,10 +431,10 @@ class CompatJuice < Minitest::Test
|
|
431
431
|
begin
|
432
432
|
Oj.compat_load(json, :create_additions => true)
|
433
433
|
rescue Exception => e
|
434
|
-
assert_equal(
|
434
|
+
assert_equal('ArgumentError', e.class().name)
|
435
435
|
return
|
436
436
|
end
|
437
|
-
assert(false,
|
437
|
+
assert(false, '*** expected an exception')
|
438
438
|
end
|
439
439
|
|
440
440
|
def test_json_object_create_cache
|
@@ -450,7 +450,7 @@ class CompatJuice < Minitest::Test
|
|
450
450
|
expected = Jeez.new(true, 58)
|
451
451
|
json = Oj.to_json(expected)
|
452
452
|
json.gsub!('json_class', '_class_')
|
453
|
-
obj = Oj.compat_load(json, :create_id =>
|
453
|
+
obj = Oj.compat_load(json, :create_id => '_class_', :create_additions => true)
|
454
454
|
assert_equal(expected, obj)
|
455
455
|
end
|
456
456
|
|
@@ -467,10 +467,28 @@ class CompatJuice < Minitest::Test
|
|
467
467
|
end
|
468
468
|
|
469
469
|
def test_arg_passing
|
470
|
-
json = Oj.to_json(Argy.new(), :max_nesting=> 40)
|
470
|
+
json = Oj.to_json(Argy.new(), :max_nesting => 40)
|
471
471
|
assert_equal(%|{"args":"[{:max_nesting=>40}]"}|, json)
|
472
472
|
end
|
473
473
|
|
474
|
+
def test_max_nesting
|
475
|
+
assert_raises() { Oj.to_json([[[[[]]]]], :max_nesting => 3) }
|
476
|
+
assert_raises() { Oj.dump([[[[[]]]]], :max_nesting => 3, :mode=>:compat) }
|
477
|
+
|
478
|
+
assert_raises() { Oj.to_json([[]], :max_nesting => 1) }
|
479
|
+
assert_equal('[[]]', Oj.to_json([[]], :max_nesting => 2))
|
480
|
+
|
481
|
+
assert_raises() { Oj.dump([[]], :max_nesting => 1, :mode=>:compat) }
|
482
|
+
assert_equal('[[]]', Oj.dump([[]], :max_nesting => 2, :mode=>:compat))
|
483
|
+
|
484
|
+
assert_raises() { Oj.to_json([[3]], :max_nesting => 1) }
|
485
|
+
assert_equal('[[3]]', Oj.to_json([[3]], :max_nesting => 2))
|
486
|
+
|
487
|
+
assert_raises() { Oj.dump([[3]], :max_nesting => 1, :mode=>:compat) }
|
488
|
+
assert_equal('[[3]]', Oj.dump([[3]], :max_nesting => 2, :mode=>:compat))
|
489
|
+
|
490
|
+
end
|
491
|
+
|
474
492
|
def test_bad_unicode
|
475
493
|
assert_raises() { Oj.to_json("\xE4xy") }
|
476
494
|
end
|
@@ -485,13 +503,47 @@ class CompatJuice < Minitest::Test
|
|
485
503
|
|
486
504
|
def test_parse_to_s
|
487
505
|
s = Stringy.new
|
488
|
-
assert_equal([1,2], Oj.load(s, :mode => :compat))
|
506
|
+
assert_equal([1, 2], Oj.load(s, :mode => :compat))
|
507
|
+
end
|
508
|
+
|
509
|
+
def test_parse_large_string
|
510
|
+
error = assert_raises() { Oj.load(%|{"a":"aaaaaaaaaa\0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}|) }
|
511
|
+
assert_includes(error.message, 'NULL byte in string')
|
512
|
+
|
513
|
+
error = assert_raises() { Oj.load(%|{"a":"aaaaaaaaaaaaaaaaaaaa }|) }
|
514
|
+
assert_includes(error.message, 'quoted string not terminated')
|
515
|
+
|
516
|
+
json =<<~JSON
|
517
|
+
{
|
518
|
+
"a": "\\u3074\\u30fc\\u305f\\u30fc",
|
519
|
+
"b": "aaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
520
|
+
}
|
521
|
+
JSON
|
522
|
+
assert_equal('ぴーたー', Oj.load(json)['a'])
|
523
|
+
end
|
524
|
+
|
525
|
+
def test_parse_large_escaped_string
|
526
|
+
invalid_json = %|{"a":"aaaa\\nbbbb\\rcccc\\tddd\\feee\\bf/\\\\\\u3074\\u30fc\\u305f\\u30fc }|
|
527
|
+
error = assert_raises() { Oj.load(invalid_json) }
|
528
|
+
assert_includes(error.message, 'quoted string not terminated')
|
529
|
+
|
530
|
+
json = '"aaaa\\nbbbb\\rcccc\\tddd\\feee\\bf/\\\\\\u3074\\u30fc\\u305f\\u30fc "'
|
531
|
+
assert_equal("aaaa\nbbbb\rcccc\tddd\feee\bf/\\ぴーたー ", Oj.load(json))
|
532
|
+
end
|
533
|
+
|
534
|
+
def test_invalid_to_s
|
535
|
+
obj = Object.new
|
536
|
+
def obj.to_s
|
537
|
+
nil
|
538
|
+
end
|
539
|
+
|
540
|
+
assert_raises(TypeError) { Oj.dump(obj, mode: :compat) }
|
489
541
|
end
|
490
542
|
|
491
543
|
def dump_and_load(obj, trace=false)
|
492
544
|
json = Oj.dump(obj)
|
493
545
|
puts json if trace
|
494
|
-
loaded = Oj.compat_load(json, :create_additions => true)
|
546
|
+
loaded = Oj.compat_load(json, :create_additions => true)
|
495
547
|
if obj.nil?
|
496
548
|
assert_nil(loaded)
|
497
549
|
else
|
@@ -503,7 +555,7 @@ class CompatJuice < Minitest::Test
|
|
503
555
|
def dump_to_json_and_load(obj, trace=false)
|
504
556
|
json = Oj.to_json(obj, :indent => ' ')
|
505
557
|
puts json if trace
|
506
|
-
loaded = Oj.compat_load(json, :create_additions => true)
|
558
|
+
loaded = Oj.compat_load(json, :create_additions => true)
|
507
559
|
if obj.nil?
|
508
560
|
assert_nil(loaded)
|
509
561
|
else
|
data/test/test_custom.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
#
|
2
|
+
# frozen_string_literal: true
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
$LOAD_PATH << __dir__
|
5
|
+
@oj_dir = File.dirname(File.expand_path(__dir__))
|
6
6
|
%w(lib ext).each do |dir|
|
7
|
-
|
7
|
+
$LOAD_PATH << File.join(@oj_dir, dir)
|
8
8
|
end
|
9
9
|
|
10
10
|
require 'minitest'
|
@@ -27,19 +27,24 @@ class CustomJuice < Minitest::Test
|
|
27
27
|
@y = y
|
28
28
|
@_z = x.to_s
|
29
29
|
end
|
30
|
+
|
30
31
|
def ==(o)
|
31
32
|
self.class == o.class && @x == o.x && @y = o.y
|
32
33
|
end
|
33
|
-
|
34
|
+
|
35
|
+
def to_json(*_args)
|
34
36
|
%|{"xx":#{@x},"yy":#{y}}|
|
35
37
|
end
|
36
|
-
|
38
|
+
|
39
|
+
def raw_json(_depth, _indent)
|
37
40
|
%|{"xxx":#{@x},"yyy":#{y}}|
|
38
41
|
end
|
39
|
-
|
42
|
+
|
43
|
+
def as_json(*_args)
|
40
44
|
{'a' => @x, :b => @y }
|
41
45
|
end
|
42
|
-
|
46
|
+
|
47
|
+
def to_hash
|
43
48
|
{'b' => @x, 'n' => @y }
|
44
49
|
end
|
45
50
|
end
|
@@ -51,10 +56,12 @@ class CustomJuice < Minitest::Test
|
|
51
56
|
@x = x
|
52
57
|
@y = y
|
53
58
|
end
|
59
|
+
|
54
60
|
def ==(o)
|
55
61
|
self.class == o.class && @x == o.x && @y = o.y
|
56
62
|
end
|
57
|
-
|
63
|
+
|
64
|
+
def as_json(*_args)
|
58
65
|
{'a' => @x, :b => @y }
|
59
66
|
end
|
60
67
|
end
|
@@ -66,10 +73,12 @@ class CustomJuice < Minitest::Test
|
|
66
73
|
@x = x
|
67
74
|
@y = y
|
68
75
|
end
|
76
|
+
|
69
77
|
def ==(o)
|
70
78
|
self.class == o.class && @x == o.x && @y = o.y
|
71
79
|
end
|
72
|
-
|
80
|
+
|
81
|
+
def as_json(*_args)
|
73
82
|
a = @x
|
74
83
|
a = a.as_json if a.respond_to?('as_json')
|
75
84
|
b = @y
|
@@ -101,16 +110,16 @@ class CustomJuice < Minitest::Test
|
|
101
110
|
|
102
111
|
def test_fixnum
|
103
112
|
dump_and_load(0, false)
|
104
|
-
dump_and_load(
|
105
|
-
dump_and_load(-
|
113
|
+
dump_and_load(12_345, false)
|
114
|
+
dump_and_load(-54_321, false)
|
106
115
|
dump_and_load(1, false)
|
107
116
|
end
|
108
117
|
|
109
118
|
def test_float
|
110
119
|
dump_and_load(0.0, false)
|
111
|
-
dump_and_load(
|
120
|
+
dump_and_load(12_345.6789, false)
|
112
121
|
dump_and_load(70.35, false)
|
113
|
-
dump_and_load(-
|
122
|
+
dump_and_load(-54_321.012, false)
|
114
123
|
dump_and_load(1.7775, false)
|
115
124
|
dump_and_load(2.5024, false)
|
116
125
|
dump_and_load(2.48e16, false)
|
@@ -119,12 +128,12 @@ class CustomJuice < Minitest::Test
|
|
119
128
|
end
|
120
129
|
|
121
130
|
def test_float_parse
|
122
|
-
f = Oj.load(
|
131
|
+
f = Oj.load('12.123456789012345678', mode: :custom, bigdecimal_load: :float)
|
123
132
|
assert_equal(Float, f.class)
|
124
133
|
end
|
125
134
|
|
126
135
|
def test_float_parse_fast
|
127
|
-
f = Oj.load(
|
136
|
+
f = Oj.load('12.123456789012345678', mode: :custom, bigdecimal_load: :fast)
|
128
137
|
assert_equal(Float, f.class)
|
129
138
|
assert(12.12345678901234 <= f && f < 12.12345678901236)
|
130
139
|
end
|
@@ -139,7 +148,7 @@ class CustomJuice < Minitest::Test
|
|
139
148
|
assert(true)
|
140
149
|
return
|
141
150
|
end
|
142
|
-
assert(false,
|
151
|
+
assert(false, '*** expected an exception')
|
143
152
|
end
|
144
153
|
|
145
154
|
def test_infinity_dump
|
@@ -152,7 +161,7 @@ class CustomJuice < Minitest::Test
|
|
152
161
|
assert(true)
|
153
162
|
return
|
154
163
|
end
|
155
|
-
assert(false,
|
164
|
+
assert(false, '*** expected an exception')
|
156
165
|
end
|
157
166
|
|
158
167
|
def test_neg_infinity_dump
|
@@ -165,7 +174,7 @@ class CustomJuice < Minitest::Test
|
|
165
174
|
assert(true)
|
166
175
|
return
|
167
176
|
end
|
168
|
-
assert(false,
|
177
|
+
assert(false, '*** expected an exception')
|
169
178
|
end
|
170
179
|
|
171
180
|
def test_string
|
@@ -176,15 +185,15 @@ class CustomJuice < Minitest::Test
|
|
176
185
|
end
|
177
186
|
|
178
187
|
def test_string_ascii
|
179
|
-
json = Oj.dump(
|
188
|
+
json = Oj.dump('ぴーたー', :escape_mode => :ascii)
|
180
189
|
assert_equal(%{"\\u3074\\u30fc\\u305f\\u30fc"}, json)
|
181
|
-
dump_and_load(
|
190
|
+
dump_and_load('ぴーたー', false, :escape_mode => :ascii)
|
182
191
|
end
|
183
192
|
|
184
193
|
def test_string_json
|
185
|
-
json = Oj.dump(
|
194
|
+
json = Oj.dump('ぴーたー', :escape_mode => :json)
|
186
195
|
assert_equal(%{"ぴーたー"}, json)
|
187
|
-
dump_and_load(
|
196
|
+
dump_and_load('ぴーたー', false, :escape_mode => :json)
|
188
197
|
end
|
189
198
|
|
190
199
|
def test_array
|
@@ -196,15 +205,17 @@ class CustomJuice < Minitest::Test
|
|
196
205
|
end
|
197
206
|
|
198
207
|
def test_array_deep
|
199
|
-
dump_and_load([1,[2,[3,[4,[5,[6,[7,[8,[9,[10,[11,[12,[13,[14,[15,[16,[17,[18,[19,[20]]]]]]]]]]]]]]]]]]]], false)
|
208
|
+
dump_and_load([1, [2, [3, [4, [5, [6, [7, [8, [9, [10, [11, [12, [13, [14, [15, [16, [17, [18, [19, [20]]]]]]]]]]]]]]]]]]]], false)
|
200
209
|
end
|
201
210
|
|
202
211
|
def test_deep_nest
|
212
|
+
skip 'TruffleRuby causes SEGV' if RUBY_ENGINE == 'truffleruby'
|
213
|
+
|
203
214
|
begin
|
204
|
-
n =
|
205
|
-
Oj.strict_load('[' * n + ']' * n)
|
215
|
+
n = 10_000
|
216
|
+
Oj.strict_load(('[' * n) + (']' * n))
|
206
217
|
rescue Exception => e
|
207
|
-
|
218
|
+
refute(e.message)
|
208
219
|
end
|
209
220
|
end
|
210
221
|
|
@@ -259,11 +270,11 @@ class CustomJuice < Minitest::Test
|
|
259
270
|
|
260
271
|
def test_object
|
261
272
|
obj = Jeez.new(true, 58)
|
262
|
-
json = Oj.dump(obj, create_id:
|
273
|
+
json = Oj.dump(obj, create_id: '^o', use_to_json: false, use_as_json: false, use_to_hash: false)
|
263
274
|
assert_equal(%|{"x":true,"y":58,"_z":"true"}|, json)
|
264
|
-
json = Oj.dump(obj, create_id:
|
275
|
+
json = Oj.dump(obj, create_id: '^o', use_to_json: false, use_as_json: false, use_to_hash: false, ignore_under: true)
|
265
276
|
assert_equal(%|{"x":true,"y":58}|, json)
|
266
|
-
dump_and_load(obj, false, :create_id =>
|
277
|
+
dump_and_load(obj, false, :create_id => '^o', :create_additions => true)
|
267
278
|
end
|
268
279
|
|
269
280
|
def test_object_to_json
|
@@ -341,7 +352,6 @@ class CustomJuice < Minitest::Test
|
|
341
352
|
"b":[3]
|
342
353
|
}
|
343
354
|
|, json)
|
344
|
-
|
345
355
|
end
|
346
356
|
|
347
357
|
def test_symbol
|
@@ -392,24 +402,29 @@ class CustomJuice < Minitest::Test
|
|
392
402
|
assert_equal(%|{"x":{"a":1}}|, json)
|
393
403
|
end
|
394
404
|
|
405
|
+
def test_omit_null_byte
|
406
|
+
json = Oj.dump({ "fo\x00o" => "b\x00ar" }, :omit_null_byte => true)
|
407
|
+
assert_equal(%|{"foo":"bar"}|, json)
|
408
|
+
end
|
409
|
+
|
395
410
|
def test_complex
|
396
411
|
obj = Complex(2, 9)
|
397
|
-
dump_and_load(obj, false, :create_id =>
|
412
|
+
dump_and_load(obj, false, :create_id => '^o', :create_additions => true)
|
398
413
|
end
|
399
414
|
|
400
415
|
def test_rational
|
401
416
|
obj = Rational(2, 9)
|
402
|
-
dump_and_load(obj, false, :create_id =>
|
417
|
+
dump_and_load(obj, false, :create_id => '^o', :create_additions => true)
|
403
418
|
end
|
404
419
|
|
405
420
|
def test_range
|
406
421
|
obj = 3..8
|
407
|
-
dump_and_load(obj, false, :create_id =>
|
422
|
+
dump_and_load(obj, false, :create_id => '^o', :create_additions => true)
|
408
423
|
end
|
409
424
|
|
410
425
|
def test_date
|
411
426
|
obj = Date.new(2017, 1, 5)
|
412
|
-
dump_and_load(obj, false, :create_id =>
|
427
|
+
dump_and_load(obj, false, :create_id => '^o', :create_additions => true)
|
413
428
|
end
|
414
429
|
|
415
430
|
def test_date_unix
|
@@ -438,7 +453,7 @@ class CustomJuice < Minitest::Test
|
|
438
453
|
|
439
454
|
def test_datetime
|
440
455
|
obj = DateTime.new(2017, 1, 5, 10, 20, 30)
|
441
|
-
dump_and_load(obj, false, :create_id =>
|
456
|
+
dump_and_load(obj, false, :create_id => '^o', :create_additions => true)
|
442
457
|
end
|
443
458
|
|
444
459
|
def test_datetime_unix
|
@@ -448,12 +463,9 @@ class CustomJuice < Minitest::Test
|
|
448
463
|
end
|
449
464
|
|
450
465
|
def test_datetime_unix_zone
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
json = Oj.dump(obj, :indent => 2, time_format: :unix_zone)
|
455
|
-
assert_equal('1483629630.000000000e-18000', json)
|
456
|
-
end
|
466
|
+
obj = DateTime.new(2017, 1, 5, 10, 20, 30, '-0500')
|
467
|
+
json = Oj.dump(obj, :indent => 2, time_format: :unix_zone)
|
468
|
+
assert_equal('1483629630.000000000e-18000', json)
|
457
469
|
end
|
458
470
|
|
459
471
|
def test_datetime_ruby
|
@@ -471,20 +483,30 @@ class CustomJuice < Minitest::Test
|
|
471
483
|
def test_regexp
|
472
484
|
# this notation must be used to get an == match later
|
473
485
|
obj = /(?ix-m:^yes$)/
|
474
|
-
dump_and_load(obj, false, :create_id =>
|
486
|
+
dump_and_load(obj, false, :create_id => '^o', :create_additions => true)
|
475
487
|
end
|
476
488
|
|
477
489
|
def test_openstruct
|
478
490
|
obj = OpenStruct.new(:a => 1, 'b' => 2)
|
479
|
-
dump_and_load(obj, false, :create_id =>
|
491
|
+
dump_and_load(obj, false, :create_id => '^o', :create_additions => true)
|
480
492
|
end
|
481
493
|
|
482
494
|
def test_time
|
495
|
+
skip 'TruffleRuby fails this spec' if RUBY_ENGINE == 'truffleruby'
|
496
|
+
|
483
497
|
obj = Time.now()
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
498
|
+
# These two forms should be able to recreate the time precisely,
|
499
|
+
# so we check they can load a dumped version and recreate the
|
500
|
+
# original object correctly.
|
501
|
+
dump_and_load(obj, false, :time_format => :unix, :create_id => '^o', :create_additions => true)
|
502
|
+
dump_and_load(obj, false, :time_format => :unix_zone, :create_id => '^o', :create_additions => true)
|
503
|
+
# These two forms will lose precision while dumping as they don't
|
504
|
+
# preserve full precision. We check that a dumped version is equal
|
505
|
+
# to that version loaded and dumped a second time, but don't check
|
506
|
+
# that the loaded Ruby object is still the same as the original.
|
507
|
+
dump_load_dump(obj, false, :time_format => :xmlschema, :create_id => '^o', :create_additions => true)
|
508
|
+
dump_load_dump(obj, false, :time_format => :xmlschema, :create_id => '^o', :create_additions => true, second_precision: 3)
|
509
|
+
dump_load_dump(obj, false, :time_format => :ruby, :create_id => '^o', :create_additions => true)
|
488
510
|
end
|
489
511
|
|
490
512
|
def dump_and_load(obj, trace=false, options={})
|
@@ -492,7 +514,7 @@ class CustomJuice < Minitest::Test
|
|
492
514
|
json = Oj.dump(obj, options)
|
493
515
|
puts json if trace
|
494
516
|
|
495
|
-
loaded = Oj.load(json, options)
|
517
|
+
loaded = Oj.load(json, options)
|
496
518
|
if obj.nil?
|
497
519
|
assert_nil(loaded)
|
498
520
|
else
|
@@ -506,7 +528,7 @@ class CustomJuice < Minitest::Test
|
|
506
528
|
json = Oj.dump(obj, options)
|
507
529
|
puts json if trace
|
508
530
|
|
509
|
-
loaded = Oj.load(json, options)
|
531
|
+
loaded = Oj.load(json, options)
|
510
532
|
if obj.nil?
|
511
533
|
assert_nil(loaded)
|
512
534
|
else
|
@@ -520,7 +542,7 @@ class CustomJuice < Minitest::Test
|
|
520
542
|
json = Oj.dump(obj, options)
|
521
543
|
puts json if trace
|
522
544
|
|
523
|
-
loaded = Oj.load(json, options)
|
545
|
+
loaded = Oj.load(json, options)
|
524
546
|
if obj.nil?
|
525
547
|
assert_nil(loaded)
|
526
548
|
else
|
data/test/test_debian.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'helper'
|
4
4
|
|
@@ -16,22 +16,19 @@ class DebJuice < Minitest::Test
|
|
16
16
|
end
|
17
17
|
alias == eql?
|
18
18
|
|
19
|
-
end# Jam
|
19
|
+
end # Jam
|
20
20
|
|
21
21
|
# contributed by sauliusg to fix as_json
|
22
22
|
class Orange < Jam
|
23
|
-
def initialize(x, y)
|
24
|
-
super
|
25
|
-
end
|
26
23
|
|
27
|
-
def as_json
|
24
|
+
def as_json
|
28
25
|
{ :json_class => self.class,
|
29
26
|
:x => @x,
|
30
27
|
:y => @y }
|
31
28
|
end
|
32
29
|
|
33
30
|
def self.json_create(h)
|
34
|
-
|
31
|
+
new(h['x'], h['y'])
|
35
32
|
end
|
36
33
|
end
|
37
34
|
|
@@ -39,13 +36,13 @@ class DebJuice < Minitest::Test
|
|
39
36
|
Oj.default_options = { :mode => :compat, :class_cache => true, :use_as_json => true }
|
40
37
|
obj = Orange.new(true, 58)
|
41
38
|
json = Oj.dump(obj, :indent => 2)
|
42
|
-
|
39
|
+
refute_nil(json)
|
43
40
|
dump_and_load(obj, true)
|
44
41
|
end
|
45
42
|
|
46
|
-
def dump_and_load(obj,
|
43
|
+
def dump_and_load(obj, _trace=false)
|
47
44
|
json = Oj.dump(obj, :indent => 2)
|
48
|
-
loaded = Oj.load(json)
|
45
|
+
loaded = Oj.load(json)
|
49
46
|
assert_equal(obj, loaded)
|
50
47
|
loaded
|
51
48
|
end
|