oj 3.16.3 → 3.17.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +99 -0
  3. data/README.md +0 -16
  4. data/ext/oj/compat.c +3 -3
  5. data/ext/oj/custom.c +17 -11
  6. data/ext/oj/dump.c +619 -88
  7. data/ext/oj/dump.h +9 -2
  8. data/ext/oj/dump_compat.c +16 -9
  9. data/ext/oj/dump_leaf.c +1 -1
  10. data/ext/oj/dump_object.c +52 -27
  11. data/ext/oj/dump_strict.c +20 -14
  12. data/ext/oj/extconf.rb +11 -9
  13. data/ext/oj/fast.c +90 -65
  14. data/ext/oj/intern.c +1 -1
  15. data/ext/oj/mimic_json.c +14 -8
  16. data/ext/oj/object.c +18 -12
  17. data/ext/oj/oj.c +426 -11
  18. data/ext/oj/oj.h +60 -52
  19. data/ext/oj/parse.c +252 -48
  20. data/ext/oj/parse.h +0 -1
  21. data/ext/oj/parser.c +179 -47
  22. data/ext/oj/parser.h +4 -2
  23. data/ext/oj/rails.c +57 -36
  24. data/ext/oj/reader.c +17 -5
  25. data/ext/oj/rxclass.c +17 -1
  26. data/ext/oj/rxclass.h +2 -1
  27. data/ext/oj/safe.c +230 -0
  28. data/ext/oj/safe.h +79 -0
  29. data/ext/oj/saj.c +55 -20
  30. data/ext/oj/scp.c +3 -6
  31. data/ext/oj/simd.h +219 -0
  32. data/ext/oj/sparse.c +15 -3
  33. data/ext/oj/stream_writer.c +19 -14
  34. data/ext/oj/strict.c +2 -4
  35. data/ext/oj/string_writer.c +26 -14
  36. data/ext/oj/usual.c +37 -35
  37. data/ext/oj/wab.c +5 -4
  38. data/lib/oj/mimic.rb +1 -5
  39. data/lib/oj/schandler.rb +5 -4
  40. data/lib/oj/version.rb +1 -1
  41. data/pages/Encoding.md +1 -1
  42. metadata +22 -100
  43. data/test/_test_active.rb +0 -75
  44. data/test/_test_active_mimic.rb +0 -95
  45. data/test/_test_mimic_rails.rb +0 -123
  46. data/test/activerecord/result_test.rb +0 -31
  47. data/test/activesupport6/abstract_unit.rb +0 -44
  48. data/test/activesupport6/decoding_test.rb +0 -133
  49. data/test/activesupport6/encoding_test.rb +0 -507
  50. data/test/activesupport6/encoding_test_cases.rb +0 -98
  51. data/test/activesupport6/test_common.rb +0 -17
  52. data/test/activesupport6/test_helper.rb +0 -163
  53. data/test/activesupport6/time_zone_test_helpers.rb +0 -39
  54. data/test/activesupport7/abstract_unit.rb +0 -49
  55. data/test/activesupport7/decoding_test.rb +0 -125
  56. data/test/activesupport7/encoding_test.rb +0 -486
  57. data/test/activesupport7/encoding_test_cases.rb +0 -104
  58. data/test/activesupport7/time_zone_test_helpers.rb +0 -47
  59. data/test/files.rb +0 -29
  60. data/test/foo.rb +0 -14
  61. data/test/helper.rb +0 -39
  62. data/test/isolated/shared.rb +0 -309
  63. data/test/isolated/test_mimic_after.rb +0 -13
  64. data/test/isolated/test_mimic_alone.rb +0 -12
  65. data/test/isolated/test_mimic_as_json.rb +0 -45
  66. data/test/isolated/test_mimic_before.rb +0 -13
  67. data/test/isolated/test_mimic_define.rb +0 -28
  68. data/test/isolated/test_mimic_rails_after.rb +0 -22
  69. data/test/isolated/test_mimic_rails_before.rb +0 -21
  70. data/test/isolated/test_mimic_redefine.rb +0 -15
  71. data/test/json_gem/json_addition_test.rb +0 -216
  72. data/test/json_gem/json_common_interface_test.rb +0 -155
  73. data/test/json_gem/json_encoding_test.rb +0 -107
  74. data/test/json_gem/json_ext_parser_test.rb +0 -21
  75. data/test/json_gem/json_fixtures_test.rb +0 -36
  76. data/test/json_gem/json_generator_test.rb +0 -413
  77. data/test/json_gem/json_generic_object_test.rb +0 -90
  78. data/test/json_gem/json_parser_test.rb +0 -477
  79. data/test/json_gem/json_string_matching_test.rb +0 -42
  80. data/test/json_gem/test_helper.rb +0 -30
  81. data/test/mem.rb +0 -34
  82. data/test/perf.rb +0 -102
  83. data/test/perf_compat.rb +0 -128
  84. data/test/perf_dump.rb +0 -50
  85. data/test/perf_fast.rb +0 -162
  86. data/test/perf_file.rb +0 -62
  87. data/test/perf_object.rb +0 -134
  88. data/test/perf_once.rb +0 -59
  89. data/test/perf_parser.rb +0 -183
  90. data/test/perf_saj.rb +0 -101
  91. data/test/perf_scp.rb +0 -140
  92. data/test/perf_simple.rb +0 -289
  93. data/test/perf_strict.rb +0 -137
  94. data/test/perf_wab.rb +0 -129
  95. data/test/prec.rb +0 -23
  96. data/test/sample/change.rb +0 -13
  97. data/test/sample/dir.rb +0 -18
  98. data/test/sample/doc.rb +0 -35
  99. data/test/sample/file.rb +0 -47
  100. data/test/sample/group.rb +0 -15
  101. data/test/sample/hasprops.rb +0 -15
  102. data/test/sample/layer.rb +0 -11
  103. data/test/sample/line.rb +0 -20
  104. data/test/sample/oval.rb +0 -10
  105. data/test/sample/rect.rb +0 -9
  106. data/test/sample/shape.rb +0 -34
  107. data/test/sample/text.rb +0 -19
  108. data/test/sample.rb +0 -54
  109. data/test/sample_json.rb +0 -37
  110. data/test/test_compat.rb +0 -567
  111. data/test/test_custom.rb +0 -555
  112. data/test/test_debian.rb +0 -50
  113. data/test/test_fast.rb +0 -526
  114. data/test/test_file.rb +0 -250
  115. data/test/test_gc.rb +0 -60
  116. data/test/test_generate.rb +0 -21
  117. data/test/test_hash.rb +0 -39
  118. data/test/test_integer_range.rb +0 -72
  119. data/test/test_null.rb +0 -376
  120. data/test/test_object.rb +0 -1030
  121. data/test/test_parser.rb +0 -11
  122. data/test/test_parser_debug.rb +0 -27
  123. data/test/test_parser_saj.rb +0 -337
  124. data/test/test_parser_usual.rb +0 -227
  125. data/test/test_rails.rb +0 -35
  126. data/test/test_saj.rb +0 -188
  127. data/test/test_scp.rb +0 -431
  128. data/test/test_strict.rb +0 -441
  129. data/test/test_various.rb +0 -801
  130. data/test/test_wab.rb +0 -311
  131. data/test/test_writer.rb +0 -380
  132. data/test/tests.rb +0 -33
  133. data/test/tests_mimic.rb +0 -23
  134. data/test/tests_mimic_addition.rb +0 -16
