oj 2.18.3 → 3.13.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +1324 -0
- data/README.md +51 -204
- data/RELEASE_NOTES.md +61 -0
- data/ext/oj/buf.h +49 -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 -68
- data/ext/oj/circarray.h +16 -42
- data/ext/oj/code.c +221 -0
- data/ext/oj/code.h +40 -0
- data/ext/oj/compat.c +231 -107
- data/ext/oj/custom.c +1125 -0
- data/ext/oj/debug.c +132 -0
- data/ext/oj/dump.c +935 -2513
- data/ext/oj/dump.h +108 -0
- data/ext/oj/dump_compat.c +936 -0
- data/ext/oj/dump_leaf.c +164 -0
- data/ext/oj/dump_object.c +761 -0
- data/ext/oj/dump_strict.c +410 -0
- data/ext/oj/encode.h +7 -42
- data/ext/oj/encoder.c +43 -0
- data/ext/oj/err.c +40 -54
- data/ext/oj/err.h +52 -46
- data/ext/oj/extconf.rb +21 -30
- data/ext/oj/fast.c +1097 -1080
- data/ext/oj/intern.c +301 -0
- data/ext/oj/intern.h +26 -0
- data/ext/oj/mimic_json.c +893 -0
- data/ext/oj/object.c +549 -620
- data/ext/oj/odd.c +155 -167
- data/ext/oj/odd.h +37 -63
- data/ext/oj/oj.c +1661 -2063
- data/ext/oj/oj.h +341 -270
- data/ext/oj/parse.c +974 -737
- data/ext/oj/parse.h +105 -97
- data/ext/oj/parser.c +1526 -0
- data/ext/oj/parser.h +90 -0
- data/ext/oj/rails.c +1504 -0
- data/ext/oj/rails.h +18 -0
- data/ext/oj/reader.c +141 -163
- data/ext/oj/reader.h +75 -113
- data/ext/oj/resolve.c +45 -93
- data/ext/oj/resolve.h +7 -34
- data/ext/oj/rxclass.c +143 -0
- data/ext/oj/rxclass.h +26 -0
- data/ext/oj/saj.c +447 -511
- data/ext/oj/saj2.c +348 -0
- data/ext/oj/scp.c +91 -138
- data/ext/oj/sparse.c +793 -644
- data/ext/oj/stream_writer.c +331 -0
- data/ext/oj/strict.c +145 -109
- data/ext/oj/string_writer.c +493 -0
- data/ext/oj/trace.c +72 -0
- data/ext/oj/trace.h +28 -0
- 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 +62 -70
- data/ext/oj/val_stack.h +95 -129
- data/ext/oj/validate.c +51 -0
- data/ext/oj/wab.c +622 -0
- data/lib/oj/bag.rb +1 -0
- data/lib/oj/easy_hash.rb +17 -8
- data/lib/oj/error.rb +10 -11
- data/lib/oj/json.rb +176 -0
- data/lib/oj/mimic.rb +158 -19
- data/lib/oj/state.rb +132 -0
- data/lib/oj/version.rb +2 -2
- data/lib/oj.rb +1 -31
- data/pages/Advanced.md +22 -0
- data/pages/Compatibility.md +25 -0
- data/pages/Custom.md +23 -0
- data/pages/Encoding.md +65 -0
- data/pages/JsonGem.md +94 -0
- data/pages/Modes.md +161 -0
- data/pages/Options.md +327 -0
- data/pages/Parser.md +309 -0
- data/pages/Rails.md +167 -0
- data/pages/Security.md +20 -0
- data/pages/WAB.md +13 -0
- data/test/activerecord/result_test.rb +32 -0
- data/test/activesupport4/decoding_test.rb +108 -0
- data/test/activesupport4/encoding_test.rb +531 -0
- data/test/activesupport4/test_helper.rb +41 -0
- data/test/activesupport5/abstract_unit.rb +45 -0
- data/test/activesupport5/decoding_test.rb +133 -0
- data/test/activesupport5/encoding_test.rb +500 -0
- data/test/activesupport5/encoding_test_cases.rb +98 -0
- data/test/activesupport5/test_helper.rb +72 -0
- data/test/activesupport5/time_zone_test_helpers.rb +39 -0
- 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 +9 -0
- data/test/baz.rb +16 -0
- data/test/bug.rb +11 -46
- data/test/foo.rb +69 -16
- data/test/helper.rb +10 -1
- data/test/isolated/shared.rb +12 -8
- data/test/isolated/test_mimic_rails_after.rb +3 -3
- data/test/isolated/test_mimic_rails_before.rb +3 -3
- data/test/json_gem/json_addition_test.rb +216 -0
- data/test/json_gem/json_common_interface_test.rb +153 -0
- data/test/json_gem/json_encoding_test.rb +107 -0
- data/test/json_gem/json_ext_parser_test.rb +20 -0
- data/test/json_gem/json_fixtures_test.rb +35 -0
- data/test/json_gem/json_generator_test.rb +397 -0
- data/test/json_gem/json_generic_object_test.rb +90 -0
- data/test/json_gem/json_parser_test.rb +470 -0
- data/test/json_gem/json_string_matching_test.rb +42 -0
- data/test/json_gem/test_helper.rb +26 -0
- data/test/mem.rb +33 -0
- data/test/perf.rb +1 -1
- data/test/perf_compat.rb +30 -28
- data/test/perf_dump.rb +50 -0
- data/test/perf_object.rb +1 -1
- 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 +30 -19
- data/test/perf_wab.rb +131 -0
- data/test/prec.rb +23 -0
- data/test/sample.rb +0 -1
- data/test/sample_json.rb +1 -1
- data/test/test_compat.rb +219 -102
- data/test/test_custom.rb +533 -0
- data/test/test_fast.rb +107 -35
- data/test/test_file.rb +19 -25
- data/test/test_generate.rb +21 -0
- data/test/test_hash.rb +11 -1
- data/test/test_integer_range.rb +72 -0
- data/test/test_null.rb +376 -0
- data/test/test_object.rb +357 -70
- data/test/test_parser.rb +27 -0
- data/test/test_parser_saj.rb +245 -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 +39 -2
- data/test/test_strict.rb +186 -7
- data/test/test_various.rb +160 -774
- data/test/test_wab.rb +307 -0
- data/test/test_writer.rb +90 -2
- data/test/tests.rb +24 -0
- data/test/tests_mimic.rb +14 -0
- data/test/tests_mimic_addition.rb +7 -0
- data/test/zoo.rb +13 -0
- metadata +194 -56
- data/ext/oj/hash.c +0 -163
- data/ext/oj/hash.h +0 -46
- data/ext/oj/hash_test.c +0 -512
- data/test/activesupport_datetime_test.rb +0 -23
- data/test/bug2.rb +0 -10
- data/test/bug3.rb +0 -46
- data/test/bug_fast.rb +0 -32
- data/test/bug_load.rb +0 -24
- data/test/crash.rb +0 -111
- data/test/curl/curl_oj.rb +0 -46
- data/test/curl/get_oj.rb +0 -24
- data/test/curl/just_curl.rb +0 -31
- data/test/curl/just_oj.rb +0 -51
- data/test/example.rb +0 -11
- data/test/io.rb +0 -48
- data/test/isolated/test_mimic_rails_datetime.rb +0 -27
- data/test/mod.rb +0 -16
- data/test/rails.rb +0 -50
- data/test/russian.rb +0 -18
- data/test/struct.rb +0 -29
- data/test/test_serializer.rb +0 -59
- data/test/write_timebars.rb +0 -31
@@ -0,0 +1,470 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
# frozen_string_literal: false
|
5
|
+
|
6
|
+
require 'json_gem/test_helper'
|
7
|
+
|
8
|
+
require 'stringio'
|
9
|
+
require 'tempfile'
|
10
|
+
require 'ostruct'
|
11
|
+
|
12
|
+
class JSONParserTest < Test::Unit::TestCase
|
13
|
+
include Test::Unit::TestCaseOmissionSupport
|
14
|
+
|
15
|
+
def test_construction
|
16
|
+
parser = JSON::Parser.new('test')
|
17
|
+
assert_equal 'test', parser.source
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_argument_encoding
|
21
|
+
source = "{}".encode("UTF-16")
|
22
|
+
JSON::Parser.new(source)
|
23
|
+
assert_equal Encoding::UTF_16, source.encoding
|
24
|
+
end if defined?(Encoding::UTF_16)
|
25
|
+
|
26
|
+
def test_error_message_encoding
|
27
|
+
bug10705 = '[ruby-core:67386] [Bug #10705]'
|
28
|
+
json = ".\"\xE2\x88\x9A\"".force_encoding(Encoding::UTF_8)
|
29
|
+
e = assert_raise(JSON::ParserError) {
|
30
|
+
JSON::Ext::Parser.new(json).parse
|
31
|
+
}
|
32
|
+
assert_equal(Encoding::UTF_8, e.message.encoding, bug10705)
|
33
|
+
assert_include(e.message, json, bug10705)
|
34
|
+
end if defined?(Encoding::UTF_8)
|
35
|
+
|
36
|
+
def test_parsing
|
37
|
+
parser = JSON::Parser.new('"test"')
|
38
|
+
assert_equal 'test', parser.parse
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_parser_reset
|
42
|
+
parser = JSON::Parser.new('{"a":"b"}')
|
43
|
+
assert_equal({ 'a' => 'b' }, parser.parse)
|
44
|
+
assert_equal({ 'a' => 'b' }, parser.parse)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_parse_values
|
48
|
+
assert_equal(nil, JSON.parse('null'))
|
49
|
+
assert_equal(false, JSON.parse('false'))
|
50
|
+
assert_equal(true, JSON.parse('true'))
|
51
|
+
assert_equal(-23, JSON.parse('-23'))
|
52
|
+
assert_equal(23, JSON.parse('23'))
|
53
|
+
assert_in_delta(0.23, JSON.parse('0.23'), 1e-2)
|
54
|
+
assert_in_delta(0.0, JSON.parse('0e0'), 1e-2)
|
55
|
+
assert_equal("", JSON.parse('""'))
|
56
|
+
assert_equal("foobar", JSON.parse('"foobar"'))
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_parse_simple_arrays
|
60
|
+
assert_equal([], JSON.parse('[]'))
|
61
|
+
assert_equal([], JSON.parse(' [ ] '))
|
62
|
+
assert_equal([ nil ], JSON.parse('[null]'))
|
63
|
+
assert_equal([ false ], JSON.parse('[false]'))
|
64
|
+
assert_equal([ true ], JSON.parse('[true]'))
|
65
|
+
assert_equal([ -23 ], JSON.parse('[-23]'))
|
66
|
+
assert_equal([ 23 ], JSON.parse('[23]'))
|
67
|
+
assert_equal_float([ 0.23 ], JSON.parse('[0.23]'))
|
68
|
+
assert_equal_float([ 0.0 ], JSON.parse('[0e0]'))
|
69
|
+
assert_equal([""], JSON.parse('[""]'))
|
70
|
+
assert_equal(["foobar"], JSON.parse('["foobar"]'))
|
71
|
+
assert_equal([{}], JSON.parse('[{}]'))
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_parse_simple_objects
|
75
|
+
assert_equal({}, JSON.parse('{}'))
|
76
|
+
assert_equal({}, JSON.parse(' { } '))
|
77
|
+
assert_equal({ "a" => nil }, JSON.parse('{ "a" : null}'))
|
78
|
+
assert_equal({ "a" => nil }, JSON.parse('{"a":null}'))
|
79
|
+
assert_equal({ "a" => false }, JSON.parse('{ "a" : false } '))
|
80
|
+
assert_equal({ "a" => false }, JSON.parse('{"a":false}'))
|
81
|
+
assert_raise(JSON::ParserError) { JSON.parse('{false}') }
|
82
|
+
assert_equal({ "a" => true }, JSON.parse('{"a":true}'))
|
83
|
+
assert_equal({ "a" => true }, JSON.parse(' { "a" : true } '))
|
84
|
+
assert_equal({ "a" => -23 }, JSON.parse(' { "a" : -23 } '))
|
85
|
+
assert_equal({ "a" => -23 }, JSON.parse(' { "a" : -23 } '))
|
86
|
+
assert_equal({ "a" => 23 }, JSON.parse('{"a":23 } '))
|
87
|
+
assert_equal({ "a" => 23 }, JSON.parse(' { "a" : 23 } '))
|
88
|
+
assert_equal({ "a" => 0.23 }, JSON.parse(' { "a" : 0.23 } '))
|
89
|
+
assert_equal({ "a" => 0.23 }, JSON.parse(' { "a" : 0.23 } '))
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_parse_numbers
|
93
|
+
assert_raise(JSON::ParserError) { JSON.parse('+23.2') }
|
94
|
+
assert_raise(JSON::ParserError) { JSON.parse('+23') }
|
95
|
+
assert_raise(JSON::ParserError) { JSON.parse('.23') }
|
96
|
+
assert_raise(JSON::ParserError) { JSON.parse('023') }
|
97
|
+
assert_equal 23, JSON.parse('23')
|
98
|
+
assert_equal -23, JSON.parse('-23')
|
99
|
+
assert_equal_float 3.141, JSON.parse('3.141')
|
100
|
+
assert_equal_float -3.141, JSON.parse('-3.141')
|
101
|
+
assert_equal_float 3.141, JSON.parse('3141e-3')
|
102
|
+
assert_equal_float 3.141, JSON.parse('3141.1e-3')
|
103
|
+
assert_equal_float 3.141, JSON.parse('3141E-3')
|
104
|
+
assert_equal_float 3.141, JSON.parse('3141.0E-3')
|
105
|
+
assert_equal_float -3.141, JSON.parse('-3141.0e-3')
|
106
|
+
assert_equal_float -3.141, JSON.parse('-3141e-3')
|
107
|
+
assert_raise(JSON::ParserError) { JSON.parse('NaN') }
|
108
|
+
assert JSON.parse('NaN', :allow_nan => true).nan?
|
109
|
+
assert_raise(JSON::ParserError) { JSON.parse('Infinity') }
|
110
|
+
assert_equal 1.0/0, JSON.parse('Infinity', :allow_nan => true)
|
111
|
+
assert_raise(JSON::ParserError) { JSON.parse('-Infinity') }
|
112
|
+
assert_equal -1.0/0, JSON.parse('-Infinity', :allow_nan => true)
|
113
|
+
end
|
114
|
+
|
115
|
+
if Array.method_defined?(:permutation)
|
116
|
+
def test_parse_more_complex_arrays
|
117
|
+
a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
|
118
|
+
a.permutation.each do |perm|
|
119
|
+
json = JSON.pretty_generate(perm)
|
120
|
+
assert_equal perm, JSON.parse(json)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_parse_complex_objects
|
125
|
+
a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
|
126
|
+
a.permutation.each do |perm|
|
127
|
+
s = "a"
|
128
|
+
orig_obj = perm.inject({}) { |h, x| h[s.dup] = x; s = s.succ; h }
|
129
|
+
json = JSON.pretty_generate(orig_obj)
|
130
|
+
assert_equal orig_obj, JSON.parse(json)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_parse_arrays
|
136
|
+
assert_equal([1,2,3], JSON.parse('[1,2,3]'))
|
137
|
+
assert_equal([1.2,2,3], JSON.parse('[1.2,2,3]'))
|
138
|
+
assert_equal([[],[[],[]]], JSON.parse('[[],[[],[]]]'))
|
139
|
+
assert_equal([], JSON.parse('[]'))
|
140
|
+
assert_equal([], JSON.parse(' [ ] '))
|
141
|
+
assert_equal([1], JSON.parse('[1]'))
|
142
|
+
assert_equal([1], JSON.parse(' [ 1 ] '))
|
143
|
+
ary = [[1], ["foo"], [3.14], [4711.0], [2.718], [nil],
|
144
|
+
[[1, -2, 3]], [false], [true]]
|
145
|
+
assert_equal(ary,
|
146
|
+
JSON.parse('[[1],["foo"],[3.14],[47.11e+2],[2718.0E-3],[null],[[1,-2,3]],[false],[true]]'))
|
147
|
+
assert_equal(ary, JSON.parse(%Q{ [ [1] , ["foo"] , [3.14] \t , [47.11e+2]\s
|
148
|
+
, [2718.0E-3 ],\r[ null] , [[1, -2, 3 ]], [false ],[ true]\n ] }))
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_parse_json_primitive_values
|
152
|
+
assert_raise(JSON::ParserError) { JSON.parse('') }
|
153
|
+
assert_raise(TypeError) { JSON.parse(nil) }
|
154
|
+
assert_raise(JSON::ParserError) { JSON.parse(' /* foo */ ') }
|
155
|
+
assert_equal nil, JSON.parse('null')
|
156
|
+
assert_equal false, JSON.parse('false')
|
157
|
+
assert_equal true, JSON.parse('true')
|
158
|
+
assert_equal 23, JSON.parse('23')
|
159
|
+
assert_equal 1, JSON.parse('1')
|
160
|
+
assert_equal_float 3.141, JSON.parse('3.141'), 1E-3
|
161
|
+
assert_equal 2 ** 64, JSON.parse('18446744073709551616')
|
162
|
+
assert_equal 'foo', JSON.parse('"foo"')
|
163
|
+
assert JSON.parse('NaN', :allow_nan => true).nan?
|
164
|
+
assert JSON.parse('Infinity', :allow_nan => true).infinite?
|
165
|
+
assert JSON.parse('-Infinity', :allow_nan => true).infinite?
|
166
|
+
assert_raise(JSON::ParserError) { JSON.parse('[ 1, ]') }
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_parse_some_strings
|
170
|
+
assert_equal([""], JSON.parse('[""]'))
|
171
|
+
assert_equal(["\\"], JSON.parse('["\\\\"]'))
|
172
|
+
assert_equal(['"'], JSON.parse('["\""]'))
|
173
|
+
assert_equal(['\\"\\'], JSON.parse('["\\\\\\"\\\\"]'))
|
174
|
+
assert_equal(
|
175
|
+
["\"\b\n\r\t\0\037"],
|
176
|
+
JSON.parse('["\"\b\n\r\t\u0000\u001f"]')
|
177
|
+
)
|
178
|
+
end
|
179
|
+
|
180
|
+
def test_parse_big_integers
|
181
|
+
json1 = JSON(orig = (1 << 31) - 1)
|
182
|
+
assert_equal orig, JSON.parse(json1)
|
183
|
+
json2 = JSON(orig = 1 << 31)
|
184
|
+
assert_equal orig, JSON.parse(json2)
|
185
|
+
json3 = JSON(orig = (1 << 62) - 1)
|
186
|
+
assert_equal orig, JSON.parse(json3)
|
187
|
+
json4 = JSON(orig = 1 << 62)
|
188
|
+
assert_equal orig, JSON.parse(json4)
|
189
|
+
json5 = JSON(orig = 1 << 64)
|
190
|
+
assert_equal orig, JSON.parse(json5)
|
191
|
+
end
|
192
|
+
|
193
|
+
def test_some_wrong_inputs
|
194
|
+
assert_raise(JSON::ParserError) { JSON.parse('[] bla') }
|
195
|
+
assert_raise(JSON::ParserError) { JSON.parse('[] 1') }
|
196
|
+
assert_raise(JSON::ParserError) { JSON.parse('[] []') }
|
197
|
+
assert_raise(JSON::ParserError) { JSON.parse('[] {}') }
|
198
|
+
assert_raise(JSON::ParserError) { JSON.parse('{} []') }
|
199
|
+
assert_raise(JSON::ParserError) { JSON.parse('{} {}') }
|
200
|
+
assert_raise(JSON::ParserError) { JSON.parse('[NULL]') }
|
201
|
+
assert_raise(JSON::ParserError) { JSON.parse('[FALSE]') }
|
202
|
+
assert_raise(JSON::ParserError) { JSON.parse('[TRUE]') }
|
203
|
+
assert_raise(JSON::ParserError) { JSON.parse('[07] ') }
|
204
|
+
assert_raise(JSON::ParserError) { JSON.parse('[0a]') }
|
205
|
+
assert_raise(JSON::ParserError) { JSON.parse('[1.]') }
|
206
|
+
assert_raise(JSON::ParserError) { JSON.parse(' ') }
|
207
|
+
end
|
208
|
+
|
209
|
+
def test_symbolize_names
|
210
|
+
assert_equal({ "foo" => "bar", "baz" => "quux" },
|
211
|
+
JSON.parse('{"foo":"bar", "baz":"quux"}'))
|
212
|
+
assert_equal({ :foo => "bar", :baz => "quux" },
|
213
|
+
JSON.parse('{"foo":"bar", "baz":"quux"}', :symbolize_names => true))
|
214
|
+
assert_raise(ArgumentError) do
|
215
|
+
JSON.parse('{}', :symbolize_names => true, :create_additions => true)
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
def test_parse_comments
|
220
|
+
json = <<EOT
|
221
|
+
{
|
222
|
+
"key1":"value1", // eol comment
|
223
|
+
"key2":"value2" /* multi line
|
224
|
+
* comment */,
|
225
|
+
"key3":"value3" /* multi line
|
226
|
+
// nested eol comment
|
227
|
+
* comment */
|
228
|
+
}
|
229
|
+
EOT
|
230
|
+
assert_equal(
|
231
|
+
{ "key1" => "value1", "key2" => "value2", "key3" => "value3" },
|
232
|
+
JSON.parse(json))
|
233
|
+
json = <<EOT
|
234
|
+
{
|
235
|
+
"key1":"value1" /* multi line
|
236
|
+
// nested eol comment
|
237
|
+
/* illegal nested multi line comment */
|
238
|
+
* comment */
|
239
|
+
}
|
240
|
+
EOT
|
241
|
+
assert_raise(JSON::ParserError) { JSON.parse(json) }
|
242
|
+
json = <<EOT
|
243
|
+
{
|
244
|
+
"key1":"value1" /* multi line
|
245
|
+
// nested eol comment
|
246
|
+
closed multi comment */
|
247
|
+
and again, throw an Error */
|
248
|
+
}
|
249
|
+
EOT
|
250
|
+
assert_raise(JSON::ParserError) { JSON.parse(json) }
|
251
|
+
json = <<EOT
|
252
|
+
{
|
253
|
+
"key1":"value1" /*/*/
|
254
|
+
}
|
255
|
+
EOT
|
256
|
+
assert_equal({ "key1" => "value1" }, JSON.parse(json))
|
257
|
+
end
|
258
|
+
|
259
|
+
def test_nesting
|
260
|
+
too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
|
261
|
+
too_deep_ary = eval too_deep
|
262
|
+
assert_raise(JSON::NestingError) { JSON.parse too_deep }
|
263
|
+
assert_raise(JSON::NestingError) { JSON.parse too_deep, :max_nesting => 100 }
|
264
|
+
ok = JSON.parse too_deep, :max_nesting => 101
|
265
|
+
assert_equal too_deep_ary, ok
|
266
|
+
ok = JSON.parse too_deep, :max_nesting => nil
|
267
|
+
assert_equal too_deep_ary, ok
|
268
|
+
ok = JSON.parse too_deep, :max_nesting => false
|
269
|
+
assert_equal too_deep_ary, ok
|
270
|
+
ok = JSON.parse too_deep, :max_nesting => 0
|
271
|
+
assert_equal too_deep_ary, ok
|
272
|
+
end
|
273
|
+
|
274
|
+
def test_backslash
|
275
|
+
data = [ '\\.(?i:gif|jpe?g|png)$' ]
|
276
|
+
json = '["\\\\.(?i:gif|jpe?g|png)$"]'
|
277
|
+
assert_equal data, JSON.parse(json)
|
278
|
+
#
|
279
|
+
data = [ '\\"' ]
|
280
|
+
json = '["\\\\\""]'
|
281
|
+
assert_equal data, JSON.parse(json)
|
282
|
+
#
|
283
|
+
json = '["/"]'
|
284
|
+
data = [ '/' ]
|
285
|
+
assert_equal data, JSON.parse(json)
|
286
|
+
#
|
287
|
+
json = '["\""]'
|
288
|
+
data = ['"']
|
289
|
+
assert_equal data, JSON.parse(json)
|
290
|
+
#
|
291
|
+
json = '["\\\'"]'
|
292
|
+
data = ["'"]
|
293
|
+
assert_equal data, JSON.parse(json)
|
294
|
+
end
|
295
|
+
|
296
|
+
class SubArray < Array
|
297
|
+
def <<(v)
|
298
|
+
@shifted = true
|
299
|
+
super
|
300
|
+
end
|
301
|
+
|
302
|
+
def shifted?
|
303
|
+
@shifted
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
class SubArray2 < Array
|
308
|
+
def to_json(*a)
|
309
|
+
{
|
310
|
+
JSON.create_id => self.class.name,
|
311
|
+
'ary' => to_a,
|
312
|
+
}.to_json(*a)
|
313
|
+
end
|
314
|
+
|
315
|
+
def self.json_create(o)
|
316
|
+
o.delete JSON.create_id
|
317
|
+
o['ary']
|
318
|
+
end
|
319
|
+
end
|
320
|
+
|
321
|
+
class SubArrayWrapper
|
322
|
+
def initialize
|
323
|
+
@data = []
|
324
|
+
end
|
325
|
+
|
326
|
+
attr_reader :data
|
327
|
+
|
328
|
+
def [](index)
|
329
|
+
@data[index]
|
330
|
+
end
|
331
|
+
|
332
|
+
def <<(value)
|
333
|
+
@data << value
|
334
|
+
@shifted = true
|
335
|
+
end
|
336
|
+
|
337
|
+
def shifted?
|
338
|
+
@shifted
|
339
|
+
end
|
340
|
+
end
|
341
|
+
|
342
|
+
def test_parse_array_custom_array_derived_class
|
343
|
+
res = JSON.parse('[1,2]', :array_class => SubArray)
|
344
|
+
assert_equal([1,2], res)
|
345
|
+
assert_equal(SubArray, res.class)
|
346
|
+
assert res.shifted?
|
347
|
+
end
|
348
|
+
|
349
|
+
def test_parse_array_custom_non_array_derived_class
|
350
|
+
res = JSON.parse('[1,2]', :array_class => SubArrayWrapper)
|
351
|
+
assert_equal(SubArrayWrapper, res.class)
|
352
|
+
assert_equal([1,2], res.data)
|
353
|
+
assert res.shifted?
|
354
|
+
end
|
355
|
+
|
356
|
+
def test_parse_object
|
357
|
+
assert_equal({}, JSON.parse('{}'))
|
358
|
+
assert_equal({}, JSON.parse(' { } '))
|
359
|
+
assert_equal({'foo'=>'bar'}, JSON.parse('{"foo":"bar"}'))
|
360
|
+
assert_equal({'foo'=>'bar'}, JSON.parse(' { "foo" : "bar" } '))
|
361
|
+
end
|
362
|
+
|
363
|
+
class SubHash < Hash
|
364
|
+
def []=(k, v)
|
365
|
+
@item_set = true
|
366
|
+
super
|
367
|
+
end
|
368
|
+
|
369
|
+
def item_set?
|
370
|
+
@item_set
|
371
|
+
end
|
372
|
+
end
|
373
|
+
|
374
|
+
class SubHash2 < Hash
|
375
|
+
def to_json(*a)
|
376
|
+
{
|
377
|
+
JSON.create_id => self.class.name,
|
378
|
+
}.merge(self).to_json(*a)
|
379
|
+
end
|
380
|
+
|
381
|
+
def self.json_create(o)
|
382
|
+
o.delete JSON.create_id
|
383
|
+
self[o]
|
384
|
+
end
|
385
|
+
end
|
386
|
+
|
387
|
+
class SubOpenStruct < OpenStruct
|
388
|
+
def [](k)
|
389
|
+
__send__(k)
|
390
|
+
end
|
391
|
+
|
392
|
+
def []=(k, v)
|
393
|
+
@item_set = true
|
394
|
+
__send__("#{k}=", v)
|
395
|
+
end
|
396
|
+
|
397
|
+
def item_set?
|
398
|
+
@item_set
|
399
|
+
end
|
400
|
+
end
|
401
|
+
|
402
|
+
def test_parse_object_custom_hash_derived_class
|
403
|
+
res = JSON.parse('{"foo":"bar"}', :object_class => SubHash)
|
404
|
+
assert_equal(SubHash, res.class)
|
405
|
+
assert_equal({"foo" => "bar"}, res)
|
406
|
+
assert res.item_set?
|
407
|
+
end
|
408
|
+
|
409
|
+
def test_parse_object_custom_non_hash_derived_class
|
410
|
+
res = JSON.parse('{"foo":"bar"}', :object_class => SubOpenStruct)
|
411
|
+
assert_equal(SubOpenStruct, res.class)
|
412
|
+
assert_equal "bar", res.foo
|
413
|
+
assert res.item_set?
|
414
|
+
end
|
415
|
+
|
416
|
+
def test_parse_generic_object
|
417
|
+
res = JSON.parse(
|
418
|
+
'{"foo":"bar", "baz":{}}',
|
419
|
+
:object_class => JSON::GenericObject
|
420
|
+
)
|
421
|
+
assert_equal(JSON::GenericObject, res.class)
|
422
|
+
assert_equal "bar", res.foo
|
423
|
+
assert_equal "bar", res["foo"]
|
424
|
+
assert_equal "bar", res[:foo]
|
425
|
+
assert_equal "bar", res.to_hash[:foo]
|
426
|
+
assert_equal(JSON::GenericObject, res.baz.class)
|
427
|
+
end if defined?(JSON::GenericObject)
|
428
|
+
|
429
|
+
def test_generate_core_subclasses_with_new_to_json
|
430
|
+
obj = SubHash2["foo" => SubHash2["bar" => true]]
|
431
|
+
obj_json = JSON(obj)
|
432
|
+
obj_again = JSON.parse(obj_json, :create_additions => true)
|
433
|
+
assert_kind_of SubHash2, obj_again
|
434
|
+
assert_kind_of SubHash2, obj_again['foo']
|
435
|
+
assert obj_again['foo']['bar']
|
436
|
+
assert_equal obj, obj_again
|
437
|
+
assert_equal ["foo"],
|
438
|
+
JSON(JSON(SubArray2["foo"]), :create_additions => true)
|
439
|
+
end
|
440
|
+
|
441
|
+
def test_generate_core_subclasses_with_default_to_json
|
442
|
+
assert_equal '{"foo":"bar"}', JSON(SubHash["foo" => "bar"])
|
443
|
+
assert_equal '["foo"]', JSON(SubArray["foo"])
|
444
|
+
end
|
445
|
+
|
446
|
+
def test_generate_of_core_subclasses
|
447
|
+
obj = SubHash["foo" => SubHash["bar" => true]]
|
448
|
+
obj_json = JSON(obj)
|
449
|
+
obj_again = JSON(obj_json)
|
450
|
+
assert_kind_of Hash, obj_again
|
451
|
+
assert_kind_of Hash, obj_again['foo']
|
452
|
+
assert obj_again['foo']['bar']
|
453
|
+
assert_equal obj, obj_again
|
454
|
+
end
|
455
|
+
|
456
|
+
def test_parsing_frozen_ascii8bit_string
|
457
|
+
assert_equal(
|
458
|
+
{ 'foo' => 'bar' },
|
459
|
+
JSON('{ "foo": "bar" }'.force_encoding(Encoding::ASCII_8BIT).freeze)
|
460
|
+
)
|
461
|
+
end
|
462
|
+
|
463
|
+
private
|
464
|
+
|
465
|
+
def assert_equal_float(expected, actual, delta = 1e-2)
|
466
|
+
Array === expected and expected = expected.first
|
467
|
+
Array === actual and actual = actual.first
|
468
|
+
assert_in_delta(expected, actual, delta)
|
469
|
+
end
|
470
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
#frozen_string_literal: false
|
4
|
+
|
5
|
+
require 'json_gem/test_helper'
|
6
|
+
|
7
|
+
require 'time'
|
8
|
+
|
9
|
+
class JSONStringMatchingTest < Test::Unit::TestCase
|
10
|
+
include Test::Unit::TestCaseOmissionSupport
|
11
|
+
|
12
|
+
class TestTime < ::Time
|
13
|
+
def self.json_create(string)
|
14
|
+
Time.parse(string)
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_json(*)
|
18
|
+
%{"#{strftime('%FT%T%z')}"}
|
19
|
+
end
|
20
|
+
|
21
|
+
def ==(other)
|
22
|
+
to_i == other.to_i
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_match_date
|
27
|
+
t = TestTime.new
|
28
|
+
t_json = [ t ].to_json
|
29
|
+
time_regexp = /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}\z/
|
30
|
+
assert_equal [ t ],
|
31
|
+
JSON.parse(
|
32
|
+
t_json,
|
33
|
+
:create_additions => true,
|
34
|
+
:match_string => { time_regexp => TestTime }
|
35
|
+
)
|
36
|
+
assert_equal [ t.strftime('%FT%T%z') ],
|
37
|
+
JSON.parse(
|
38
|
+
t_json,
|
39
|
+
:match_string => { time_regexp => TestTime }
|
40
|
+
)
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
$: << File.dirname(__FILE__)
|
4
|
+
$oj_dir = File.dirname(File.dirname(File.expand_path(File.dirname(__FILE__))))
|
5
|
+
%w(lib ext).each do |dir|
|
6
|
+
$: << File.join($oj_dir, dir)
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'test/unit'
|
10
|
+
REAL_JSON_GEM = !!ENV['REAL_JSON_GEM']
|
11
|
+
|
12
|
+
if ENV['REAL_JSON_GEM']
|
13
|
+
require 'json'
|
14
|
+
else
|
15
|
+
require 'oj'
|
16
|
+
Oj.mimic_JSON
|
17
|
+
|
18
|
+
if defined?(GC.verify_compaction_references) == 'method'
|
19
|
+
# This method was added in Ruby 3.0.0. Calling it this way asks the GC to
|
20
|
+
# move objects around, helping to find object movement bugs.
|
21
|
+
GC.verify_compaction_references(double_heap: true, toward: :empty)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
NaN = JSON::NaN if defined?(JSON::NaN)
|
26
|
+
NaN = 0.0/0 unless defined?(NaN)
|
data/test/mem.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
$: << '.'
|
5
|
+
$: << File.join(File.dirname(__FILE__), "../lib")
|
6
|
+
$: << File.join(File.dirname(__FILE__), "../ext")
|
7
|
+
|
8
|
+
require 'oj'
|
9
|
+
|
10
|
+
Oj.default_options = { mode: :rails, cache_keys: false, cache_str: -1 }
|
11
|
+
|
12
|
+
def mem
|
13
|
+
`ps -o rss= -p #{$$}`.to_i
|
14
|
+
end
|
15
|
+
|
16
|
+
('a'..'z').each { |a|
|
17
|
+
('a'..'z').each { |b|
|
18
|
+
('a'..'z').each { |c|
|
19
|
+
('a'..'z').each { |d|
|
20
|
+
('a'..'z').each { |e|
|
21
|
+
('a'..'z').each { |f|
|
22
|
+
key = "#{a}#{b}#{c}#{d}#{e}#{f}"
|
23
|
+
x = Oj.load(%|{ "#{key}": 101}|)
|
24
|
+
#Oj.dump(x)
|
25
|
+
}
|
26
|
+
}
|
27
|
+
}
|
28
|
+
}
|
29
|
+
puts "#{a}#{b} #{mem}"
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
Oj::Parser.new(:usual)
|
data/test/perf.rb
CHANGED
data/test/perf_compat.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
1
|
+
#!/usr/bin/env ruby
|
2
2
|
# encoding: UTF-8
|
3
3
|
|
4
4
|
$: << '.'
|
@@ -12,8 +12,6 @@ require 'oj'
|
|
12
12
|
$verbose = false
|
13
13
|
$indent = 0
|
14
14
|
$iter = 20000
|
15
|
-
$with_bignum = false
|
16
|
-
$with_nums = true
|
17
15
|
$size = 0
|
18
16
|
|
19
17
|
opts = OptionParser.new
|
@@ -24,25 +22,48 @@ opts.on("-s", "--size [Int]", Integer, "size (~Kbytes)") { |i| $size = i }
|
|
24
22
|
opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
|
25
23
|
files = opts.parse(ARGV)
|
26
24
|
|
25
|
+
def capture_error(tag, orig, load_key, dump_key, &blk)
|
26
|
+
begin
|
27
|
+
obj = blk.call(orig)
|
28
|
+
puts obj unless orig == obj
|
29
|
+
raise "#{tag} #{dump_key} and #{load_key} did not return the same object as the original." unless orig == obj
|
30
|
+
rescue Exception => e
|
31
|
+
$failed[tag] = "#{e.class}: #{e.message}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Verify that all packages dump and load correctly and return the same Object as the original.
|
36
|
+
capture_error('Oj:compat', $obj, 'load', 'dump') { |o| Oj.compat_load(Oj.dump(o, :mode => :compat)) }
|
37
|
+
capture_error('JSON::Ext', $obj, 'generate', 'parse') { |o|
|
38
|
+
require 'json'
|
39
|
+
require 'json/ext'
|
40
|
+
JSON.generator = JSON::Ext::Generator
|
41
|
+
JSON.parser = JSON::Ext::Parser
|
42
|
+
JSON.load(JSON.generate(o))
|
43
|
+
}
|
44
|
+
|
27
45
|
module One
|
28
46
|
module Two
|
29
47
|
module Three
|
30
48
|
class Empty
|
31
49
|
|
32
50
|
def initialize()
|
51
|
+
@a = 1
|
52
|
+
@b = 2
|
53
|
+
@c = 3
|
33
54
|
end
|
34
55
|
|
35
56
|
def eql?(o)
|
36
|
-
self.class == o.class
|
57
|
+
self.class == o.class && @a == o.a && @b = o.b && @c = o.c
|
37
58
|
end
|
38
59
|
alias == eql?
|
39
60
|
|
40
|
-
def
|
41
|
-
{
|
61
|
+
def as_json(*a)
|
62
|
+
{JSON.create_id => self.class.name, 'a' => @a, 'b' => @b, 'c' => @c }
|
42
63
|
end
|
43
|
-
|
64
|
+
|
44
65
|
def to_json(*a)
|
45
|
-
|
66
|
+
JSON.generate(as_json())
|
46
67
|
end
|
47
68
|
|
48
69
|
def self.json_create(h)
|
@@ -65,7 +86,7 @@ $obj = {
|
|
65
86
|
'i' => [[[[[[[nil]]]]]]] # deep array, again, not that deep
|
66
87
|
}
|
67
88
|
|
68
|
-
Oj.default_options = { :indent => $indent, :mode => :compat }
|
89
|
+
Oj.default_options = { :indent => $indent, :mode => :compat, :use_to_json => true, :create_additions => true, :create_id => '^o' }
|
69
90
|
|
70
91
|
if 0 < $size
|
71
92
|
s = Oj.dump($obj).size + 1
|
@@ -80,25 +101,6 @@ end
|
|
80
101
|
$json = Oj.dump($obj)
|
81
102
|
$failed = {} # key is same as String used in tests later
|
82
103
|
|
83
|
-
def capture_error(tag, orig, load_key, dump_key, &blk)
|
84
|
-
begin
|
85
|
-
obj = blk.call(orig)
|
86
|
-
raise "#{tag} #{dump_key} and #{load_key} did not return the same object as the original." unless orig == obj
|
87
|
-
rescue Exception => e
|
88
|
-
$failed[tag] = "#{e.class}: #{e.message}"
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
# Verify that all packages dump and load correctly and return the same Object as the original.
|
93
|
-
capture_error('Oj:compat', $obj, 'load', 'dump') { |o| Oj.compat_load(Oj.dump(o, :mode => :compat)) }
|
94
|
-
capture_error('JSON::Ext', $obj, 'generate', 'parse') { |o|
|
95
|
-
require 'json'
|
96
|
-
require 'json/ext'
|
97
|
-
JSON.generator = JSON::Ext::Generator
|
98
|
-
JSON.parser = JSON::Ext::Parser
|
99
|
-
JSON.load(JSON.generate(o))
|
100
|
-
}
|
101
|
-
|
102
104
|
if $verbose
|
103
105
|
puts "size: #{$json.size}"
|
104
106
|
puts "json:\n#{$json}\n"
|