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_parser.rb
ADDED
@@ -0,0 +1,11 @@
|
|
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'
|
@@ -0,0 +1,27 @@
|
|
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
|
@@ -0,0 +1,337 @@
|
|
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
|
@@ -0,0 +1,251 @@
|
|
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_multi_parse
|
118
|
+
p = Oj::Parser.new(:usual)
|
119
|
+
out = []
|
120
|
+
p.parse('{"a":1}{"b":{"x":2}} {"c":3}') { |j| out.push(j) }
|
121
|
+
assert_equal([{'a'=>1}, {'b'=>{'x'=>2}},{'c'=>3}], out)
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_multi_load
|
125
|
+
p = Oj::Parser.new(:usual)
|
126
|
+
out = []
|
127
|
+
r, w = IO.pipe
|
128
|
+
thread = Thread.new do
|
129
|
+
['{"a":1}', '{"b":{"x"', ':2}}{"c":', '3}'].each { |seg|
|
130
|
+
w.write(seg)
|
131
|
+
sleep(0.1)
|
132
|
+
}
|
133
|
+
w.close
|
134
|
+
end
|
135
|
+
p.load(r) { |j| out.push(j) }
|
136
|
+
r.close
|
137
|
+
thread.join
|
138
|
+
assert_equal([{'a'=>1}, {'b'=>{'x'=>2}},{'c'=>3}], out)
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_omit_null
|
142
|
+
p = Oj::Parser.new(:usual)
|
143
|
+
p.omit_null = true
|
144
|
+
doc = p.parse('{"a":true,"b":null}')
|
145
|
+
assert_equal({'a'=>true}, doc)
|
146
|
+
|
147
|
+
p.omit_null = false
|
148
|
+
doc = p.parse('{"a":true,"b":null}')
|
149
|
+
assert_equal({'a'=>true, 'b'=>nil}, doc)
|
150
|
+
end
|
151
|
+
|
152
|
+
class MyArray < Array
|
153
|
+
end
|
154
|
+
|
155
|
+
def test_array_class
|
156
|
+
p = Oj::Parser.new(:usual)
|
157
|
+
p.array_class = MyArray
|
158
|
+
assert_equal(MyArray, p.array_class)
|
159
|
+
doc = p.parse('[true]')
|
160
|
+
assert_equal(MyArray, doc.class)
|
161
|
+
end
|
162
|
+
|
163
|
+
class MyHash < Hash
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_hash_class
|
167
|
+
p = Oj::Parser.new(:usual)
|
168
|
+
p.hash_class = MyHash
|
169
|
+
assert_equal(MyHash, p.hash_class)
|
170
|
+
doc = p.parse('{"a":true}')
|
171
|
+
assert_equal(MyHash, doc.class)
|
172
|
+
end
|
173
|
+
|
174
|
+
def test_empty
|
175
|
+
p = Oj::Parser.new(:usual)
|
176
|
+
p.raise_on_empty = false
|
177
|
+
doc = p.parse(' ')
|
178
|
+
assert_nil(doc)
|
179
|
+
|
180
|
+
p.raise_on_empty = true
|
181
|
+
assert_raises(Oj::ParseError) { p.parse(' ') }
|
182
|
+
end
|
183
|
+
|
184
|
+
class MyClass
|
185
|
+
attr_accessor :a
|
186
|
+
attr_accessor :b
|
187
|
+
|
188
|
+
def to_s
|
189
|
+
"#{self.class}{a: #{@a} b: #{b}}"
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
class MyClass2 < MyClass
|
194
|
+
def self.json_create(arg)
|
195
|
+
obj = new
|
196
|
+
obj.a = arg['a']
|
197
|
+
obj.b = arg['b']
|
198
|
+
obj
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
def test_create_id
|
203
|
+
p = Oj::Parser.new(:usual)
|
204
|
+
p.create_id = '^'
|
205
|
+
doc = p.parse('{"a":true}')
|
206
|
+
assert_equal(Hash, doc.class)
|
207
|
+
doc = p.parse('{"a":true,"^":"UsualTest::MyClass","b":false}')
|
208
|
+
assert_equal('UsualTest::MyClass{a: true b: false}', doc.to_s)
|
209
|
+
|
210
|
+
doc = p.parse('{"a":true,"^":"UsualTest::MyClass2","b":false}')
|
211
|
+
assert_equal('UsualTest::MyClass2{a: true b: false}', doc.to_s)
|
212
|
+
|
213
|
+
p.hash_class = MyHash
|
214
|
+
assert_equal(MyHash, p.hash_class)
|
215
|
+
doc = p.parse('{"a":true}')
|
216
|
+
assert_equal(MyHash, doc.class)
|
217
|
+
|
218
|
+
doc = p.parse('{"a":true,"^":"UsualTest::MyClass","b":false}')
|
219
|
+
assert_equal('UsualTest::MyClass{a: true b: false}', doc.to_s)
|
220
|
+
end
|
221
|
+
|
222
|
+
def test_missing_class
|
223
|
+
p = Oj::Parser.new(:usual, create_id: '^')
|
224
|
+
json = '{"a":true,"^":"Auto","b":false}'
|
225
|
+
doc = p.parse(json)
|
226
|
+
assert_equal(Hash, doc.class)
|
227
|
+
|
228
|
+
p.missing_class = :auto
|
229
|
+
doc = p.parse(json)
|
230
|
+
# Auto should be defined after parsing
|
231
|
+
assert_equal(Auto, doc.class)
|
232
|
+
end
|
233
|
+
|
234
|
+
def test_class_cache
|
235
|
+
p = Oj::Parser.new(:usual)
|
236
|
+
p.create_id = '^'
|
237
|
+
p.class_cache = true
|
238
|
+
p.missing_class = :auto
|
239
|
+
json = '{"a":true,"^":"Auto2","b":false}'
|
240
|
+
doc = p.parse(json)
|
241
|
+
assert_equal(Auto2, doc.class)
|
242
|
+
|
243
|
+
doc = p.parse(json)
|
244
|
+
assert_equal(Auto2, doc.class)
|
245
|
+
end
|
246
|
+
|
247
|
+
def test_default_parser
|
248
|
+
doc = Oj::Parser.usual.parse('{"a":true,"b":null}')
|
249
|
+
assert_equal({'a'=>true, 'b'=>nil}, doc)
|
250
|
+
end
|
251
|
+
end
|
data/test/test_rails.rb
CHANGED
data/test/test_saj.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
#
|
2
|
+
# frozen_string_literal: true
|
3
3
|
|
4
|
-
|
4
|
+
$LOAD_PATH << __dir__
|
5
5
|
|
6
6
|
require 'helper'
|
7
7
|
|
@@ -23,8 +23,10 @@ $json = %{{
|
|
23
23
|
class AllSaj < Oj::Saj
|
24
24
|
attr_accessor :calls
|
25
25
|
|
26
|
-
def initialize
|
26
|
+
def initialize
|
27
27
|
@calls = []
|
28
|
+
|
29
|
+
super
|
28
30
|
end
|
29
31
|
|
30
32
|
def hash_start(key)
|
@@ -95,14 +97,14 @@ class SajTest < Minitest::Test
|
|
95
97
|
handler = AllSaj.new()
|
96
98
|
json = %{12345}
|
97
99
|
Oj.saj_parse(handler, json)
|
98
|
-
assert_equal([[:add_value,
|
100
|
+
assert_equal([[:add_value, 12_345, nil]], handler.calls)
|
99
101
|
end
|
100
102
|
|
101
103
|
def test_float
|
102
104
|
handler = AllSaj.new()
|
103
105
|
json = %{12345.6789}
|
104
106
|
Oj.saj_parse(handler, json)
|
105
|
-
assert_equal([[:add_value,
|
107
|
+
assert_equal([[:add_value, 12_345.6789, nil]], handler.calls)
|
106
108
|
end
|
107
109
|
|
108
110
|
def test_float_exp
|
@@ -111,7 +113,7 @@ class SajTest < Minitest::Test
|
|
111
113
|
Oj.saj_parse(handler, json)
|
112
114
|
assert_equal(1, handler.calls.size)
|
113
115
|
assert_equal(:add_value, handler.calls[0][0])
|
114
|
-
assert_equal((
|
116
|
+
assert_equal((12_345.6789e7 * 10_000).to_i, (handler.calls[0][1] * 10_000).to_i)
|
115
117
|
end
|
116
118
|
|
117
119
|
def test_array_empty
|
@@ -177,10 +179,10 @@ class SajTest < Minitest::Test
|
|
177
179
|
handler = AllSaj.new()
|
178
180
|
json = %{12345xyz}
|
179
181
|
Oj.saj_parse(handler, json)
|
180
|
-
assert_equal([:add_value,
|
182
|
+
assert_equal([:add_value, 12_345, nil], handler.calls.first)
|
181
183
|
type, message, line, column = handler.calls.last
|
182
184
|
assert_equal([:error, 1, 6], [type, line, column])
|
183
|
-
assert_match(%r{invalid format, extra characters at line 1, column 6 \[(?:[a-z
|
185
|
+
assert_match(%r{invalid format, extra characters at line 1, column 6 \[(?:[A-Za-z]:/)?(?:[a-z.]+/)*saj\.c:\d+\]}, message)
|
184
186
|
end
|
185
187
|
|
186
188
|
end
|