oj 3.11.5 → 3.16.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1421 -0
  3. data/README.md +19 -5
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +20 -6
  6. data/ext/oj/cache.c +329 -0
  7. data/ext/oj/cache.h +22 -0
  8. data/ext/oj/cache8.c +10 -9
  9. data/ext/oj/circarray.c +8 -6
  10. data/ext/oj/circarray.h +2 -2
  11. data/ext/oj/code.c +19 -33
  12. data/ext/oj/code.h +2 -2
  13. data/ext/oj/compat.c +27 -77
  14. data/ext/oj/custom.c +86 -179
  15. data/ext/oj/debug.c +126 -0
  16. data/ext/oj/dump.c +256 -249
  17. data/ext/oj/dump.h +26 -12
  18. data/ext/oj/dump_compat.c +565 -642
  19. data/ext/oj/dump_leaf.c +17 -63
  20. data/ext/oj/dump_object.c +65 -187
  21. data/ext/oj/dump_strict.c +27 -51
  22. data/ext/oj/encoder.c +43 -0
  23. data/ext/oj/err.c +2 -13
  24. data/ext/oj/err.h +24 -8
  25. data/ext/oj/extconf.rb +21 -6
  26. data/ext/oj/fast.c +149 -149
  27. data/ext/oj/intern.c +313 -0
  28. data/ext/oj/intern.h +22 -0
  29. data/ext/oj/mem.c +318 -0
  30. data/ext/oj/mem.h +53 -0
  31. data/ext/oj/mimic_json.c +121 -106
  32. data/ext/oj/object.c +85 -162
  33. data/ext/oj/odd.c +89 -67
  34. data/ext/oj/odd.h +15 -15
  35. data/ext/oj/oj.c +542 -411
  36. data/ext/oj/oj.h +99 -73
  37. data/ext/oj/parse.c +175 -187
  38. data/ext/oj/parse.h +26 -24
  39. data/ext/oj/parser.c +1600 -0
  40. data/ext/oj/parser.h +101 -0
  41. data/ext/oj/rails.c +112 -159
  42. data/ext/oj/rails.h +1 -1
  43. data/ext/oj/reader.c +11 -14
  44. data/ext/oj/reader.h +4 -2
  45. data/ext/oj/resolve.c +5 -24
  46. data/ext/oj/rxclass.c +7 -6
  47. data/ext/oj/rxclass.h +1 -1
  48. data/ext/oj/saj.c +22 -33
  49. data/ext/oj/saj2.c +584 -0
  50. data/ext/oj/saj2.h +23 -0
  51. data/ext/oj/scp.c +5 -28
  52. data/ext/oj/sparse.c +28 -72
  53. data/ext/oj/stream_writer.c +50 -40
  54. data/ext/oj/strict.c +56 -61
  55. data/ext/oj/string_writer.c +72 -39
  56. data/ext/oj/trace.h +31 -4
  57. data/ext/oj/usual.c +1218 -0
  58. data/ext/oj/usual.h +69 -0
  59. data/ext/oj/util.h +1 -1
  60. data/ext/oj/val_stack.c +14 -3
  61. data/ext/oj/val_stack.h +8 -7
  62. data/ext/oj/validate.c +46 -0
  63. data/ext/oj/wab.c +63 -88
  64. data/lib/oj/active_support_helper.rb +1 -3
  65. data/lib/oj/bag.rb +7 -1
  66. data/lib/oj/easy_hash.rb +4 -5
  67. data/lib/oj/error.rb +1 -2
  68. data/lib/oj/json.rb +162 -150
  69. data/lib/oj/mimic.rb +9 -7
  70. data/lib/oj/saj.rb +20 -6
  71. data/lib/oj/schandler.rb +5 -4
  72. data/lib/oj/state.rb +12 -8
  73. data/lib/oj/version.rb +1 -2
  74. data/lib/oj.rb +2 -0
  75. data/pages/Compatibility.md +1 -1
  76. data/pages/InstallOptions.md +20 -0
  77. data/pages/JsonGem.md +15 -0
  78. data/pages/Modes.md +8 -3
  79. data/pages/Options.md +43 -5
  80. data/pages/Parser.md +309 -0
  81. data/pages/Rails.md +14 -2
  82. data/test/_test_active.rb +8 -9
  83. data/test/_test_active_mimic.rb +7 -8
  84. data/test/_test_mimic_rails.rb +17 -20
  85. data/test/activerecord/result_test.rb +5 -6
  86. data/test/activesupport6/encoding_test.rb +63 -28
  87. data/test/{activesupport5 → activesupport7}/abstract_unit.rb +16 -12
  88. data/test/{activesupport5 → activesupport7}/decoding_test.rb +2 -10
  89. data/test/{activesupport5 → activesupport7}/encoding_test.rb +86 -50
  90. data/test/{activesupport5 → activesupport7}/encoding_test_cases.rb +6 -0
  91. data/test/{activesupport5 → activesupport7}/time_zone_test_helpers.rb +8 -0
  92. data/test/files.rb +15 -15
  93. data/test/foo.rb +16 -45
  94. data/test/helper.rb +11 -8
  95. data/test/isolated/shared.rb +3 -2
  96. data/test/json_gem/json_addition_test.rb +2 -2
  97. data/test/json_gem/json_common_interface_test.rb +8 -6
  98. data/test/json_gem/json_encoding_test.rb +0 -0
  99. data/test/json_gem/json_ext_parser_test.rb +1 -0
  100. data/test/json_gem/json_fixtures_test.rb +3 -2
  101. data/test/json_gem/json_generator_test.rb +56 -38
  102. data/test/json_gem/json_generic_object_test.rb +11 -11
  103. data/test/json_gem/json_parser_test.rb +54 -47
  104. data/test/json_gem/json_string_matching_test.rb +9 -9
  105. data/test/json_gem/test_helper.rb +7 -3
  106. data/test/mem.rb +34 -0
  107. data/test/perf.rb +22 -27
  108. data/test/perf_compat.rb +31 -33
  109. data/test/perf_dump.rb +50 -0
  110. data/test/perf_fast.rb +80 -82
  111. data/test/perf_file.rb +27 -29
  112. data/test/perf_object.rb +65 -69
  113. data/test/perf_once.rb +59 -0
  114. data/test/perf_parser.rb +183 -0
  115. data/test/perf_saj.rb +46 -54
  116. data/test/perf_scp.rb +58 -69
  117. data/test/perf_simple.rb +41 -39
  118. data/test/perf_strict.rb +74 -82
  119. data/test/perf_wab.rb +67 -69
  120. data/test/prec.rb +5 -5
  121. data/test/sample/change.rb +0 -1
  122. data/test/sample/dir.rb +0 -1
  123. data/test/sample/doc.rb +0 -1
  124. data/test/sample/file.rb +0 -1
  125. data/test/sample/group.rb +0 -1
  126. data/test/sample/hasprops.rb +0 -1
  127. data/test/sample/layer.rb +0 -1
  128. data/test/sample/rect.rb +0 -1
  129. data/test/sample/shape.rb +0 -1
  130. data/test/sample/text.rb +0 -1
  131. data/test/sample.rb +16 -16
  132. data/test/sample_json.rb +8 -8
  133. data/test/test_compat.rb +95 -43
  134. data/test/test_custom.rb +73 -51
  135. data/test/test_debian.rb +7 -10
  136. data/test/test_fast.rb +135 -79
  137. data/test/test_file.rb +41 -30
  138. data/test/test_gc.rb +16 -5
  139. data/test/test_generate.rb +5 -5
  140. data/test/test_hash.rb +5 -5
  141. data/test/test_integer_range.rb +9 -9
  142. data/test/test_null.rb +20 -20
  143. data/test/test_object.rb +99 -96
  144. data/test/test_parser.rb +11 -0
  145. data/test/test_parser_debug.rb +27 -0
  146. data/test/test_parser_saj.rb +337 -0
  147. data/test/test_parser_usual.rb +251 -0
  148. data/test/test_rails.rb +2 -2
  149. data/test/test_saj.rb +10 -8
  150. data/test/test_scp.rb +37 -39
  151. data/test/test_strict.rb +40 -32
  152. data/test/test_various.rb +165 -84
  153. data/test/test_wab.rb +48 -44
  154. data/test/test_writer.rb +47 -47
  155. data/test/tests.rb +13 -5
  156. data/test/tests_mimic.rb +12 -3
  157. data/test/tests_mimic_addition.rb +12 -3
  158. metadata +74 -128
  159. data/ext/oj/hash.c +0 -131
  160. data/ext/oj/hash.h +0 -19
  161. data/ext/oj/hash_test.c +0 -491
  162. data/test/activesupport4/decoding_test.rb +0 -108
  163. data/test/activesupport4/encoding_test.rb +0 -531
  164. data/test/activesupport4/test_helper.rb +0 -41
  165. data/test/activesupport5/test_helper.rb +0 -72
  166. data/test/bar.rb +0 -35
  167. data/test/baz.rb +0 -16
  168. data/test/zoo.rb +0 -13
