json 1.8.6 → 2.7.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/{CHANGES → CHANGES.md} +292 -96
- data/LICENSE +56 -0
- data/README.md +185 -114
- data/ext/json/ext/fbuffer/fbuffer.h +0 -3
- data/ext/json/ext/generator/generator.c +328 -117
- data/ext/json/ext/generator/generator.h +8 -8
- data/ext/json/ext/parser/extconf.rb +29 -0
- data/ext/json/ext/parser/parser.c +540 -569
- data/ext/json/ext/parser/parser.h +10 -6
- data/ext/json/ext/parser/parser.rl +269 -261
- data/ext/json/extconf.rb +1 -1
- data/json.gemspec +0 -0
- data/lib/json/add/bigdecimal.rb +40 -10
- data/lib/json/add/complex.rb +32 -9
- data/lib/json/add/core.rb +1 -0
- data/lib/json/add/date.rb +27 -7
- data/lib/json/add/date_time.rb +26 -9
- data/lib/json/add/exception.rb +25 -7
- data/lib/json/add/ostruct.rb +32 -9
- data/lib/json/add/range.rb +33 -8
- data/lib/json/add/rational.rb +30 -8
- data/lib/json/add/regexp.rb +28 -10
- data/lib/json/add/set.rb +48 -0
- data/lib/json/add/struct.rb +29 -7
- data/lib/json/add/symbol.rb +28 -5
- data/lib/json/add/time.rb +27 -6
- data/lib/json/common.rb +402 -188
- data/lib/json/ext.rb +0 -6
- data/lib/json/generic_object.rb +11 -6
- data/lib/json/pure/generator.rb +120 -137
- data/lib/json/pure/parser.rb +64 -86
- data/lib/json/pure.rb +2 -8
- data/lib/json/version.rb +2 -1
- data/lib/json.rb +559 -29
- metadata +18 -129
- data/.gitignore +0 -17
- data/.travis.yml +0 -18
- data/Gemfile +0 -7
- data/README-json-jruby.markdown +0 -33
- data/Rakefile +0 -402
- data/TODO +0 -1
- data/VERSION +0 -1
- data/data/example.json +0 -1
- data/data/index.html +0 -38
- data/data/prototype.js +0 -4184
- data/diagrams/.keep +0 -0
- data/install.rb +0 -23
- data/java/src/json/ext/ByteListTranscoder.java +0 -166
- data/java/src/json/ext/Generator.java +0 -446
- data/java/src/json/ext/GeneratorMethods.java +0 -231
- data/java/src/json/ext/GeneratorService.java +0 -42
- data/java/src/json/ext/GeneratorState.java +0 -542
- data/java/src/json/ext/OptionsReader.java +0 -113
- data/java/src/json/ext/Parser.java +0 -2644
- data/java/src/json/ext/Parser.rl +0 -968
- data/java/src/json/ext/ParserService.java +0 -34
- data/java/src/json/ext/RuntimeInfo.java +0 -120
- data/java/src/json/ext/StringDecoder.java +0 -166
- data/java/src/json/ext/StringEncoder.java +0 -111
- data/java/src/json/ext/Utils.java +0 -88
- data/json-java.gemspec +0 -38
- data/json_pure.gemspec +0 -37
- data/lib/json/ext/.keep +0 -0
- data/tests/fixtures/fail1.json +0 -1
- data/tests/fixtures/fail10.json +0 -1
- data/tests/fixtures/fail11.json +0 -1
- data/tests/fixtures/fail12.json +0 -1
- data/tests/fixtures/fail13.json +0 -1
- data/tests/fixtures/fail14.json +0 -1
- data/tests/fixtures/fail18.json +0 -1
- data/tests/fixtures/fail19.json +0 -1
- data/tests/fixtures/fail2.json +0 -1
- data/tests/fixtures/fail20.json +0 -1
- data/tests/fixtures/fail21.json +0 -1
- data/tests/fixtures/fail22.json +0 -1
- data/tests/fixtures/fail23.json +0 -1
- data/tests/fixtures/fail24.json +0 -1
- data/tests/fixtures/fail25.json +0 -1
- data/tests/fixtures/fail27.json +0 -2
- data/tests/fixtures/fail28.json +0 -2
- data/tests/fixtures/fail3.json +0 -1
- data/tests/fixtures/fail4.json +0 -1
- data/tests/fixtures/fail5.json +0 -1
- data/tests/fixtures/fail6.json +0 -1
- data/tests/fixtures/fail7.json +0 -1
- data/tests/fixtures/fail8.json +0 -1
- data/tests/fixtures/fail9.json +0 -1
- data/tests/fixtures/pass1.json +0 -56
- data/tests/fixtures/pass15.json +0 -1
- data/tests/fixtures/pass16.json +0 -1
- data/tests/fixtures/pass17.json +0 -1
- data/tests/fixtures/pass2.json +0 -1
- data/tests/fixtures/pass26.json +0 -1
- data/tests/fixtures/pass3.json +0 -6
- data/tests/setup_variant.rb +0 -11
- data/tests/test_json.rb +0 -519
- data/tests/test_json_addition.rb +0 -196
- data/tests/test_json_encoding.rb +0 -65
- data/tests/test_json_fixtures.rb +0 -35
- data/tests/test_json_generate.rb +0 -348
- data/tests/test_json_generic_object.rb +0 -75
- data/tests/test_json_string_matching.rb +0 -39
- data/tests/test_json_unicode.rb +0 -72
- data/tools/diff.sh +0 -18
- data/tools/fuzz.rb +0 -139
- data/tools/server.rb +0 -62
data/tests/fixtures/fail4.json
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
["extra comma",]
|
data/tests/fixtures/fail5.json
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
["double extra comma",,]
|
data/tests/fixtures/fail6.json
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
[ , "<-- missing value"]
|
data/tests/fixtures/fail7.json
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
["Comma after the close"],
|
data/tests/fixtures/fail8.json
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
["Extra close"]]
|
data/tests/fixtures/fail9.json
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"Extra comma": true,}
|
data/tests/fixtures/pass1.json
DELETED
@@ -1,56 +0,0 @@
|
|
1
|
-
[
|
2
|
-
"JSON Test Pattern pass1",
|
3
|
-
{"object with 1 member":["array with 1 element"]},
|
4
|
-
{},
|
5
|
-
[],
|
6
|
-
-42,
|
7
|
-
true,
|
8
|
-
false,
|
9
|
-
null,
|
10
|
-
{
|
11
|
-
"integer": 1234567890,
|
12
|
-
"real": -9876.543210,
|
13
|
-
"e": 0.123456789e-12,
|
14
|
-
"E": 1.234567890E+34,
|
15
|
-
"": 23456789012E666,
|
16
|
-
"zero": 0,
|
17
|
-
"one": 1,
|
18
|
-
"space": " ",
|
19
|
-
"quote": "\"",
|
20
|
-
"backslash": "\\",
|
21
|
-
"controls": "\b\f\n\r\t",
|
22
|
-
"slash": "/ & \/",
|
23
|
-
"alpha": "abcdefghijklmnopqrstuvwyz",
|
24
|
-
"ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
|
25
|
-
"digit": "0123456789",
|
26
|
-
"special": "`1~!@#$%^&*()_+-={':[,]}|;.</>?",
|
27
|
-
"hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A",
|
28
|
-
"true": true,
|
29
|
-
"false": false,
|
30
|
-
"null": null,
|
31
|
-
"array":[ ],
|
32
|
-
"object":{ },
|
33
|
-
"address": "50 St. James Street",
|
34
|
-
"url": "http://www.JSON.org/",
|
35
|
-
"comment": "// /* <!-- --",
|
36
|
-
"# -- --> */": " ",
|
37
|
-
" s p a c e d " :[1,2 , 3
|
38
|
-
|
39
|
-
,
|
40
|
-
|
41
|
-
4 , 5 , 6 ,7 ],
|
42
|
-
"compact": [1,2,3,4,5,6,7],
|
43
|
-
"jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}",
|
44
|
-
"quotes": "" \u0022 %22 0x22 034 "",
|
45
|
-
"\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"
|
46
|
-
: "A key can be any string"
|
47
|
-
},
|
48
|
-
0.5 ,98.6
|
49
|
-
,
|
50
|
-
99.44
|
51
|
-
,
|
52
|
-
|
53
|
-
1066
|
54
|
-
|
55
|
-
|
56
|
-
,"rosebud"]
|
data/tests/fixtures/pass15.json
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
["Illegal backslash escape: \x15"]
|
data/tests/fixtures/pass16.json
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
["Illegal backslash escape: \'"]
|
data/tests/fixtures/pass17.json
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
["Illegal backslash escape: \017"]
|
data/tests/fixtures/pass2.json
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]
|
data/tests/fixtures/pass26.json
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
["tab\ character\ in\ string\ "]
|
data/tests/fixtures/pass3.json
DELETED
data/tests/setup_variant.rb
DELETED
data/tests/test_json.rb
DELETED
@@ -1,519 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# encoding: utf-8
|
3
|
-
|
4
|
-
require 'test/unit'
|
5
|
-
require File.join(File.dirname(__FILE__), 'setup_variant')
|
6
|
-
require 'stringio'
|
7
|
-
require 'tempfile'
|
8
|
-
require 'ostruct'
|
9
|
-
|
10
|
-
class TestJSON < Test::Unit::TestCase
|
11
|
-
include JSON
|
12
|
-
|
13
|
-
def setup
|
14
|
-
@ary = [1, "foo", 3.14, 4711.0, 2.718, nil, [1,-2,3], false, true].map do
|
15
|
-
|x| [x]
|
16
|
-
end
|
17
|
-
@ary_to_parse = ["1", '"foo"', "3.14", "4711.0", "2.718", "null",
|
18
|
-
"[1,-2,3]", "false", "true"].map do
|
19
|
-
|x| "[#{x}]"
|
20
|
-
end
|
21
|
-
@hash = {
|
22
|
-
'a' => 2,
|
23
|
-
'b' => 3.141,
|
24
|
-
'c' => 'c',
|
25
|
-
'd' => [ 1, "b", 3.14 ],
|
26
|
-
'e' => { 'foo' => 'bar' },
|
27
|
-
'g' => "\"\0\037",
|
28
|
-
'h' => 1000.0,
|
29
|
-
'i' => 0.001
|
30
|
-
}
|
31
|
-
@json = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},'\
|
32
|
-
'"g":"\\"\\u0000\\u001f","h":1.0E3,"i":1.0E-3}'
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_construction
|
36
|
-
parser = JSON::Parser.new('test')
|
37
|
-
assert_equal 'test', parser.source
|
38
|
-
end
|
39
|
-
|
40
|
-
def assert_equal_float(expected, is)
|
41
|
-
assert_in_delta(expected.first, is.first, 1e-2)
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_parse_simple_arrays
|
45
|
-
assert_equal([], parse('[]'))
|
46
|
-
assert_equal([], parse(' [ ] '))
|
47
|
-
assert_equal([nil], parse('[null]'))
|
48
|
-
assert_equal([false], parse('[false]'))
|
49
|
-
assert_equal([true], parse('[true]'))
|
50
|
-
assert_equal([-23], parse('[-23]'))
|
51
|
-
assert_equal([23], parse('[23]'))
|
52
|
-
assert_equal([0.23], parse('[0.23]'))
|
53
|
-
assert_equal([0.0], parse('[0e0]'))
|
54
|
-
assert_raises(JSON::ParserError) { parse('[+23.2]') }
|
55
|
-
assert_raises(JSON::ParserError) { parse('[+23]') }
|
56
|
-
assert_raises(JSON::ParserError) { parse('[.23]') }
|
57
|
-
assert_raises(JSON::ParserError) { parse('[023]') }
|
58
|
-
assert_equal_float [3.141], parse('[3.141]')
|
59
|
-
assert_equal_float [-3.141], parse('[-3.141]')
|
60
|
-
assert_equal_float [3.141], parse('[3141e-3]')
|
61
|
-
assert_equal_float [3.141], parse('[3141.1e-3]')
|
62
|
-
assert_equal_float [3.141], parse('[3141E-3]')
|
63
|
-
assert_equal_float [3.141], parse('[3141.0E-3]')
|
64
|
-
assert_equal_float [-3.141], parse('[-3141.0e-3]')
|
65
|
-
assert_equal_float [-3.141], parse('[-3141e-3]')
|
66
|
-
assert_raises(ParserError) { parse('[NaN]') }
|
67
|
-
assert parse('[NaN]', :allow_nan => true).first.nan?
|
68
|
-
assert_raises(ParserError) { parse('[Infinity]') }
|
69
|
-
assert_equal [1.0/0], parse('[Infinity]', :allow_nan => true)
|
70
|
-
assert_raises(ParserError) { parse('[-Infinity]') }
|
71
|
-
assert_equal [-1.0/0], parse('[-Infinity]', :allow_nan => true)
|
72
|
-
assert_equal([""], parse('[""]'))
|
73
|
-
assert_equal(["foobar"], parse('["foobar"]'))
|
74
|
-
assert_equal([{}], parse('[{}]'))
|
75
|
-
end
|
76
|
-
|
77
|
-
def test_parse_simple_objects
|
78
|
-
assert_equal({}, parse('{}'))
|
79
|
-
assert_equal({}, parse(' { } '))
|
80
|
-
assert_equal({ "a" => nil }, parse('{ "a" : null}'))
|
81
|
-
assert_equal({ "a" => nil }, parse('{"a":null}'))
|
82
|
-
assert_equal({ "a" => false }, parse('{ "a" : false } '))
|
83
|
-
assert_equal({ "a" => false }, parse('{"a":false}'))
|
84
|
-
assert_raises(JSON::ParserError) { parse('{false}') }
|
85
|
-
assert_equal({ "a" => true }, parse('{"a":true}'))
|
86
|
-
assert_equal({ "a" => true }, parse(' { "a" : true } '))
|
87
|
-
assert_equal({ "a" => -23 }, parse(' { "a" : -23 } '))
|
88
|
-
assert_equal({ "a" => -23 }, parse(' { "a" : -23 } '))
|
89
|
-
assert_equal({ "a" => 23 }, parse('{"a":23 } '))
|
90
|
-
assert_equal({ "a" => 23 }, parse(' { "a" : 23 } '))
|
91
|
-
assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
|
92
|
-
assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
|
93
|
-
end
|
94
|
-
|
95
|
-
def test_parse_json_primitive_values
|
96
|
-
assert_raise(JSON::ParserError) { JSON.parse('') }
|
97
|
-
assert_raise(JSON::ParserError) { JSON.parse('', :quirks_mode => true) }
|
98
|
-
assert_raise(TypeError) { JSON::Parser.new(nil).parse }
|
99
|
-
assert_raise(TypeError) { JSON::Parser.new(nil, :quirks_mode => true).parse }
|
100
|
-
assert_raise(TypeError) { JSON.parse(nil) }
|
101
|
-
assert_raise(TypeError) { JSON.parse(nil, :quirks_mode => true) }
|
102
|
-
assert_raise(JSON::ParserError) { JSON.parse(' /* foo */ ') }
|
103
|
-
assert_raise(JSON::ParserError) { JSON.parse(' /* foo */ ', :quirks_mode => true) }
|
104
|
-
parser = JSON::Parser.new('null')
|
105
|
-
assert_equal false, parser.quirks_mode?
|
106
|
-
assert_raise(JSON::ParserError) { parser.parse }
|
107
|
-
assert_raise(JSON::ParserError) { JSON.parse('null') }
|
108
|
-
assert_equal nil, JSON.parse('null', :quirks_mode => true)
|
109
|
-
parser = JSON::Parser.new('null', :quirks_mode => true)
|
110
|
-
assert_equal true, parser.quirks_mode?
|
111
|
-
assert_equal nil, parser.parse
|
112
|
-
assert_raise(JSON::ParserError) { JSON.parse('false') }
|
113
|
-
assert_equal false, JSON.parse('false', :quirks_mode => true)
|
114
|
-
assert_raise(JSON::ParserError) { JSON.parse('true') }
|
115
|
-
assert_equal true, JSON.parse('true', :quirks_mode => true)
|
116
|
-
assert_raise(JSON::ParserError) { JSON.parse('23') }
|
117
|
-
assert_equal 23, JSON.parse('23', :quirks_mode => true)
|
118
|
-
assert_raise(JSON::ParserError) { JSON.parse('1') }
|
119
|
-
assert_equal 1, JSON.parse('1', :quirks_mode => true)
|
120
|
-
assert_raise(JSON::ParserError) { JSON.parse('3.141') }
|
121
|
-
assert_in_delta 3.141, JSON.parse('3.141', :quirks_mode => true), 1E-3
|
122
|
-
assert_raise(JSON::ParserError) { JSON.parse('18446744073709551616') }
|
123
|
-
assert_equal 2 ** 64, JSON.parse('18446744073709551616', :quirks_mode => true)
|
124
|
-
assert_raise(JSON::ParserError) { JSON.parse('"foo"') }
|
125
|
-
assert_equal 'foo', JSON.parse('"foo"', :quirks_mode => true)
|
126
|
-
assert_raise(JSON::ParserError) { JSON.parse('NaN', :allow_nan => true) }
|
127
|
-
assert JSON.parse('NaN', :quirks_mode => true, :allow_nan => true).nan?
|
128
|
-
assert_raise(JSON::ParserError) { JSON.parse('Infinity', :allow_nan => true) }
|
129
|
-
assert JSON.parse('Infinity', :quirks_mode => true, :allow_nan => true).infinite?
|
130
|
-
assert_raise(JSON::ParserError) { JSON.parse('-Infinity', :allow_nan => true) }
|
131
|
-
assert JSON.parse('-Infinity', :quirks_mode => true, :allow_nan => true).infinite?
|
132
|
-
assert_raise(JSON::ParserError) { JSON.parse('[ 1, ]', :quirks_mode => true) }
|
133
|
-
end
|
134
|
-
|
135
|
-
def test_parse_arrays
|
136
|
-
assert_equal([1,2,3], parse('[1,2,3]'))
|
137
|
-
assert_equal([1.2,2,3], parse('[1.2,2,3]'))
|
138
|
-
assert_equal([[],[[],[]]], parse('[[],[[],[]]]'))
|
139
|
-
end
|
140
|
-
|
141
|
-
def test_parse_values
|
142
|
-
assert_equal([""], parse('[""]'))
|
143
|
-
assert_equal(["\\"], parse('["\\\\"]'))
|
144
|
-
assert_equal(['"'], parse('["\""]'))
|
145
|
-
assert_equal(['\\"\\'], parse('["\\\\\\"\\\\"]'))
|
146
|
-
assert_equal(["\"\b\n\r\t\0\037"],
|
147
|
-
parse('["\"\b\n\r\t\u0000\u001f"]'))
|
148
|
-
for i in 0 ... @ary.size
|
149
|
-
assert_equal(@ary[i], parse(@ary_to_parse[i]))
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
def test_parse_array
|
154
|
-
assert_equal([], parse('[]'))
|
155
|
-
assert_equal([], parse(' [ ] '))
|
156
|
-
assert_equal([1], parse('[1]'))
|
157
|
-
assert_equal([1], parse(' [ 1 ] '))
|
158
|
-
assert_equal(@ary,
|
159
|
-
parse('[[1],["foo"],[3.14],[47.11e+2],[2718.0E-3],[null],[[1,-2,3]]'\
|
160
|
-
',[false],[true]]'))
|
161
|
-
assert_equal(@ary, parse(%Q{ [ [1] , ["foo"] , [3.14] \t , [47.11e+2]\s
|
162
|
-
, [2718.0E-3 ],\r[ null] , [[1, -2, 3 ]], [false ],[ true]\n ] }))
|
163
|
-
end
|
164
|
-
|
165
|
-
class SubArray < Array
|
166
|
-
def <<(v)
|
167
|
-
@shifted = true
|
168
|
-
super
|
169
|
-
end
|
170
|
-
|
171
|
-
def shifted?
|
172
|
-
@shifted
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
class SubArray2 < Array
|
177
|
-
def to_json(*a)
|
178
|
-
{
|
179
|
-
JSON.create_id => self.class.name,
|
180
|
-
'ary' => to_a,
|
181
|
-
}.to_json(*a)
|
182
|
-
end
|
183
|
-
|
184
|
-
def self.json_create(o)
|
185
|
-
o.delete JSON.create_id
|
186
|
-
o['ary']
|
187
|
-
end
|
188
|
-
end
|
189
|
-
|
190
|
-
class SubArrayWrapper
|
191
|
-
def initialize
|
192
|
-
@data = []
|
193
|
-
end
|
194
|
-
|
195
|
-
attr_reader :data
|
196
|
-
|
197
|
-
def [](index)
|
198
|
-
@data[index]
|
199
|
-
end
|
200
|
-
|
201
|
-
def <<(value)
|
202
|
-
@data << value
|
203
|
-
@shifted = true
|
204
|
-
end
|
205
|
-
|
206
|
-
def shifted?
|
207
|
-
@shifted
|
208
|
-
end
|
209
|
-
end
|
210
|
-
|
211
|
-
def test_parse_array_custom_array_derived_class
|
212
|
-
res = parse('[1,2]', :array_class => SubArray)
|
213
|
-
assert_equal([1,2], res)
|
214
|
-
assert_equal(SubArray, res.class)
|
215
|
-
assert res.shifted?
|
216
|
-
end
|
217
|
-
|
218
|
-
def test_parse_array_custom_non_array_derived_class
|
219
|
-
res = parse('[1,2]', :array_class => SubArrayWrapper)
|
220
|
-
assert_equal([1,2], res.data)
|
221
|
-
assert_equal(SubArrayWrapper, res.class)
|
222
|
-
assert res.shifted?
|
223
|
-
end
|
224
|
-
|
225
|
-
def test_parse_object
|
226
|
-
assert_equal({}, parse('{}'))
|
227
|
-
assert_equal({}, parse(' { } '))
|
228
|
-
assert_equal({'foo'=>'bar'}, parse('{"foo":"bar"}'))
|
229
|
-
assert_equal({'foo'=>'bar'}, parse(' { "foo" : "bar" } '))
|
230
|
-
end
|
231
|
-
|
232
|
-
class SubHash < Hash
|
233
|
-
def []=(k, v)
|
234
|
-
@item_set = true
|
235
|
-
super
|
236
|
-
end
|
237
|
-
|
238
|
-
def item_set?
|
239
|
-
@item_set
|
240
|
-
end
|
241
|
-
end
|
242
|
-
|
243
|
-
class SubHash2 < Hash
|
244
|
-
def to_json(*a)
|
245
|
-
{
|
246
|
-
JSON.create_id => self.class.name,
|
247
|
-
}.merge(self).to_json(*a)
|
248
|
-
end
|
249
|
-
|
250
|
-
def self.json_create(o)
|
251
|
-
o.delete JSON.create_id
|
252
|
-
self[o]
|
253
|
-
end
|
254
|
-
end
|
255
|
-
|
256
|
-
class SubOpenStruct < OpenStruct
|
257
|
-
def [](k)
|
258
|
-
__send__(k)
|
259
|
-
end
|
260
|
-
|
261
|
-
def []=(k, v)
|
262
|
-
@item_set = true
|
263
|
-
__send__("#{k}=", v)
|
264
|
-
end
|
265
|
-
|
266
|
-
def item_set?
|
267
|
-
@item_set
|
268
|
-
end
|
269
|
-
end
|
270
|
-
|
271
|
-
def test_parse_object_custom_hash_derived_class
|
272
|
-
res = parse('{"foo":"bar"}', :object_class => SubHash)
|
273
|
-
assert_equal({"foo" => "bar"}, res)
|
274
|
-
assert_equal(SubHash, res.class)
|
275
|
-
assert res.item_set?
|
276
|
-
end
|
277
|
-
|
278
|
-
def test_parse_object_custom_non_hash_derived_class
|
279
|
-
res = parse('{"foo":"bar"}', :object_class => SubOpenStruct)
|
280
|
-
assert_equal "bar", res.foo
|
281
|
-
assert_equal(SubOpenStruct, res.class)
|
282
|
-
assert res.item_set?
|
283
|
-
end
|
284
|
-
|
285
|
-
def test_parse_generic_object
|
286
|
-
res = parse('{"foo":"bar", "baz":{}}', :object_class => JSON::GenericObject)
|
287
|
-
assert_equal(JSON::GenericObject, res.class)
|
288
|
-
assert_equal "bar", res.foo
|
289
|
-
assert_equal "bar", res["foo"]
|
290
|
-
assert_equal "bar", res[:foo]
|
291
|
-
assert_equal "bar", res.to_hash[:foo]
|
292
|
-
assert_equal(JSON::GenericObject, res.baz.class)
|
293
|
-
end
|
294
|
-
|
295
|
-
def test_generate_core_subclasses_with_new_to_json
|
296
|
-
obj = SubHash2["foo" => SubHash2["bar" => true]]
|
297
|
-
obj_json = JSON(obj)
|
298
|
-
obj_again = JSON.parse(obj_json, :create_additions => true)
|
299
|
-
assert_kind_of SubHash2, obj_again
|
300
|
-
assert_kind_of SubHash2, obj_again['foo']
|
301
|
-
assert obj_again['foo']['bar']
|
302
|
-
assert_equal obj, obj_again
|
303
|
-
assert_equal ["foo"], JSON(JSON(SubArray2["foo"]), :create_additions => true)
|
304
|
-
end
|
305
|
-
|
306
|
-
def test_generate_core_subclasses_with_default_to_json
|
307
|
-
assert_equal '{"foo":"bar"}', JSON(SubHash["foo" => "bar"])
|
308
|
-
assert_equal '["foo"]', JSON(SubArray["foo"])
|
309
|
-
end
|
310
|
-
|
311
|
-
def test_generate_of_core_subclasses
|
312
|
-
obj = SubHash["foo" => SubHash["bar" => true]]
|
313
|
-
obj_json = JSON(obj)
|
314
|
-
obj_again = JSON(obj_json)
|
315
|
-
assert_kind_of Hash, obj_again
|
316
|
-
assert_kind_of Hash, obj_again['foo']
|
317
|
-
assert obj_again['foo']['bar']
|
318
|
-
assert_equal obj, obj_again
|
319
|
-
end
|
320
|
-
|
321
|
-
def test_parser_reset
|
322
|
-
parser = Parser.new(@json)
|
323
|
-
assert_equal(@hash, parser.parse)
|
324
|
-
assert_equal(@hash, parser.parse)
|
325
|
-
end
|
326
|
-
|
327
|
-
def test_comments
|
328
|
-
json = <<EOT
|
329
|
-
{
|
330
|
-
"key1":"value1", // eol comment
|
331
|
-
"key2":"value2" /* multi line
|
332
|
-
* comment */,
|
333
|
-
"key3":"value3" /* multi line
|
334
|
-
// nested eol comment
|
335
|
-
* comment */
|
336
|
-
}
|
337
|
-
EOT
|
338
|
-
assert_equal(
|
339
|
-
{ "key1" => "value1", "key2" => "value2", "key3" => "value3" },
|
340
|
-
parse(json))
|
341
|
-
json = <<EOT
|
342
|
-
{
|
343
|
-
"key1":"value1" /* multi line
|
344
|
-
// nested eol comment
|
345
|
-
/* illegal nested multi line comment */
|
346
|
-
* comment */
|
347
|
-
}
|
348
|
-
EOT
|
349
|
-
assert_raises(ParserError) { parse(json) }
|
350
|
-
json = <<EOT
|
351
|
-
{
|
352
|
-
"key1":"value1" /* multi line
|
353
|
-
// nested eol comment
|
354
|
-
closed multi comment */
|
355
|
-
and again, throw an Error */
|
356
|
-
}
|
357
|
-
EOT
|
358
|
-
assert_raises(ParserError) { parse(json) }
|
359
|
-
json = <<EOT
|
360
|
-
{
|
361
|
-
"key1":"value1" /*/*/
|
362
|
-
}
|
363
|
-
EOT
|
364
|
-
assert_equal({ "key1" => "value1" }, parse(json))
|
365
|
-
end
|
366
|
-
|
367
|
-
def test_backslash
|
368
|
-
data = [ '\\.(?i:gif|jpe?g|png)$' ]
|
369
|
-
json = '["\\\\.(?i:gif|jpe?g|png)$"]'
|
370
|
-
assert_equal json, JSON.generate(data)
|
371
|
-
assert_equal data, JSON.parse(json)
|
372
|
-
#
|
373
|
-
data = [ '\\"' ]
|
374
|
-
json = '["\\\\\""]'
|
375
|
-
assert_equal json, JSON.generate(data)
|
376
|
-
assert_equal data, JSON.parse(json)
|
377
|
-
#
|
378
|
-
json = '["/"]'
|
379
|
-
data = JSON.parse(json)
|
380
|
-
assert_equal ['/'], data
|
381
|
-
assert_equal json, JSON.generate(data)
|
382
|
-
#
|
383
|
-
json = '["\""]'
|
384
|
-
data = JSON.parse(json)
|
385
|
-
assert_equal ['"'], data
|
386
|
-
assert_equal json, JSON.generate(data)
|
387
|
-
json = '["\\\'"]'
|
388
|
-
data = JSON.parse(json)
|
389
|
-
assert_equal ["'"], data
|
390
|
-
assert_equal '["\'"]', JSON.generate(data)
|
391
|
-
end
|
392
|
-
|
393
|
-
def test_wrong_inputs
|
394
|
-
assert_raises(ParserError) { JSON.parse('"foo"') }
|
395
|
-
assert_raises(ParserError) { JSON.parse('123') }
|
396
|
-
assert_raises(ParserError) { JSON.parse('[] bla') }
|
397
|
-
assert_raises(ParserError) { JSON.parse('[] 1') }
|
398
|
-
assert_raises(ParserError) { JSON.parse('[] []') }
|
399
|
-
assert_raises(ParserError) { JSON.parse('[] {}') }
|
400
|
-
assert_raises(ParserError) { JSON.parse('{} []') }
|
401
|
-
assert_raises(ParserError) { JSON.parse('{} {}') }
|
402
|
-
assert_raises(ParserError) { JSON.parse('[NULL]') }
|
403
|
-
assert_raises(ParserError) { JSON.parse('[FALSE]') }
|
404
|
-
assert_raises(ParserError) { JSON.parse('[TRUE]') }
|
405
|
-
assert_raises(ParserError) { JSON.parse('[07] ') }
|
406
|
-
assert_raises(ParserError) { JSON.parse('[0a]') }
|
407
|
-
assert_raises(ParserError) { JSON.parse('[1.]') }
|
408
|
-
assert_raises(ParserError) { JSON.parse(' ') }
|
409
|
-
end
|
410
|
-
|
411
|
-
def test_nesting
|
412
|
-
assert_raises(JSON::NestingError) { JSON.parse '[[]]', :max_nesting => 1 }
|
413
|
-
assert_raises(JSON::NestingError) { JSON.parser.new('[[]]', :max_nesting => 1).parse }
|
414
|
-
assert_equal [[]], JSON.parse('[[]]', :max_nesting => 2)
|
415
|
-
too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
|
416
|
-
too_deep_ary = eval too_deep
|
417
|
-
assert_raises(JSON::NestingError) { JSON.parse too_deep }
|
418
|
-
assert_raises(JSON::NestingError) { JSON.parser.new(too_deep).parse }
|
419
|
-
assert_raises(JSON::NestingError) { JSON.parse too_deep, :max_nesting => 100 }
|
420
|
-
ok = JSON.parse too_deep, :max_nesting => 101
|
421
|
-
assert_equal too_deep_ary, ok
|
422
|
-
ok = JSON.parse too_deep, :max_nesting => nil
|
423
|
-
assert_equal too_deep_ary, ok
|
424
|
-
ok = JSON.parse too_deep, :max_nesting => false
|
425
|
-
assert_equal too_deep_ary, ok
|
426
|
-
ok = JSON.parse too_deep, :max_nesting => 0
|
427
|
-
assert_equal too_deep_ary, ok
|
428
|
-
assert_raises(JSON::NestingError) { JSON.generate [[]], :max_nesting => 1 }
|
429
|
-
assert_equal '[[]]', JSON.generate([[]], :max_nesting => 2)
|
430
|
-
assert_raises(JSON::NestingError) { JSON.generate too_deep_ary }
|
431
|
-
assert_raises(JSON::NestingError) { JSON.generate too_deep_ary, :max_nesting => 100 }
|
432
|
-
ok = JSON.generate too_deep_ary, :max_nesting => 101
|
433
|
-
assert_equal too_deep, ok
|
434
|
-
ok = JSON.generate too_deep_ary, :max_nesting => nil
|
435
|
-
assert_equal too_deep, ok
|
436
|
-
ok = JSON.generate too_deep_ary, :max_nesting => false
|
437
|
-
assert_equal too_deep, ok
|
438
|
-
ok = JSON.generate too_deep_ary, :max_nesting => 0
|
439
|
-
assert_equal too_deep, ok
|
440
|
-
end
|
441
|
-
|
442
|
-
def test_symbolize_names
|
443
|
-
assert_equal({ "foo" => "bar", "baz" => "quux" },
|
444
|
-
JSON.parse('{"foo":"bar", "baz":"quux"}'))
|
445
|
-
assert_equal({ :foo => "bar", :baz => "quux" },
|
446
|
-
JSON.parse('{"foo":"bar", "baz":"quux"}', :symbolize_names => true))
|
447
|
-
end
|
448
|
-
|
449
|
-
def test_load
|
450
|
-
assert_equal @hash, JSON.load(@json)
|
451
|
-
tempfile = Tempfile.open('json')
|
452
|
-
tempfile.write @json
|
453
|
-
tempfile.rewind
|
454
|
-
assert_equal @hash, JSON.load(tempfile)
|
455
|
-
stringio = StringIO.new(@json)
|
456
|
-
stringio.rewind
|
457
|
-
assert_equal @hash, JSON.load(stringio)
|
458
|
-
assert_equal nil, JSON.load(nil)
|
459
|
-
assert_equal nil, JSON.load('')
|
460
|
-
ensure
|
461
|
-
tempfile.close!
|
462
|
-
end
|
463
|
-
|
464
|
-
def test_load_with_options
|
465
|
-
small_hash = JSON("foo" => 'bar')
|
466
|
-
symbol_hash = { :foo => 'bar' }
|
467
|
-
assert_equal symbol_hash, JSON.load(small_hash, nil, :symbolize_names => true)
|
468
|
-
end
|
469
|
-
|
470
|
-
def test_dump
|
471
|
-
too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
|
472
|
-
assert_equal too_deep, JSON.dump(eval(too_deep))
|
473
|
-
assert_kind_of String, Marshal.dump(eval(too_deep))
|
474
|
-
assert_raises(ArgumentError) { JSON.dump(eval(too_deep), 100) }
|
475
|
-
assert_raises(ArgumentError) { Marshal.dump(eval(too_deep), 100) }
|
476
|
-
assert_equal too_deep, JSON.dump(eval(too_deep), 101)
|
477
|
-
assert_kind_of String, Marshal.dump(eval(too_deep), 101)
|
478
|
-
output = StringIO.new
|
479
|
-
JSON.dump(eval(too_deep), output)
|
480
|
-
assert_equal too_deep, output.string
|
481
|
-
output = StringIO.new
|
482
|
-
JSON.dump(eval(too_deep), output, 101)
|
483
|
-
assert_equal too_deep, output.string
|
484
|
-
end
|
485
|
-
|
486
|
-
def test_dump_should_modify_defaults
|
487
|
-
max_nesting = JSON.dump_default_options[:max_nesting]
|
488
|
-
JSON.dump([], StringIO.new, 10)
|
489
|
-
assert_equal max_nesting, JSON.dump_default_options[:max_nesting]
|
490
|
-
end
|
491
|
-
|
492
|
-
def test_big_integers
|
493
|
-
json1 = JSON([orig = (1 << 31) - 1])
|
494
|
-
assert_equal orig, JSON[json1][0]
|
495
|
-
json2 = JSON([orig = 1 << 31])
|
496
|
-
assert_equal orig, JSON[json2][0]
|
497
|
-
json3 = JSON([orig = (1 << 62) - 1])
|
498
|
-
assert_equal orig, JSON[json3][0]
|
499
|
-
json4 = JSON([orig = 1 << 62])
|
500
|
-
assert_equal orig, JSON[json4][0]
|
501
|
-
json5 = JSON([orig = 1 << 64])
|
502
|
-
assert_equal orig, JSON[json5][0]
|
503
|
-
end
|
504
|
-
|
505
|
-
if defined?(JSON::Ext::Parser)
|
506
|
-
def test_allocate
|
507
|
-
parser = JSON::Ext::Parser.new("{}")
|
508
|
-
assert_raise(TypeError, '[ruby-core:35079]') {parser.__send__(:initialize, "{}")}
|
509
|
-
parser = JSON::Ext::Parser.allocate
|
510
|
-
assert_raise(TypeError, '[ruby-core:35079]') {parser.source}
|
511
|
-
end
|
512
|
-
end
|
513
|
-
|
514
|
-
def test_argument_encoding
|
515
|
-
source = "{}".force_encoding("ascii-8bit")
|
516
|
-
JSON::Parser.new(source)
|
517
|
-
assert_equal Encoding::ASCII_8BIT, source.encoding
|
518
|
-
end if defined?(Encoding::ASCII_8BIT)
|
519
|
-
end
|