json 1.0.3-mswin32
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of json might be problematic. Click here for more details.
- data/CHANGES +32 -0
- data/GPL +340 -0
- data/README +77 -0
- data/Rakefile +304 -0
- data/TODO +1 -0
- data/VERSION +1 -0
- data/benchmarks/benchmark.txt +133 -0
- data/benchmarks/benchmark_generator.rb +44 -0
- data/benchmarks/benchmark_parser.rb +22 -0
- data/benchmarks/benchmark_rails.rb +26 -0
- data/bin/edit_json.rb +11 -0
- data/bin/prettify_json.rb +75 -0
- data/data/example.json +1 -0
- data/data/index.html +37 -0
- data/data/prototype.js +2515 -0
- data/ext/json/ext/generator.so +0 -0
- data/ext/json/ext/generator/extconf.rb +9 -0
- data/ext/json/ext/generator/generator.c +729 -0
- data/ext/json/ext/generator/unicode.c +184 -0
- data/ext/json/ext/generator/unicode.h +40 -0
- data/ext/json/ext/parser.so +0 -0
- data/ext/json/ext/parser/extconf.rb +9 -0
- data/ext/json/ext/parser/parser.c +1554 -0
- data/ext/json/ext/parser/parser.rl +515 -0
- data/ext/json/ext/parser/unicode.c +156 -0
- data/ext/json/ext/parser/unicode.h +44 -0
- data/install.rb +26 -0
- data/lib/json.rb +205 -0
- data/lib/json/Array.xpm +21 -0
- data/lib/json/FalseClass.xpm +21 -0
- data/lib/json/Hash.xpm +21 -0
- data/lib/json/Key.xpm +73 -0
- data/lib/json/NilClass.xpm +21 -0
- data/lib/json/Numeric.xpm +28 -0
- data/lib/json/String.xpm +96 -0
- data/lib/json/TrueClass.xpm +21 -0
- data/lib/json/common.rb +184 -0
- data/lib/json/editor.rb +1207 -0
- data/lib/json/ext.rb +13 -0
- data/lib/json/json.xpm +1499 -0
- data/lib/json/pure.rb +75 -0
- data/lib/json/pure/generator.rb +321 -0
- data/lib/json/pure/parser.rb +214 -0
- data/lib/json/version.rb +8 -0
- data/tests/fixtures/fail1.json +1 -0
- data/tests/fixtures/fail10.json +1 -0
- data/tests/fixtures/fail11.json +1 -0
- data/tests/fixtures/fail12.json +1 -0
- data/tests/fixtures/fail13.json +1 -0
- data/tests/fixtures/fail14.json +1 -0
- data/tests/fixtures/fail15.json +1 -0
- data/tests/fixtures/fail16.json +1 -0
- data/tests/fixtures/fail17.json +1 -0
- data/tests/fixtures/fail19.json +1 -0
- data/tests/fixtures/fail2.json +1 -0
- data/tests/fixtures/fail20.json +1 -0
- data/tests/fixtures/fail21.json +1 -0
- data/tests/fixtures/fail22.json +1 -0
- data/tests/fixtures/fail23.json +1 -0
- data/tests/fixtures/fail24.json +1 -0
- data/tests/fixtures/fail25.json +1 -0
- data/tests/fixtures/fail26.json +1 -0
- data/tests/fixtures/fail27.json +2 -0
- data/tests/fixtures/fail28.json +2 -0
- data/tests/fixtures/fail3.json +1 -0
- data/tests/fixtures/fail4.json +1 -0
- data/tests/fixtures/fail5.json +1 -0
- data/tests/fixtures/fail6.json +1 -0
- data/tests/fixtures/fail7.json +1 -0
- data/tests/fixtures/fail8.json +1 -0
- data/tests/fixtures/fail9.json +1 -0
- data/tests/fixtures/pass1.json +56 -0
- data/tests/fixtures/pass18.json +1 -0
- data/tests/fixtures/pass2.json +1 -0
- data/tests/fixtures/pass3.json +6 -0
- data/tests/runner.rb +24 -0
- data/tests/test_json.rb +236 -0
- data/tests/test_json_addition.rb +94 -0
- data/tests/test_json_fixtures.rb +30 -0
- data/tests/test_json_generate.rb +81 -0
- data/tests/test_json_unicode.rb +58 -0
- data/tools/fuzz.rb +131 -0
- data/tools/server.rb +62 -0
- metadata +149 -0
data/lib/json/version.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
"A JSON payload should be an object or array, not a string."
|
@@ -0,0 +1 @@
|
|
1
|
+
{"Extra value after close": true} "misplaced quoted value"
|
@@ -0,0 +1 @@
|
|
1
|
+
{"Illegal expression": 1 + 2}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"Illegal invocation": alert()}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"Numbers cannot have leading zeroes": 013}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"Numbers cannot be hex": 0x14}
|
@@ -0,0 +1 @@
|
|
1
|
+
["Illegal backslash escape: \x15"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["Illegal backslash escape: \'"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["Illegal backslash escape: \017"]
|
@@ -0,0 +1 @@
|
|
1
|
+
{"Missing colon" null}
|
@@ -0,0 +1 @@
|
|
1
|
+
["Unclosed array"
|
@@ -0,0 +1 @@
|
|
1
|
+
{"Double colon":: null}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"Comma instead of colon", null}
|
@@ -0,0 +1 @@
|
|
1
|
+
["Colon instead of comma": false]
|
@@ -0,0 +1 @@
|
|
1
|
+
["Bad value", truth]
|
@@ -0,0 +1 @@
|
|
1
|
+
['single quote']
|
@@ -0,0 +1 @@
|
|
1
|
+
["tab character in string "]
|
@@ -0,0 +1 @@
|
|
1
|
+
["tab\ character\ in\ string\ "]
|
@@ -0,0 +1 @@
|
|
1
|
+
{unquoted_key: "keys must be quoted"}
|
@@ -0,0 +1 @@
|
|
1
|
+
["extra comma",]
|
@@ -0,0 +1 @@
|
|
1
|
+
["double extra comma",,]
|
@@ -0,0 +1 @@
|
|
1
|
+
[ , "<-- missing value"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["Comma after the close"],
|
@@ -0,0 +1 @@
|
|
1
|
+
["Extra close"]]
|
@@ -0,0 +1 @@
|
|
1
|
+
{"Extra comma": true,}
|
@@ -0,0 +1,56 @@
|
|
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"]
|
@@ -0,0 +1 @@
|
|
1
|
+
[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]] // No, we don't limit our depth: Moved to pass...
|
@@ -0,0 +1 @@
|
|
1
|
+
[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]
|
data/tests/runner.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'test/unit/ui/console/testrunner'
|
4
|
+
require 'test/unit/testsuite'
|
5
|
+
$:.unshift File.expand_path(File.dirname($0))
|
6
|
+
$:.unshift 'tests'
|
7
|
+
require 'test_json'
|
8
|
+
require 'test_json_generate'
|
9
|
+
require 'test_json_unicode'
|
10
|
+
require 'test_json_addition'
|
11
|
+
require 'test_json_fixtures'
|
12
|
+
|
13
|
+
class TS_AllTests
|
14
|
+
def self.suite
|
15
|
+
suite = Test::Unit::TestSuite.new name
|
16
|
+
suite << TC_JSONGenerate.suite
|
17
|
+
suite << TC_JSON.suite
|
18
|
+
suite << TC_JSONUnicode.suite
|
19
|
+
suite << TC_JSONAddition.suite
|
20
|
+
suite << TC_JSONFixtures.suite
|
21
|
+
end
|
22
|
+
end
|
23
|
+
Test::Unit::UI::Console::TestRunner.run(TS_AllTests)
|
24
|
+
# vim: set et sw=2 ts=2:
|
data/tests/test_json.rb
ADDED
@@ -0,0 +1,236 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
class TC_JSON < Test::Unit::TestCase
|
7
|
+
include JSON
|
8
|
+
|
9
|
+
def setup
|
10
|
+
$KCODE = 'UTF8'
|
11
|
+
@ary = [1, "foo", 3.14, 4711.0, 2.718, nil, [1,-2,3], false, true].map do
|
12
|
+
|x| [x]
|
13
|
+
end
|
14
|
+
@ary_to_parse = ["1", '"foo"', "3.14", "4711.0", "2.718", "null",
|
15
|
+
"[1,-2,3]", "false", "true"].map do
|
16
|
+
|x| "[#{x}]"
|
17
|
+
end
|
18
|
+
@hash = {
|
19
|
+
'a' => 2,
|
20
|
+
'b' => 3.141,
|
21
|
+
'c' => 'c',
|
22
|
+
'd' => [ 1, "b", 3.14 ],
|
23
|
+
'e' => { 'foo' => 'bar' },
|
24
|
+
'g' => "\"\0\037",
|
25
|
+
'h' => 1000.0,
|
26
|
+
'i' => 0.001
|
27
|
+
}
|
28
|
+
@json = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' +
|
29
|
+
'"g":"\\"\\u0000\\u001f","h":1.0E3,"i":1.0E-3}'
|
30
|
+
end
|
31
|
+
suite << TC_JSON.suite
|
32
|
+
|
33
|
+
def test_construction
|
34
|
+
parser = JSON::Parser.new('test')
|
35
|
+
assert_equal 'test', parser.source
|
36
|
+
end
|
37
|
+
|
38
|
+
def assert_equal_float(expected, is)
|
39
|
+
assert_in_delta(expected.first, is.first, 1e-2)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_parse_simple_arrays
|
43
|
+
assert_equal([], parse('[]'))
|
44
|
+
assert_equal([], parse(' [ ] '))
|
45
|
+
assert_equal([nil], parse('[null]'))
|
46
|
+
assert_equal([false], parse('[false]'))
|
47
|
+
assert_equal([true], parse('[true]'))
|
48
|
+
assert_equal([-23], parse('[-23]'))
|
49
|
+
assert_equal([23], parse('[23]'))
|
50
|
+
assert_equal([0.23], parse('[0.23]'))
|
51
|
+
assert_equal([0.0], parse('[0e0]'))
|
52
|
+
assert_raises(JSON::ParserError) { parse('[+23.2]') }
|
53
|
+
assert_raises(JSON::ParserError) { parse('[+23]') }
|
54
|
+
assert_raises(JSON::ParserError) { parse('[.23]') }
|
55
|
+
assert_raises(JSON::ParserError) { parse('[023]') }
|
56
|
+
assert_equal_float [3.141], parse('[3.141]')
|
57
|
+
assert_equal_float [-3.141], parse('[-3.141]')
|
58
|
+
assert_equal_float [3.141], parse('[3141e-3]')
|
59
|
+
assert_equal_float [3.141], parse('[3141.1e-3]')
|
60
|
+
assert_equal_float [3.141], parse('[3141E-3]')
|
61
|
+
assert_equal_float [3.141], parse('[3141.0E-3]')
|
62
|
+
assert_equal_float [-3.141], parse('[-3141.0e-3]')
|
63
|
+
assert_equal_float [-3.141], parse('[-3141e-3]')
|
64
|
+
assert_equal([""], parse('[""]'))
|
65
|
+
assert_equal(["foobar"], parse('["foobar"]'))
|
66
|
+
assert_equal([{}], parse('[{}]'))
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_parse_simple_objects
|
70
|
+
assert_equal({}, parse('{}'))
|
71
|
+
assert_equal({}, parse(' { } '))
|
72
|
+
assert_equal({ "a" => nil }, parse('{ "a" : null}'))
|
73
|
+
assert_equal({ "a" => nil }, parse('{"a":null}'))
|
74
|
+
assert_equal({ "a" => false }, parse('{ "a" : false } '))
|
75
|
+
assert_equal({ "a" => false }, parse('{"a":false}'))
|
76
|
+
assert_raises(JSON::ParserError) { parse('{false}') }
|
77
|
+
assert_equal({ "a" => true }, parse('{"a":true}'))
|
78
|
+
assert_equal({ "a" => true }, parse(' { "a" : true } '))
|
79
|
+
assert_equal({ "a" => -23 }, parse(' { "a" : -23 } '))
|
80
|
+
assert_equal({ "a" => -23 }, parse(' { "a" : -23 } '))
|
81
|
+
assert_equal({ "a" => 23 }, parse('{"a":23 } '))
|
82
|
+
assert_equal({ "a" => 23 }, parse(' { "a" : 23 } '))
|
83
|
+
assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
|
84
|
+
assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
|
85
|
+
end
|
86
|
+
|
87
|
+
begin
|
88
|
+
require 'permutation'
|
89
|
+
def test_parse_more_complex_arrays
|
90
|
+
a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
|
91
|
+
perms = Permutation.for a
|
92
|
+
perms.each do |perm|
|
93
|
+
orig_ary = perm.project
|
94
|
+
json = pretty_generate(orig_ary)
|
95
|
+
assert_equal orig_ary, parse(json)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_parse_complex_objects
|
100
|
+
a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
|
101
|
+
perms = Permutation.for a
|
102
|
+
perms.each do |perm|
|
103
|
+
s = "a"
|
104
|
+
orig_obj = perm.project.inject({}) { |h, x| h[s.dup] = x; s = s.succ; h }
|
105
|
+
json = pretty_generate(orig_obj)
|
106
|
+
assert_equal orig_obj, parse(json)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
rescue LoadError
|
110
|
+
warn "Skipping permutation tests."
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_parse_arrays
|
114
|
+
assert_equal([1,2,3], parse('[1,2,3]'))
|
115
|
+
assert_equal([1.2,2,3], parse('[1.2,2,3]'))
|
116
|
+
assert_equal([[],[[],[]]], parse('[[],[[],[]]]'))
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_parse_values
|
120
|
+
assert_equal([""], parse('[""]'))
|
121
|
+
assert_equal(["\\"], parse('["\\\\"]'))
|
122
|
+
assert_equal(['"'], parse('["\""]'))
|
123
|
+
assert_equal(['\\"\\'], parse('["\\\\\\"\\\\"]'))
|
124
|
+
assert_equal(["\"\b\n\r\t\0\037"],
|
125
|
+
parse('["\"\b\n\r\t\u0000\u001f"]'))
|
126
|
+
for i in 0 ... @ary.size
|
127
|
+
assert_equal(@ary[i], parse(@ary_to_parse[i]))
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_parse_array
|
132
|
+
assert_equal([], parse('[]'))
|
133
|
+
assert_equal([], parse(' [ ] '))
|
134
|
+
assert_equal([1], parse('[1]'))
|
135
|
+
assert_equal([1], parse(' [ 1 ] '))
|
136
|
+
assert_equal(@ary,
|
137
|
+
parse('[[1],["foo"],[3.14],[47.11e+2],[2718.0E-3],[null],[[1,-2,3]]'\
|
138
|
+
',[false],[true]]'))
|
139
|
+
assert_equal(@ary, parse(%Q{ [ [1] , ["foo"] , [3.14] \t , [47.11e+2]
|
140
|
+
, [2718.0E-3 ],\r[ null] , [[1, -2, 3 ]], [false ],[ true]\n ] }))
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_parse_object
|
144
|
+
assert_equal({}, parse('{}'))
|
145
|
+
assert_equal({}, parse(' { } '))
|
146
|
+
assert_equal({'foo'=>'bar'}, parse('{"foo":"bar"}'))
|
147
|
+
assert_equal({'foo'=>'bar'}, parse(' { "foo" : "bar" } '))
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_parser_reset
|
151
|
+
parser = Parser.new(@json)
|
152
|
+
assert_equal(@hash, parser.parse)
|
153
|
+
assert_equal(@hash, parser.parse)
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_comments
|
157
|
+
json = <<EOT
|
158
|
+
{
|
159
|
+
"key1":"value1", // eol comment
|
160
|
+
"key2":"value2" /* multi line
|
161
|
+
* comment */,
|
162
|
+
"key3":"value3" /* multi line
|
163
|
+
// nested eol comment
|
164
|
+
* comment */
|
165
|
+
}
|
166
|
+
EOT
|
167
|
+
assert_equal(
|
168
|
+
{ "key1" => "value1", "key2" => "value2", "key3" => "value3" },
|
169
|
+
parse(json))
|
170
|
+
json = <<EOT
|
171
|
+
{
|
172
|
+
"key1":"value1" /* multi line
|
173
|
+
// nested eol comment
|
174
|
+
/* illegal nested multi line comment */
|
175
|
+
* comment */
|
176
|
+
}
|
177
|
+
EOT
|
178
|
+
assert_raises(ParserError) { parse(json) }
|
179
|
+
json = <<EOT
|
180
|
+
{
|
181
|
+
"key1":"value1" /* multi line
|
182
|
+
// nested eol comment
|
183
|
+
closed multi comment */
|
184
|
+
and again, throw an Error */
|
185
|
+
}
|
186
|
+
EOT
|
187
|
+
assert_raises(ParserError) { parse(json) }
|
188
|
+
json = <<EOT
|
189
|
+
{
|
190
|
+
"key1":"value1" /*/*/
|
191
|
+
}
|
192
|
+
EOT
|
193
|
+
assert_equal({ "key1" => "value1" }, parse(json))
|
194
|
+
end
|
195
|
+
|
196
|
+
def test_backslash
|
197
|
+
data = [ '\\.(?i:gif|jpe?g|png)$' ]
|
198
|
+
json = '["\\\\.(?i:gif|jpe?g|png)$"]'
|
199
|
+
assert_equal json, JSON.unparse(data)
|
200
|
+
assert_equal data, JSON.parse(json)
|
201
|
+
#
|
202
|
+
data = [ '\\"' ]
|
203
|
+
json = '["\\\\\""]'
|
204
|
+
assert_equal json, JSON.unparse(data)
|
205
|
+
assert_equal data, JSON.parse(json)
|
206
|
+
#
|
207
|
+
json = '["\/"]'
|
208
|
+
data = JSON.parse(json)
|
209
|
+
assert_equal ['/'], data
|
210
|
+
assert_equal json, JSON.unparse(data)
|
211
|
+
#
|
212
|
+
json = '["\""]'
|
213
|
+
data = JSON.parse(json)
|
214
|
+
assert_equal ['"'], data
|
215
|
+
assert_equal json, JSON.unparse(data)
|
216
|
+
end
|
217
|
+
|
218
|
+
def test_wrong_inputs
|
219
|
+
assert_raises(ParserError) { JSON.parse('"foo"') }
|
220
|
+
assert_raises(ParserError) { JSON.parse('123') }
|
221
|
+
assert_raises(ParserError) { JSON.parse('[] bla') }
|
222
|
+
assert_raises(ParserError) { JSON.parse('[] 1') }
|
223
|
+
assert_raises(ParserError) { JSON.parse('[] []') }
|
224
|
+
assert_raises(ParserError) { JSON.parse('[] {}') }
|
225
|
+
assert_raises(ParserError) { JSON.parse('{} []') }
|
226
|
+
assert_raises(ParserError) { JSON.parse('{} {}') }
|
227
|
+
assert_raises(ParserError) { JSON.parse('[NULL]') }
|
228
|
+
assert_raises(ParserError) { JSON.parse('[FALSE]') }
|
229
|
+
assert_raises(ParserError) { JSON.parse('[TRUE]') }
|
230
|
+
assert_raises(ParserError) { JSON.parse('[07] ') }
|
231
|
+
assert_raises(ParserError) { JSON.parse('[0a]') }
|
232
|
+
assert_raises(ParserError) { JSON.parse('[1.]') }
|
233
|
+
assert_raises(ParserError) { JSON.parse(' ') }
|
234
|
+
end
|
235
|
+
end
|
236
|
+
# vim: set et sw=2 ts=2:
|
@@ -0,0 +1,94 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
class TC_JSONAddition < Test::Unit::TestCase
|
7
|
+
include JSON
|
8
|
+
|
9
|
+
class A
|
10
|
+
def initialize(a)
|
11
|
+
@a = a
|
12
|
+
end
|
13
|
+
|
14
|
+
attr_reader :a
|
15
|
+
|
16
|
+
def ==(other)
|
17
|
+
a == other.a
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.json_create(object)
|
21
|
+
new(*object['args'])
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_json(*args)
|
25
|
+
{
|
26
|
+
'json_class' => self.class,
|
27
|
+
'args' => [ @a ],
|
28
|
+
}.to_json(*args)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class B
|
33
|
+
def to_json(*args)
|
34
|
+
{
|
35
|
+
'json_class' => self.class,
|
36
|
+
}.to_json(*args)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class C
|
41
|
+
def to_json(*args)
|
42
|
+
{
|
43
|
+
'json_class' => 'TC_JSONAddition::Nix',
|
44
|
+
}.to_json(*args)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def setup
|
49
|
+
$KCODE = 'UTF8'
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_extended_json
|
53
|
+
a = A.new(666)
|
54
|
+
assert A.json_creatable?
|
55
|
+
json = generate(a)
|
56
|
+
a_again = JSON.parse(json)
|
57
|
+
assert_kind_of a.class, a_again
|
58
|
+
assert_equal a, a_again
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_extended_json_fail
|
62
|
+
b = B.new
|
63
|
+
assert !B.json_creatable?
|
64
|
+
json = generate(b)
|
65
|
+
assert_equal({ 'json_class' => B.name }, JSON.parse(json))
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_extended_json_fail
|
69
|
+
c = C.new
|
70
|
+
assert !C.json_creatable?
|
71
|
+
json = generate(c)
|
72
|
+
assert_raises(ArgumentError) { JSON.parse(json) }
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_raw_strings
|
76
|
+
raw = ''
|
77
|
+
raw_array = []
|
78
|
+
for i in 0..255
|
79
|
+
raw << i
|
80
|
+
raw_array << i
|
81
|
+
end
|
82
|
+
json = raw.to_json_raw
|
83
|
+
json_raw_object = raw.to_json_raw_object
|
84
|
+
hash = { 'json_class' => 'String', 'raw'=> raw_array }
|
85
|
+
assert_equal hash, json_raw_object
|
86
|
+
json_raw = <<EOT.chomp
|
87
|
+
{\"json_class\":\"String\",\"raw\":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255]}
|
88
|
+
EOT
|
89
|
+
# "
|
90
|
+
assert_equal json_raw, json
|
91
|
+
raw_again = JSON.parse(json)
|
92
|
+
assert_equal raw, raw_again
|
93
|
+
end
|
94
|
+
end
|