data/test/test_compat.rb CHANGED
@@ -1,10 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
- # encoding: utf-8
2
+ # frozen_string_literal: true
3
3
 
4
- $: << File.dirname(__FILE__)
5
- $oj_dir = File.dirname(File.expand_path(File.dirname(__FILE__)))
4
+ $LOAD_PATH << __dir__
5
+ @oj_dir = File.dirname(File.expand_path(__dir__))
6
6
  %w(lib ext).each do |dir|
7
- $: << File.join($oj_dir, dir)
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(*a)
33
- %|{"json_class":"#{self.class.to_s}","x":#{@x},"y":#{@y}}|
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
- self.new(h['x'], h['y'])
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(*a)
65
+ def to_json(*_a)
73
66
  %|{"json_class":"#{self.class.name}"}|
74
67
  end
75
68
 
76
- def self.json_create(h)
77
- self.new()
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(12345, false)
110
- dump_and_load(-54321, false)
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(12345.6789, false)
117
+ dump_and_load(12_345.6789, false)
119
118
  dump_and_load(70.35, false)
120
- dump_and_load(-54321.012, false)
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(1405460727.723866, false)
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("2012-01-05T23:58:07.123456000+09:00")
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({ "1" => true, "0" => false }, h)
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({"subtext" => "\"404er” w k 3 a"}, obj)
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(json, '{"<>":"<>"}')
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(30226801971775055948247051683954096612865741943, b)
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({"year" => orig.year, "month" => orig.month, "day" => orig.day, "start" => orig.start}, x)
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(File.dirname(__FILE__), 'open_file_test.json')
344
- File.open(filename, 'w') { |f| f.write(%{{
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 = { "child" => Jeez.new(true, 58) }
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("ArgumentError", e.class().name)
434
+ assert_equal('ArgumentError', e.class().name)
435
435
  return
436
436
  end
437
- assert(false, "*** expected an exception")
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 => "_class_", :create_additions => true)
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
- # encoding: utf-8
2
+ # frozen_string_literal: true
3
3
 
4
- $: << File.dirname(__FILE__)
5
- $oj_dir = File.dirname(File.expand_path(File.dirname(__FILE__)))
4
+ $LOAD_PATH << __dir__
5
+ @oj_dir = File.dirname(File.expand_path(__dir__))
6
6
  %w(lib ext).each do |dir|
7
- $: << File.join($oj_dir, dir)
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
- def to_json(*args)
34
+
35
+ def to_json(*_args)
34
36
  %|{"xx":#{@x},"yy":#{y}}|
35
37
  end
36
- def raw_json(depth, indent)
38
+
39
+ def raw_json(_depth, _indent)
37
40
  %|{"xxx":#{@x},"yyy":#{y}}|
38
41
  end
39
- def as_json(*args)
42
+
43
+ def as_json(*_args)
40
44
  {'a' => @x, :b => @y }
41
45
  end
42
- def to_hash()
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
- def as_json(*args)
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
- def as_json(*args)
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(12345, false)
105
- dump_and_load(-54321, false)
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(12345.6789, false)
120
+ dump_and_load(12_345.6789, false)
112
121
  dump_and_load(70.35, false)
113
- dump_and_load(-54321.012, false)
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("12.123456789012345678", mode: :custom, bigdecimal_load: :float);
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("12.123456789012345678", mode: :custom, bigdecimal_load: :fast);
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, "*** expected an exception")
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, "*** expected an exception")
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, "*** expected an exception")
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("ぴーたー", :escape_mode => :ascii)
188
+ json = Oj.dump('ぴーたー', :escape_mode => :ascii)
180
189
  assert_equal(%{"\\u3074\\u30fc\\u305f\\u30fc"}, json)
181
- dump_and_load("ぴーたー", false, :escape_mode => :ascii)
190
+ dump_and_load('ぴーたー', false, :escape_mode => :ascii)
182
191
  end
183
192
 
184
193
  def test_string_json
185
- json = Oj.dump("ぴーたー", :escape_mode => :json)
194
+ json = Oj.dump('ぴーたー', :escape_mode => :json)
186
195
  assert_equal(%{"ぴーたー"}, json)
187
- dump_and_load("ぴーたー", false, :escape_mode => :json)
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 = 10000
205
- Oj.strict_load('[' * n + ']' * n)
215
+ n = 10_000
216
+ Oj.strict_load(('[' * n) + (']' * n))
206
217
  rescue Exception => e
207
- assert(false, e.message)
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: "^o", use_to_json: false, use_as_json: false, use_to_hash: false)
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: "^o", use_to_json: false, use_as_json: false, use_to_hash: false, ignore_under: true)
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 => "^o", :create_additions => true)
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 => "^o", :create_additions => true)
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 => "^o", :create_additions => true)
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 => "^o", :create_additions => true)
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 => "^o", :create_additions => true)
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 => "^o", :create_additions => true)
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
- # older versions seems to have issues getting the utc offset.
452
- if '2.4' <= RUBY_VERSION
453
- obj = DateTime.new(2017, 1, 5, 10, 20, 30, '-0500')
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 => "^o", :create_additions => true)
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 => "^o", :create_additions => true)
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
- dump_load_dump(obj, false, :time_format => :unix, :create_id => "^o", :create_additions => true)
485
- dump_load_dump(obj, false, :time_format => :unix_zone, :create_id => "^o", :create_additions => true)
486
- dump_load_dump(obj, false, :time_format => :xmlschema, :create_id => "^o", :create_additions => true)
487
- dump_load_dump(obj, false, :time_format => :ruby, :create_id => "^o", :create_additions => true)
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
- # encoding: UTF-8
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
- self.new(h['x'], h['y'])
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
- assert(!json.nil?)
39
+ refute_nil(json)
43
40
  dump_and_load(obj, true)
44
41
  end
45
42
 
46
- def dump_and_load(obj, trace=false)
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