oj 3.7.4 → 3.13.21
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1352 -0
- data/README.md +29 -8
- data/RELEASE_NOTES.md +61 -0
- data/ext/oj/buf.h +53 -72
- data/ext/oj/cache.c +326 -0
- data/ext/oj/cache.h +21 -0
- data/ext/oj/cache8.c +61 -64
- data/ext/oj/cache8.h +12 -39
- data/ext/oj/circarray.c +37 -43
- data/ext/oj/circarray.h +16 -17
- data/ext/oj/code.c +165 -179
- data/ext/oj/code.h +27 -29
- data/ext/oj/compat.c +174 -194
- data/ext/oj/custom.c +809 -866
- data/ext/oj/debug.c +132 -0
- data/ext/oj/dump.c +848 -863
- data/ext/oj/dump.h +81 -67
- data/ext/oj/dump_compat.c +85 -123
- data/ext/oj/dump_leaf.c +100 -188
- data/ext/oj/dump_object.c +527 -656
- data/ext/oj/dump_strict.c +315 -338
- data/ext/oj/encode.h +7 -34
- data/ext/oj/encoder.c +43 -0
- data/ext/oj/err.c +40 -29
- data/ext/oj/err.h +48 -48
- data/ext/oj/extconf.rb +17 -4
- data/ext/oj/fast.c +1070 -1087
- data/ext/oj/intern.c +301 -0
- data/ext/oj/intern.h +26 -0
- data/ext/oj/mimic_json.c +469 -436
- data/ext/oj/object.c +525 -593
- data/ext/oj/odd.c +154 -138
- data/ext/oj/odd.h +37 -38
- data/ext/oj/oj.c +1325 -986
- data/ext/oj/oj.h +333 -316
- data/ext/oj/parse.c +1002 -846
- data/ext/oj/parse.h +92 -87
- data/ext/oj/parser.c +1557 -0
- data/ext/oj/parser.h +91 -0
- data/ext/oj/rails.c +888 -878
- data/ext/oj/rails.h +11 -14
- data/ext/oj/reader.c +141 -147
- data/ext/oj/reader.h +73 -89
- data/ext/oj/resolve.c +41 -62
- data/ext/oj/resolve.h +7 -9
- data/ext/oj/rxclass.c +71 -75
- data/ext/oj/rxclass.h +18 -19
- data/ext/oj/saj.c +443 -486
- data/ext/oj/saj2.c +602 -0
- data/ext/oj/scp.c +88 -113
- data/ext/oj/sparse.c +787 -709
- data/ext/oj/stream_writer.c +133 -159
- data/ext/oj/strict.c +127 -118
- data/ext/oj/string_writer.c +230 -249
- data/ext/oj/trace.c +34 -41
- data/ext/oj/trace.h +19 -19
- data/ext/oj/usual.c +1254 -0
- data/ext/oj/util.c +136 -0
- data/ext/oj/util.h +20 -0
- data/ext/oj/val_stack.c +59 -67
- data/ext/oj/val_stack.h +91 -129
- data/ext/oj/validate.c +46 -0
- data/ext/oj/wab.c +342 -353
- data/lib/oj/bag.rb +1 -0
- data/lib/oj/easy_hash.rb +5 -4
- data/lib/oj/error.rb +1 -1
- data/lib/oj/json.rb +1 -1
- data/lib/oj/mimic.rb +48 -14
- data/lib/oj/saj.rb +20 -6
- data/lib/oj/state.rb +8 -7
- data/lib/oj/version.rb +2 -2
- data/lib/oj.rb +0 -8
- data/pages/Compatibility.md +1 -1
- data/pages/JsonGem.md +15 -0
- data/pages/Modes.md +53 -46
- data/pages/Options.md +72 -11
- data/pages/Parser.md +309 -0
- data/pages/Rails.md +73 -22
- data/pages/Security.md +1 -1
- data/test/activerecord/result_test.rb +7 -2
- data/test/activesupport5/abstract_unit.rb +45 -0
- data/test/activesupport5/decoding_test.rb +68 -60
- data/test/activesupport5/encoding_test.rb +111 -96
- data/test/activesupport5/encoding_test_cases.rb +33 -25
- data/test/activesupport5/test_helper.rb +43 -21
- data/test/activesupport5/time_zone_test_helpers.rb +18 -3
- data/test/activesupport6/abstract_unit.rb +44 -0
- data/test/activesupport6/decoding_test.rb +133 -0
- data/test/activesupport6/encoding_test.rb +507 -0
- data/test/activesupport6/encoding_test_cases.rb +98 -0
- data/test/activesupport6/test_common.rb +17 -0
- data/test/activesupport6/test_helper.rb +163 -0
- data/test/activesupport6/time_zone_test_helpers.rb +39 -0
- data/test/activesupport7/abstract_unit.rb +49 -0
- data/test/activesupport7/decoding_test.rb +125 -0
- data/test/activesupport7/encoding_test.rb +486 -0
- data/test/activesupport7/encoding_test_cases.rb +104 -0
- data/test/activesupport7/time_zone_test_helpers.rb +47 -0
- data/test/bar.rb +6 -12
- data/test/baz.rb +16 -0
- data/test/bug.rb +16 -0
- data/test/foo.rb +69 -75
- data/test/helper.rb +16 -0
- data/test/json_gem/json_common_interface_test.rb +8 -3
- data/test/json_gem/json_generator_test.rb +18 -4
- data/test/json_gem/json_parser_test.rb +9 -0
- data/test/json_gem/test_helper.rb +12 -0
- data/test/mem.rb +33 -0
- data/test/perf.rb +1 -1
- data/test/perf_dump.rb +50 -0
- data/test/perf_once.rb +58 -0
- data/test/perf_parser.rb +189 -0
- data/test/perf_scp.rb +11 -10
- data/test/perf_strict.rb +17 -23
- data/test/prec.rb +23 -0
- data/test/sample_json.rb +1 -1
- data/test/test_compat.rb +46 -10
- data/test/test_custom.rb +147 -8
- data/test/test_fast.rb +62 -2
- data/test/test_file.rb +25 -2
- data/test/test_gc.rb +13 -0
- data/test/test_generate.rb +21 -0
- data/test/test_hash.rb +11 -1
- data/test/test_integer_range.rb +7 -2
- data/test/test_object.rb +85 -9
- data/test/test_parser.rb +27 -0
- data/test/test_parser_saj.rb +335 -0
- data/test/test_parser_usual.rb +217 -0
- data/test/test_rails.rb +35 -0
- data/test/test_saj.rb +1 -1
- data/test/test_scp.rb +5 -5
- data/test/test_strict.rb +26 -1
- data/test/test_various.rb +87 -65
- data/test/test_wab.rb +2 -0
- data/test/test_writer.rb +19 -2
- data/test/tests.rb +1 -1
- data/test/zoo.rb +13 -0
- metadata +60 -110
- data/ext/oj/hash.c +0 -163
- data/ext/oj/hash.h +0 -46
- data/ext/oj/hash_test.c +0 -512
@@ -0,0 +1,335 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
$: << File.dirname(__FILE__)
|
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
|
+
end
|
29
|
+
|
30
|
+
def hash_start(key)
|
31
|
+
@calls << [:hash_start, key]
|
32
|
+
end
|
33
|
+
|
34
|
+
def hash_end(key)
|
35
|
+
@calls << [:hash_end, key]
|
36
|
+
end
|
37
|
+
|
38
|
+
def array_start(key)
|
39
|
+
@calls << [:array_start, key]
|
40
|
+
end
|
41
|
+
|
42
|
+
def array_end(key)
|
43
|
+
@calls << [:array_end, key]
|
44
|
+
end
|
45
|
+
|
46
|
+
def add_value(value, key)
|
47
|
+
@calls << [:add_value, value, key]
|
48
|
+
end
|
49
|
+
|
50
|
+
def error(message, line, column)
|
51
|
+
@calls << [:error, message, line, column]
|
52
|
+
end
|
53
|
+
|
54
|
+
end # AllSaj
|
55
|
+
|
56
|
+
class LocSaj
|
57
|
+
attr_accessor :calls
|
58
|
+
|
59
|
+
def initialize()
|
60
|
+
@calls = []
|
61
|
+
end
|
62
|
+
|
63
|
+
def hash_start(key, line, column)
|
64
|
+
@calls << [:hash_start, key, line, column]
|
65
|
+
end
|
66
|
+
|
67
|
+
def hash_end(key, line, column)
|
68
|
+
@calls << [:hash_end, key, line, column]
|
69
|
+
end
|
70
|
+
|
71
|
+
def array_start(key, line, column)
|
72
|
+
@calls << [:array_start, key, line, column]
|
73
|
+
end
|
74
|
+
|
75
|
+
def array_end(key, line, column)
|
76
|
+
@calls << [:array_end, key, line, column]
|
77
|
+
end
|
78
|
+
|
79
|
+
def add_value(value, key, line, column)
|
80
|
+
@calls << [:add_value, value, key, line, column]
|
81
|
+
end
|
82
|
+
|
83
|
+
end # LocSaj
|
84
|
+
|
85
|
+
class SajTest < Minitest::Test
|
86
|
+
|
87
|
+
def test_nil
|
88
|
+
handler = AllSaj.new()
|
89
|
+
json = %{null}
|
90
|
+
p = Oj::Parser.new(:saj)
|
91
|
+
p.handler = handler
|
92
|
+
p.parse(json)
|
93
|
+
assert_equal([[:add_value, nil, nil]], handler.calls)
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_true
|
97
|
+
handler = AllSaj.new()
|
98
|
+
json = %{true}
|
99
|
+
p = Oj::Parser.new(:saj)
|
100
|
+
p.handler = handler
|
101
|
+
p.parse(json)
|
102
|
+
assert_equal([[:add_value, true, nil]], handler.calls)
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_false
|
106
|
+
handler = AllSaj.new()
|
107
|
+
json = %{false}
|
108
|
+
p = Oj::Parser.new(:saj)
|
109
|
+
p.handler = handler
|
110
|
+
p.parse(json)
|
111
|
+
assert_equal([[:add_value, false, nil]], handler.calls)
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_string
|
115
|
+
handler = AllSaj.new()
|
116
|
+
json = %{"a string"}
|
117
|
+
p = Oj::Parser.new(:saj)
|
118
|
+
p.handler = handler
|
119
|
+
p.parse(json)
|
120
|
+
assert_equal([[:add_value, 'a string', nil]], handler.calls)
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_fixnum
|
124
|
+
handler = AllSaj.new()
|
125
|
+
json = %{12345}
|
126
|
+
p = Oj::Parser.new(:saj)
|
127
|
+
p.handler = handler
|
128
|
+
p.parse(json)
|
129
|
+
assert_equal([[:add_value, 12345, nil]], handler.calls)
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_float
|
133
|
+
handler = AllSaj.new()
|
134
|
+
json = %{12345.6789}
|
135
|
+
p = Oj::Parser.new(:saj)
|
136
|
+
p.handler = handler
|
137
|
+
p.parse(json)
|
138
|
+
assert_equal([[:add_value, 12345.6789, nil]], handler.calls)
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_float_exp
|
142
|
+
handler = AllSaj.new()
|
143
|
+
json = %{12345.6789e7}
|
144
|
+
p = Oj::Parser.new(:saj)
|
145
|
+
p.handler = handler
|
146
|
+
p.parse(json)
|
147
|
+
assert_equal(1, handler.calls.size)
|
148
|
+
assert_equal(:add_value, handler.calls[0][0])
|
149
|
+
assert_equal((12345.6789e7 * 10000).to_i, (handler.calls[0][1] * 10000).to_i)
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_bignum
|
153
|
+
handler = AllSaj.new()
|
154
|
+
json = %{-11.899999999999999}
|
155
|
+
p = Oj::Parser.new(:saj)
|
156
|
+
p.handler = handler
|
157
|
+
p.parse(json)
|
158
|
+
assert_equal(1, handler.calls.size)
|
159
|
+
assert_equal(:add_value, handler.calls[0][0])
|
160
|
+
assert_equal(-118999, (handler.calls[0][1] * 10000).to_i)
|
161
|
+
end
|
162
|
+
|
163
|
+
def test_bignum_loc
|
164
|
+
handler = LocSaj.new()
|
165
|
+
json = <<~JSON
|
166
|
+
{
|
167
|
+
"width": 192.33800000000002,
|
168
|
+
"xaxis": {
|
169
|
+
"anchor": "y"
|
170
|
+
}
|
171
|
+
}
|
172
|
+
JSON
|
173
|
+
|
174
|
+
p = Oj::Parser.new(:saj)
|
175
|
+
p.handler = handler
|
176
|
+
p.parse(json)
|
177
|
+
assert_equal(6, handler.calls.size)
|
178
|
+
assert_equal(1_923_380, (handler.calls[1][1] * 10000).to_i)
|
179
|
+
handler.calls[1][1] = 1_923_380
|
180
|
+
assert_equal([[:hash_start, nil, 1, 1],
|
181
|
+
[:add_value, 1923380, 'width', 2, 30],
|
182
|
+
[:hash_start, 'xaxis', 3, 12],
|
183
|
+
[:add_value, 'y', 'anchor', 4, 17],
|
184
|
+
[:hash_end, 'xaxis', 5, 3],
|
185
|
+
[:hash_end, nil, 6, 1]],
|
186
|
+
handler.calls)
|
187
|
+
end
|
188
|
+
|
189
|
+
def test_array_empty
|
190
|
+
handler = AllSaj.new()
|
191
|
+
json = %{[]}
|
192
|
+
p = Oj::Parser.new(:saj)
|
193
|
+
p.handler = handler
|
194
|
+
p.parse(json)
|
195
|
+
assert_equal([[:array_start, nil],
|
196
|
+
[:array_end, nil]], handler.calls)
|
197
|
+
end
|
198
|
+
|
199
|
+
def test_array
|
200
|
+
handler = AllSaj.new()
|
201
|
+
json = %{[true,false]}
|
202
|
+
p = Oj::Parser.new(:saj)
|
203
|
+
p.handler = handler
|
204
|
+
p.parse(json)
|
205
|
+
assert_equal([[:array_start, nil],
|
206
|
+
[:add_value, true, nil],
|
207
|
+
[:add_value, false, nil],
|
208
|
+
[:array_end, nil]], handler.calls)
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_hash_empty
|
212
|
+
handler = AllSaj.new()
|
213
|
+
json = %{{}}
|
214
|
+
p = Oj::Parser.new(:saj)
|
215
|
+
p.handler = handler
|
216
|
+
p.parse(json)
|
217
|
+
assert_equal([[:hash_start, nil],
|
218
|
+
[:hash_end, nil]], handler.calls)
|
219
|
+
end
|
220
|
+
|
221
|
+
def test_hash
|
222
|
+
handler = AllSaj.new()
|
223
|
+
json = %{{"one":true,"two":false}}
|
224
|
+
p = Oj::Parser.new(:saj)
|
225
|
+
p.handler = handler
|
226
|
+
p.parse(json)
|
227
|
+
assert_equal([[:hash_start, nil],
|
228
|
+
[:add_value, true, 'one'],
|
229
|
+
[:add_value, false, 'two'],
|
230
|
+
[:hash_end, nil]], handler.calls)
|
231
|
+
end
|
232
|
+
|
233
|
+
def test_full
|
234
|
+
handler = AllSaj.new()
|
235
|
+
Oj.saj_parse(handler, $json)
|
236
|
+
assert_equal([[:hash_start, nil],
|
237
|
+
[:array_start, 'array'],
|
238
|
+
[:hash_start, nil],
|
239
|
+
[:add_value, 3, 'num'],
|
240
|
+
[:add_value, 'message', 'string'],
|
241
|
+
[:hash_start, 'hash'],
|
242
|
+
[:hash_start, 'h2'],
|
243
|
+
[:array_start, 'a'],
|
244
|
+
[:add_value, 1, nil],
|
245
|
+
[:add_value, 2, nil],
|
246
|
+
[:add_value, 3, nil],
|
247
|
+
[:array_end, 'a'],
|
248
|
+
[:hash_end, 'h2'],
|
249
|
+
[:hash_end, 'hash'],
|
250
|
+
[:hash_end, nil],
|
251
|
+
[:array_end, 'array'],
|
252
|
+
[:add_value, true, 'boolean'],
|
253
|
+
[:hash_end, nil]], handler.calls)
|
254
|
+
end
|
255
|
+
|
256
|
+
def test_multiple
|
257
|
+
handler = AllSaj.new()
|
258
|
+
json = %|[true][false]|
|
259
|
+
p = Oj::Parser.new(:saj)
|
260
|
+
p.handler = handler
|
261
|
+
p.parse(json)
|
262
|
+
assert_equal([
|
263
|
+
[:array_start, nil],
|
264
|
+
[:add_value, true, nil],
|
265
|
+
[:array_end, nil],
|
266
|
+
[:array_start, nil],
|
267
|
+
[:add_value, false, nil],
|
268
|
+
[:array_end, nil],
|
269
|
+
], handler.calls)
|
270
|
+
end
|
271
|
+
|
272
|
+
def test_io
|
273
|
+
handler = AllSaj.new()
|
274
|
+
json = %| [true,false] |
|
275
|
+
p = Oj::Parser.new(:saj)
|
276
|
+
p.handler = handler
|
277
|
+
p.load(StringIO.new(json))
|
278
|
+
assert_equal([
|
279
|
+
[:array_start, nil],
|
280
|
+
[:add_value, true, nil],
|
281
|
+
[:add_value, false, nil],
|
282
|
+
[:array_end, nil],
|
283
|
+
], handler.calls)
|
284
|
+
end
|
285
|
+
|
286
|
+
def test_file
|
287
|
+
handler = AllSaj.new()
|
288
|
+
p = Oj::Parser.new(:saj)
|
289
|
+
p.handler = handler
|
290
|
+
p.file('saj_test.json')
|
291
|
+
assert_equal([
|
292
|
+
[:array_start, nil],
|
293
|
+
[:add_value, true, nil],
|
294
|
+
[:add_value, false, nil],
|
295
|
+
[:array_end, nil],
|
296
|
+
], handler.calls)
|
297
|
+
end
|
298
|
+
|
299
|
+
def test_default
|
300
|
+
handler = AllSaj.new()
|
301
|
+
json = %|[true]|
|
302
|
+
Oj::Parser.saj.handler = handler
|
303
|
+
Oj::Parser.saj.parse(json)
|
304
|
+
assert_equal([
|
305
|
+
[:array_start, nil],
|
306
|
+
[:add_value, true, nil],
|
307
|
+
[:array_end, nil],
|
308
|
+
], handler.calls)
|
309
|
+
end
|
310
|
+
|
311
|
+
def test_loc
|
312
|
+
handler = LocSaj.new()
|
313
|
+
Oj::Parser.saj.handler = handler
|
314
|
+
Oj::Parser.saj.parse($json)
|
315
|
+
assert_equal([[:hash_start, nil, 1, 1],
|
316
|
+
[:array_start, 'array', 2, 12],
|
317
|
+
[:hash_start, nil, 3, 5],
|
318
|
+
[:add_value, 3, 'num', 4, 18],
|
319
|
+
[:add_value, 'message', 'string', 5, 25],
|
320
|
+
[:hash_start, 'hash', 6, 17],
|
321
|
+
[:hash_start, 'h2', 7, 17],
|
322
|
+
[:array_start, 'a', 8, 17],
|
323
|
+
[:add_value, 1, nil, 8, 20],
|
324
|
+
[:add_value, 2, nil, 8, 23],
|
325
|
+
[:add_value, 3, nil, 8, 26],
|
326
|
+
[:array_end, 'a', 8, 27],
|
327
|
+
[:hash_end, 'h2', 9, 9],
|
328
|
+
[:hash_end, 'hash', 10, 7],
|
329
|
+
[:hash_end, nil, 11, 5],
|
330
|
+
[:array_end, 'array', 12, 3],
|
331
|
+
[:add_value, true, 'boolean', 13, 18],
|
332
|
+
[:hash_end, nil, 14, 1]], handler.calls)
|
333
|
+
end
|
334
|
+
|
335
|
+
end
|
@@ -0,0 +1,217 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
$: << File.dirname(__FILE__)
|
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
|
+
assert_equal(false, 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
|
+
class MyClass
|
151
|
+
attr_accessor :a
|
152
|
+
attr_accessor :b
|
153
|
+
|
154
|
+
def to_s
|
155
|
+
"#{self.class}{a: #{@a} b: #{b}}"
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
class MyClass2 < MyClass
|
160
|
+
def self.json_create(arg)
|
161
|
+
obj = new
|
162
|
+
obj.a = arg['a']
|
163
|
+
obj.b = arg['b']
|
164
|
+
obj
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
def test_create_id
|
169
|
+
p = Oj::Parser.new(:usual)
|
170
|
+
p.create_id = '^'
|
171
|
+
doc = p.parse('{"a":true}')
|
172
|
+
assert_equal(Hash, doc.class)
|
173
|
+
doc = p.parse('{"a":true,"^":"UsualTest::MyClass","b":false}')
|
174
|
+
assert_equal('UsualTest::MyClass{a: true b: false}', doc.to_s)
|
175
|
+
|
176
|
+
doc = p.parse('{"a":true,"^":"UsualTest::MyClass2","b":false}')
|
177
|
+
assert_equal('UsualTest::MyClass2{a: true b: false}', doc.to_s)
|
178
|
+
|
179
|
+
p.hash_class = MyHash
|
180
|
+
assert_equal(MyHash, p.hash_class)
|
181
|
+
doc = p.parse('{"a":true}')
|
182
|
+
assert_equal(MyHash, doc.class)
|
183
|
+
|
184
|
+
doc = p.parse('{"a":true,"^":"UsualTest::MyClass","b":false}')
|
185
|
+
assert_equal('UsualTest::MyClass{a: true b: false}', doc.to_s)
|
186
|
+
end
|
187
|
+
|
188
|
+
def test_missing_class
|
189
|
+
p = Oj::Parser.new(:usual, create_id: '^')
|
190
|
+
json = '{"a":true,"^":"Auto","b":false}'
|
191
|
+
doc = p.parse(json)
|
192
|
+
assert_equal(Hash, doc.class)
|
193
|
+
|
194
|
+
p.missing_class = :auto
|
195
|
+
doc = p.parse(json)
|
196
|
+
# Auto should be defined after parsing
|
197
|
+
assert_equal(Auto, doc.class)
|
198
|
+
end
|
199
|
+
|
200
|
+
def test_class_cache
|
201
|
+
p = Oj::Parser.new(:usual)
|
202
|
+
p.create_id = '^'
|
203
|
+
p.class_cache = true
|
204
|
+
p.missing_class = :auto
|
205
|
+
json = '{"a":true,"^":"Auto2","b":false}'
|
206
|
+
doc = p.parse(json)
|
207
|
+
assert_equal(Auto2, doc.class)
|
208
|
+
|
209
|
+
doc = p.parse(json)
|
210
|
+
assert_equal(Auto2, doc.class)
|
211
|
+
end
|
212
|
+
|
213
|
+
def test_default_parser
|
214
|
+
doc = Oj::Parser.usual.parse('{"a":true,"b":null}')
|
215
|
+
assert_equal({'a'=>true, 'b'=>nil}, doc)
|
216
|
+
end
|
217
|
+
end
|
data/test/test_rails.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
$: << File.dirname(__FILE__)
|
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
|
data/test/test_saj.rb
CHANGED
@@ -180,7 +180,7 @@ class SajTest < Minitest::Test
|
|
180
180
|
assert_equal([:add_value, 12345, nil], handler.calls.first)
|
181
181
|
type, message, line, column = handler.calls.last
|
182
182
|
assert_equal([:error, 1, 6], [type, line, column])
|
183
|
-
assert_match(%r{invalid format, extra characters at line 1, column 6 \[(?:[a-z\.]+/)*saj\.c:\d+\]}, message)
|
183
|
+
assert_match(%r{invalid format, extra characters at line 1, column 6 \[(?:[A-Za-z]:\/)?(?:[a-z\.]+/)*saj\.c:\d+\]}, message)
|
184
184
|
end
|
185
185
|
|
186
186
|
end
|
data/test/test_scp.rb
CHANGED
@@ -320,15 +320,15 @@ class ScpTest < Minitest::Test
|
|
320
320
|
end
|
321
321
|
|
322
322
|
def test_pipe
|
323
|
-
|
324
|
-
|
323
|
+
skip ' Windows does not support fork' if RbConfig::CONFIG['host_os'] =~ /(mingw|mswin)/
|
324
|
+
skip 'TruffleRuby fails this spec with `NotImplementedError: fork is not available`' if RUBY_ENGINE == 'truffleruby'
|
325
325
|
|
326
326
|
handler = AllHandler.new()
|
327
327
|
json = %{{"one":true,"two":false}}
|
328
328
|
IO.pipe do |read_io, write_io|
|
329
329
|
if fork
|
330
330
|
write_io.close
|
331
|
-
Oj.sc_parse(handler, read_io)
|
331
|
+
Oj.sc_parse(handler, read_io)
|
332
332
|
read_io.close
|
333
333
|
assert_equal([[:hash_start],
|
334
334
|
[:hash_key, 'one'],
|
@@ -357,8 +357,8 @@ class ScpTest < Minitest::Test
|
|
357
357
|
end
|
358
358
|
|
359
359
|
def test_pipe_close
|
360
|
-
|
361
|
-
|
360
|
+
skip 'Windows does not support fork' if RbConfig::CONFIG['host_os'] =~ /(mingw|mswin)/
|
361
|
+
skip 'TruffleRuby fails this spec with `NotImplementedError: fork is not available`' if RUBY_ENGINE == 'truffleruby'
|
362
362
|
|
363
363
|
json = %{{"one":true,"two":false}}
|
364
364
|
IO.pipe do |read_io, write_io|
|
data/test/test_strict.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
# encoding:
|
2
|
+
# encoding: utf-8
|
3
3
|
|
4
4
|
$: << File.dirname(__FILE__)
|
5
5
|
$oj_dir = File.dirname(File.expand_path(File.dirname(__FILE__)))
|
@@ -156,6 +156,8 @@ class StrictJuice < Minitest::Test
|
|
156
156
|
end
|
157
157
|
|
158
158
|
def test_deep_nest
|
159
|
+
skip 'TruffleRuby causes SEGV' if RUBY_ENGINE == 'truffleruby'
|
160
|
+
|
159
161
|
begin
|
160
162
|
n = 10000
|
161
163
|
Oj.strict_load('[' * n + ']' * n)
|
@@ -379,6 +381,29 @@ class StrictJuice < Minitest::Test
|
|
379
381
|
assert_equal([{ 'x' => 1 }, { 'y' => 2 }], results)
|
380
382
|
end
|
381
383
|
|
384
|
+
def test_invalid_decimal_dot_start
|
385
|
+
assert_raises(Oj::ParseError) {
|
386
|
+
Oj.load('.123', mode: :strict)
|
387
|
+
}
|
388
|
+
assert_raises(Oj::ParseError) {
|
389
|
+
Oj.load('-.123', mode: :strict)
|
390
|
+
}
|
391
|
+
end
|
392
|
+
|
393
|
+
def test_invalid_decimal_dot_end
|
394
|
+
json = '123.'
|
395
|
+
assert_raises(Oj::ParseError) {
|
396
|
+
Oj.load(json, mode: :strict)
|
397
|
+
}
|
398
|
+
end
|
399
|
+
|
400
|
+
def test_invalid_decimal_plus
|
401
|
+
json = '+12'
|
402
|
+
assert_raises(Oj::ParseError) {
|
403
|
+
Oj.load(json, mode: :strict)
|
404
|
+
}
|
405
|
+
end
|
406
|
+
|
382
407
|
def test_circular_hash
|
383
408
|
h = { 'a' => 7 }
|
384
409
|
h['b'] = h
|