data/test/test_parser.rb DELETED
@@ -1,11 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- $LOAD_PATH << __dir__
5
- @oj_dir = File.dirname(File.expand_path(__dir__))
6
- %w(lib ext).each do |dir|
7
- $LOAD_PATH << File.join(@oj_dir, dir)
8
- end
9
-
10
- require 'test_parser_usual'
11
- require 'test_parser_saj'
@@ -1,27 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- $LOAD_PATH << __dir__
5
- @oj_dir = File.dirname(File.expand_path(__dir__))
6
- %w[lib ext].each do |dir|
7
- $LOAD_PATH << File.join(@oj_dir, dir)
8
- end
9
-
10
- require 'minitest'
11
- require 'minitest/autorun'
12
- require 'stringio'
13
- require 'date'
14
- require 'bigdecimal'
15
- require 'oj'
16
-
17
- class ParserJuice < Minitest::Test
18
-
19
- def test_array
20
- p = Oj::Parser.new(:debug)
21
- out = p.parse(%|[true, false, null, 123, -1.23, "abc"]|)
22
- puts out
23
- out = p.parse(%|{"abc": []}|)
24
- puts out
25
- end
26
-
27
- end
@@ -1,337 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- $LOAD_PATH << __dir__
5
-
6
- require 'helper'
7
-
8
- $json = %|{
9
- "array": [
10
- {
11
- "num" : 3,
12
- "string": "message",
13
- "hash" : {
14
- "h2" : {
15
- "a" : [ 1, 2, 3 ]
16
- }
17
- }
18
- }
19
- ],
20
- "boolean" : true
21
- }|
22
-
23
- class AllSaj < Oj::Saj
24
- attr_accessor :calls
25
-
26
- def initialize
27
- @calls = []
28
-
29
- super
30
- end
31
-
32
- def hash_start(key)
33
- @calls << [:hash_start, key]
34
- end
35
-
36
- def hash_end(key)
37
- @calls << [:hash_end, key]
38
- end
39
-
40
- def array_start(key)
41
- @calls << [:array_start, key]
42
- end
43
-
44
- def array_end(key)
45
- @calls << [:array_end, key]
46
- end
47
-
48
- def add_value(value, key)
49
- @calls << [:add_value, value, key]
50
- end
51
-
52
- def error(message, line, column)
53
- @calls << [:error, message, line, column]
54
- end
55
-
56
- end # AllSaj
57
-
58
- class LocSaj
59
- attr_accessor :calls
60
-
61
- def initialize
62
- @calls = []
63
- end
64
-
65
- def hash_start(key, line, column)
66
- @calls << [:hash_start, key, line, column]
67
- end
68
-
69
- def hash_end(key, line, column)
70
- @calls << [:hash_end, key, line, column]
71
- end
72
-
73
- def array_start(key, line, column)
74
- @calls << [:array_start, key, line, column]
75
- end
76
-
77
- def array_end(key, line, column)
78
- @calls << [:array_end, key, line, column]
79
- end
80
-
81
- def add_value(value, key, line, column)
82
- @calls << [:add_value, value, key, line, column]
83
- end
84
-
85
- end # LocSaj
86
-
87
- class SajTest < Minitest::Test
88
-
89
- def test_nil
90
- handler = AllSaj.new()
91
- json = %{null}
92
- p = Oj::Parser.new(:saj)
93
- p.handler = handler
94
- p.parse(json)
95
- assert_equal([[:add_value, nil, nil]], handler.calls)
96
- end
97
-
98
- def test_true
99
- handler = AllSaj.new()
100
- json = %{true}
101
- p = Oj::Parser.new(:saj)
102
- p.handler = handler
103
- p.parse(json)
104
- assert_equal([[:add_value, true, nil]], handler.calls)
105
- end
106
-
107
- def test_false
108
- handler = AllSaj.new()
109
- json = %{false}
110
- p = Oj::Parser.new(:saj)
111
- p.handler = handler
112
- p.parse(json)
113
- assert_equal([[:add_value, false, nil]], handler.calls)
114
- end
115
-
116
- def test_string
117
- handler = AllSaj.new()
118
- json = %{"a string"}
119
- p = Oj::Parser.new(:saj)
120
- p.handler = handler
121
- p.parse(json)
122
- assert_equal([[:add_value, 'a string', nil]], handler.calls)
123
- end
124
-
125
- def test_fixnum
126
- handler = AllSaj.new()
127
- json = %{12345}
128
- p = Oj::Parser.new(:saj)
129
- p.handler = handler
130
- p.parse(json)
131
- assert_equal([[:add_value, 12_345, nil]], handler.calls)
132
- end
133
-
134
- def test_float
135
- handler = AllSaj.new()
136
- json = %{12345.6789}
137
- p = Oj::Parser.new(:saj)
138
- p.handler = handler
139
- p.parse(json)
140
- assert_equal([[:add_value, 12_345.6789, nil]], handler.calls)
141
- end
142
-
143
- def test_float_exp
144
- handler = AllSaj.new()
145
- json = %{12345.6789e7}
146
- p = Oj::Parser.new(:saj)
147
- p.handler = handler
148
- p.parse(json)
149
- assert_equal(1, handler.calls.size)
150
- assert_equal(:add_value, handler.calls[0][0])
151
- assert_equal((12_345.6789e7 * 10_000).to_i, (handler.calls[0][1] * 10_000).to_i)
152
- end
153
-
154
- def test_bignum
155
- handler = AllSaj.new()
156
- json = %{-11.899999999999999}
157
- p = Oj::Parser.new(:saj)
158
- p.handler = handler
159
- p.parse(json)
160
- assert_equal(1, handler.calls.size)
161
- assert_equal(:add_value, handler.calls[0][0])
162
- assert_equal(-118_999, (handler.calls[0][1] * 10_000).to_i)
163
- end
164
-
165
- def test_bignum_loc
166
- handler = LocSaj.new()
167
- json = <<~JSON
168
- {
169
- "width": 192.33800000000002,
170
- "xaxis": {
171
- "anchor": "y"
172
- }
173
- }
174
- JSON
175
-
176
- p = Oj::Parser.new(:saj)
177
- p.handler = handler
178
- p.parse(json)
179
- assert_equal(6, handler.calls.size)
180
- assert_equal(1_923_380, (handler.calls[1][1] * 10_000).to_i)
181
- handler.calls[1][1] = 1_923_380
182
- assert_equal([[:hash_start, nil, 1, 1],
183
- [:add_value, 1_923_380, 'width', 2, 30],
184
- [:hash_start, 'xaxis', 3, 12],
185
- [:add_value, 'y', 'anchor', 4, 17],
186
- [:hash_end, 'xaxis', 5, 3],
187
- [:hash_end, nil, 6, 1]],
188
- handler.calls)
189
- end
190
-
191
- def test_array_empty
192
- handler = AllSaj.new()
193
- json = %{[]}
194
- p = Oj::Parser.new(:saj)
195
- p.handler = handler
196
- p.parse(json)
197
- assert_equal([[:array_start, nil],
198
- [:array_end, nil]], handler.calls)
199
- end
200
-
201
- def test_array
202
- handler = AllSaj.new()
203
- json = %{[true,false]}
204
- p = Oj::Parser.new(:saj)
205
- p.handler = handler
206
- p.parse(json)
207
- assert_equal([[:array_start, nil],
208
- [:add_value, true, nil],
209
- [:add_value, false, nil],
210
- [:array_end, nil]], handler.calls)
211
- end
212
-
213
- def test_hash_empty
214
- handler = AllSaj.new()
215
- json = %{{}}
216
- p = Oj::Parser.new(:saj)
217
- p.handler = handler
218
- p.parse(json)
219
- assert_equal([[:hash_start, nil],
220
- [:hash_end, nil]], handler.calls)
221
- end
222
-
223
- def test_hash
224
- handler = AllSaj.new()
225
- json = %{{"one":true,"two":false}}
226
- p = Oj::Parser.new(:saj)
227
- p.handler = handler
228
- p.parse(json)
229
- assert_equal([[:hash_start, nil],
230
- [:add_value, true, 'one'],
231
- [:add_value, false, 'two'],
232
- [:hash_end, nil]], handler.calls)
233
- end
234
-
235
- def test_full
236
- handler = AllSaj.new()
237
- Oj.saj_parse(handler, $json)
238
- assert_equal([[:hash_start, nil],
239
- [:array_start, 'array'],
240
- [:hash_start, nil],
241
- [:add_value, 3, 'num'],
242
- [:add_value, 'message', 'string'],
243
- [:hash_start, 'hash'],
244
- [:hash_start, 'h2'],
245
- [:array_start, 'a'],
246
- [:add_value, 1, nil],
247
- [:add_value, 2, nil],
248
- [:add_value, 3, nil],
249
- [:array_end, 'a'],
250
- [:hash_end, 'h2'],
251
- [:hash_end, 'hash'],
252
- [:hash_end, nil],
253
- [:array_end, 'array'],
254
- [:add_value, true, 'boolean'],
255
- [:hash_end, nil]], handler.calls)
256
- end
257
-
258
- def test_multiple
259
- handler = AllSaj.new()
260
- json = %|[true][false]|
261
- p = Oj::Parser.new(:saj)
262
- p.handler = handler
263
- p.parse(json)
264
- assert_equal([
265
- [:array_start, nil],
266
- [:add_value, true, nil],
267
- [:array_end, nil],
268
- [:array_start, nil],
269
- [:add_value, false, nil],
270
- [:array_end, nil],
271
- ], handler.calls)
272
- end
273
-
274
- def test_io
275
- handler = AllSaj.new()
276
- json = %| [true,false] |
277
- p = Oj::Parser.new(:saj)
278
- p.handler = handler
279
- p.load(StringIO.new(json))
280
- assert_equal([
281
- [:array_start, nil],
282
- [:add_value, true, nil],
283
- [:add_value, false, nil],
284
- [:array_end, nil],
285
- ], handler.calls)
286
- end
287
-
288
- def test_file
289
- handler = AllSaj.new()
290
- p = Oj::Parser.new(:saj)
291
- p.handler = handler
292
- p.file('saj_test.json')
293
- assert_equal([
294
- [:array_start, nil],
295
- [:add_value, true, nil],
296
- [:add_value, false, nil],
297
- [:array_end, nil],
298
- ], handler.calls)
299
- end
300
-
301
- def test_default
302
- handler = AllSaj.new()
303
- json = %|[true]|
304
- Oj::Parser.saj.handler = handler
305
- Oj::Parser.saj.parse(json)
306
- assert_equal([
307
- [:array_start, nil],
308
- [:add_value, true, nil],
309
- [:array_end, nil],
310
- ], handler.calls)
311
- end
312
-
313
- def test_loc
314
- handler = LocSaj.new()
315
- Oj::Parser.saj.handler = handler
316
- Oj::Parser.saj.parse($json)
317
- assert_equal([[:hash_start, nil, 1, 1],
318
- [:array_start, 'array', 2, 12],
319
- [:hash_start, nil, 3, 5],
320
- [:add_value, 3, 'num', 4, 18],
321
- [:add_value, 'message', 'string', 5, 25],
322
- [:hash_start, 'hash', 6, 17],
323
- [:hash_start, 'h2', 7, 17],
324
- [:array_start, 'a', 8, 17],
325
- [:add_value, 1, nil, 8, 20],
326
- [:add_value, 2, nil, 8, 23],
327
- [:add_value, 3, nil, 8, 26],
328
- [:array_end, 'a', 8, 27],
329
- [:hash_end, 'h2', 9, 9],
330
- [:hash_end, 'hash', 10, 7],
331
- [:hash_end, nil, 11, 5],
332
- [:array_end, 'array', 12, 3],
333
- [:add_value, true, 'boolean', 13, 18],
334
- [:hash_end, nil, 14, 1]], handler.calls)
335
- end
336
-
337
- end
@@ -1,227 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- $LOAD_PATH << __dir__
5
-
6
- require 'helper'
7
-
8
- class UsualTest < Minitest::Test
9
-
10
- def test_nil
11
- p = Oj::Parser.new(:usual)
12
- doc = p.parse('nil')
13
- assert_nil(doc)
14
- end
15
-
16
- def test_primitive
17
- p = Oj::Parser.new(:usual)
18
- [
19
- ['true', true],
20
- ['false', false],
21
- ['123', 123],
22
- ['1.25', 1.25],
23
- ['"abc"', 'abc'],
24
- ].each { |x|
25
- doc = p.parse(x[0])
26
- assert_equal(x[1], doc)
27
- }
28
- end
29
-
30
- def test_big
31
- p = Oj::Parser.new(:usual)
32
- doc = p.parse('12345678901234567890123456789')
33
- assert_equal(BigDecimal, doc.class)
34
- doc = p.parse('1234567890.1234567890123456789')
35
- assert_equal(BigDecimal, doc.class)
36
- end
37
-
38
- def test_array
39
- p = Oj::Parser.new(:usual)
40
- [
41
- ['[]', []],
42
- ['[false]', [false]],
43
- ['[true,false]', [true, false]],
44
- ['[[]]', [[]]],
45
- ['[true,[],false]', [true, [], false]],
46
- ['[true,[true],false]', [true, [true], false]],
47
- ].each { |x|
48
- doc = p.parse(x[0])
49
- assert_equal(x[1], doc)
50
- }
51
- end
52
-
53
- def test_hash
54
- p = Oj::Parser.new(:usual)
55
- [
56
- ['{}', {}],
57
- ['{"a": null}', {'a' => nil}],
58
- ['{"t": true, "f": false, "s": "abc"}', {'t' => true, 'f' => false, 's' => 'abc'}],
59
- ['{"a": {}}', {'a' => {}}],
60
- ['{"a": {"b": 2}}', {'a' => {'b' => 2}}],
61
- ['{"a": [true]}', {'a' => [true]}],
62
- ].each { |x|
63
- doc = p.parse(x[0])
64
- assert_equal(x[1], doc)
65
- }
66
- end
67
-
68
- def test_symbol_keys
69
- p = Oj::Parser.new(:usual)
70
- refute(p.symbol_keys)
71
- p.symbol_keys = true
72
- doc = p.parse('{"a": true, "b": false}')
73
- assert_equal({a: true, b: false}, doc)
74
- end
75
-
76
- def test_strings
77
- p = Oj::Parser.new(:usual)
78
- doc = p.parse('{"ぴ": "", "ぴ ": "x", "c": "ぴーたー", "d": " ぴーたー "}')
79
- assert_equal({'ぴ' => '', 'ぴ ' => 'x', 'c' => 'ぴーたー', 'd' => ' ぴーたー '}, doc)
80
- end
81
-
82
- def test_capacity
83
- p = Oj::Parser.new(:usual, capacity: 1000)
84
- assert_equal(4096, p.capacity)
85
- p.capacity = 5000
86
- assert_equal(5000, p.capacity)
87
- end
88
-
89
- def test_decimal
90
- p = Oj::Parser.new(:usual)
91
- assert_equal(:auto, p.decimal)
92
- doc = p.parse('1.234567890123456789')
93
- assert_equal(BigDecimal, doc.class)
94
- assert_equal('0.1234567890123456789e1', doc.to_s)
95
- doc = p.parse('1.25')
96
- assert_equal(Float, doc.class)
97
-
98
- p.decimal = :float
99
- assert_equal(:float, p.decimal)
100
- doc = p.parse('1.234567890123456789')
101
- assert_equal(Float, doc.class)
102
-
103
- p.decimal = :bigdecimal
104
- assert_equal(:bigdecimal, p.decimal)
105
- doc = p.parse('1.234567890123456789')
106
- assert_equal(BigDecimal, doc.class)
107
- doc = p.parse('1.25')
108
- assert_equal(BigDecimal, doc.class)
109
- assert_equal('0.125e1', doc.to_s)
110
-
111
- p.decimal = :ruby
112
- assert_equal(:ruby, p.decimal)
113
- doc = p.parse('1.234567890123456789')
114
- assert_equal(Float, doc.class)
115
- end
116
-
117
- def test_omit_null
118
- p = Oj::Parser.new(:usual)
119
- p.omit_null = true
120
- doc = p.parse('{"a":true,"b":null}')
121
- assert_equal({'a'=>true}, doc)
122
-
123
- p.omit_null = false
124
- doc = p.parse('{"a":true,"b":null}')
125
- assert_equal({'a'=>true, 'b'=>nil}, doc)
126
- end
127
-
128
- class MyArray < Array
129
- end
130
-
131
- def test_array_class
132
- p = Oj::Parser.new(:usual)
133
- p.array_class = MyArray
134
- assert_equal(MyArray, p.array_class)
135
- doc = p.parse('[true]')
136
- assert_equal(MyArray, doc.class)
137
- end
138
-
139
- class MyHash < Hash
140
- end
141
-
142
- def test_hash_class
143
- p = Oj::Parser.new(:usual)
144
- p.hash_class = MyHash
145
- assert_equal(MyHash, p.hash_class)
146
- doc = p.parse('{"a":true}')
147
- assert_equal(MyHash, doc.class)
148
- end
149
-
150
- def test_empty
151
- p = Oj::Parser.new(:usual)
152
- p.raise_on_empty = false
153
- doc = p.parse(' ')
154
- assert_nil(doc)
155
-
156
- p.raise_on_empty = true
157
- assert_raises(Oj::ParseError) { p.parse(' ') }
158
- end
159
-
160
- class MyClass
161
- attr_accessor :a
162
- attr_accessor :b
163
-
164
- def to_s
165
- "#{self.class}{a: #{@a} b: #{b}}"
166
- end
167
- end
168
-
169
- class MyClass2 < MyClass
170
- def self.json_create(arg)
171
- obj = new
172
- obj.a = arg['a']
173
- obj.b = arg['b']
174
- obj
175
- end
176
- end
177
-
178
- def test_create_id
179
- p = Oj::Parser.new(:usual)
180
- p.create_id = '^'
181
- doc = p.parse('{"a":true}')
182
- assert_equal(Hash, doc.class)
183
- doc = p.parse('{"a":true,"^":"UsualTest::MyClass","b":false}')
184
- assert_equal('UsualTest::MyClass{a: true b: false}', doc.to_s)
185
-
186
- doc = p.parse('{"a":true,"^":"UsualTest::MyClass2","b":false}')
187
- assert_equal('UsualTest::MyClass2{a: true b: false}', doc.to_s)
188
-
189
- p.hash_class = MyHash
190
- assert_equal(MyHash, p.hash_class)
191
- doc = p.parse('{"a":true}')
192
- assert_equal(MyHash, doc.class)
193
-
194
- doc = p.parse('{"a":true,"^":"UsualTest::MyClass","b":false}')
195
- assert_equal('UsualTest::MyClass{a: true b: false}', doc.to_s)
196
- end
197
-
198
- def test_missing_class
199
- p = Oj::Parser.new(:usual, create_id: '^')
200
- json = '{"a":true,"^":"Auto","b":false}'
201
- doc = p.parse(json)
202
- assert_equal(Hash, doc.class)
203
-
204
- p.missing_class = :auto
205
- doc = p.parse(json)
206
- # Auto should be defined after parsing
207
- assert_equal(Auto, doc.class)
208
- end
209
-
210
- def test_class_cache
211
- p = Oj::Parser.new(:usual)
212
- p.create_id = '^'
213
- p.class_cache = true
214
- p.missing_class = :auto
215
- json = '{"a":true,"^":"Auto2","b":false}'
216
- doc = p.parse(json)
217
- assert_equal(Auto2, doc.class)
218
-
219
- doc = p.parse(json)
220
- assert_equal(Auto2, doc.class)
221
- end
222
-
223
- def test_default_parser
224
- doc = Oj::Parser.usual.parse('{"a":true,"b":null}')
225
- assert_equal({'a'=>true, 'b'=>nil}, doc)
226
- end
227
- end
data/test/test_rails.rb DELETED
@@ -1,35 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- $LOAD_PATH << __dir__
5
-
6
- require 'helper'
7
-
8
- class RailsJuice < Minitest::Test
9
-
10
- def test_bigdecimal_dump
11
- orig = Oj.default_options
12
- Oj.default_options = { mode: :rails, bigdecimal_as_decimal: true }
13
- bd = BigDecimal('123')
14
- json = Oj.dump(bd)
15
- Oj.default_options = orig
16
-
17
- assert_equal('0.123e3', json.downcase)
18
-
19
- json = Oj.dump(bd, mode: :rails, bigdecimal_as_decimal: false)
20
- assert_equal('"0.123e3"', json.downcase)
21
-
22
- json = Oj.dump(bd, mode: :rails, bigdecimal_as_decimal: true)
23
- assert_equal('0.123e3', json.downcase)
24
- end
25
-
26
- def test_invalid_encoding
27
- assert_raises(EncodingError) {
28
- Oj.dump("\"\xf3j", mode: :rails)
29
- }
30
- assert_raises(EncodingError) {
31
- Oj.dump("\xf3j", mode: :rails)
32
- }
33
- end
34
-
35
- end