json_pure 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +25 -0
- data/GPL +340 -0
- data/README +77 -0
- data/Rakefile +250 -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/data/example.json +1 -0
- data/data/index.html +37 -0
- data/data/prototype.js +2515 -0
- data/ext/json/ext/generator/Makefile +149 -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/Makefile +149 -0
- data/ext/json/ext/parser/extconf.rb +9 -0
- data/ext/json/ext/parser/parser.c +1551 -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 +210 -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 +235 -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 +55 -0
- data/tools/fuzz.rb +133 -0
- data/tools/server.rb +62 -0
- metadata +146 -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,235 @@
|
|
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_raises(JSON::ParserError) { parse('[+23.2]') }
|
52
|
+
assert_raises(JSON::ParserError) { parse('[+23]') }
|
53
|
+
assert_raises(JSON::ParserError) { parse('[.23]') }
|
54
|
+
assert_raises(JSON::ParserError) { parse('[023]') }
|
55
|
+
assert_equal_float [3.141], parse('[3.141]')
|
56
|
+
assert_equal_float [-3.141], parse('[-3.141]')
|
57
|
+
assert_equal_float [3.141], parse('[3141e-3]')
|
58
|
+
assert_equal_float [3.141], parse('[3141.1e-3]')
|
59
|
+
assert_equal_float [3.141], parse('[3141E-3]')
|
60
|
+
assert_equal_float [3.141], parse('[3141.0E-3]')
|
61
|
+
assert_equal_float [-3.141], parse('[-3141.0e-3]')
|
62
|
+
assert_equal_float [-3.141], parse('[-3141e-3]')
|
63
|
+
assert_equal([""], parse('[""]'))
|
64
|
+
assert_equal(["foobar"], parse('["foobar"]'))
|
65
|
+
assert_equal([{}], parse('[{}]'))
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_parse_simple_objects
|
69
|
+
assert_equal({}, parse('{}'))
|
70
|
+
assert_equal({}, parse(' { } '))
|
71
|
+
assert_equal({ "a" => nil }, parse('{ "a" : null}'))
|
72
|
+
assert_equal({ "a" => nil }, parse('{"a":null}'))
|
73
|
+
assert_equal({ "a" => false }, parse('{ "a" : false } '))
|
74
|
+
assert_equal({ "a" => false }, parse('{"a":false}'))
|
75
|
+
assert_raises(JSON::ParserError) { parse('{false}') }
|
76
|
+
assert_equal({ "a" => true }, parse('{"a":true}'))
|
77
|
+
assert_equal({ "a" => true }, parse(' { "a" : true } '))
|
78
|
+
assert_equal({ "a" => -23 }, parse(' { "a" : -23 } '))
|
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" => 0.23 }, parse(' { "a" : 0.23 } '))
|
83
|
+
assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
|
84
|
+
end
|
85
|
+
|
86
|
+
begin
|
87
|
+
require 'permutation'
|
88
|
+
def test_parse_more_complex_arrays
|
89
|
+
a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
|
90
|
+
perms = Permutation.for a
|
91
|
+
perms.each do |perm|
|
92
|
+
orig_ary = perm.project
|
93
|
+
json = pretty_generate(orig_ary)
|
94
|
+
assert_equal orig_ary, parse(json)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_parse_complex_objects
|
99
|
+
a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
|
100
|
+
perms = Permutation.for a
|
101
|
+
perms.each do |perm|
|
102
|
+
s = "a"
|
103
|
+
orig_obj = perm.project.inject({}) { |h, x| h[s.dup] = x; s = s.succ; h }
|
104
|
+
json = pretty_generate(orig_obj)
|
105
|
+
assert_equal orig_obj, parse(json)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
rescue LoadError
|
109
|
+
warn "Skipping permutation tests."
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_parse_arrays
|
113
|
+
assert_equal([1,2,3], parse('[1,2,3]'))
|
114
|
+
assert_equal([1.2,2,3], parse('[1.2,2,3]'))
|
115
|
+
assert_equal([[],[[],[]]], parse('[[],[[],[]]]'))
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_parse_values
|
119
|
+
assert_equal([""], parse('[""]'))
|
120
|
+
assert_equal(["\\"], parse('["\\\\"]'))
|
121
|
+
assert_equal(['"'], parse('["\""]'))
|
122
|
+
assert_equal(['\\"\\'], parse('["\\\\\\"\\\\"]'))
|
123
|
+
assert_equal(["\"\b\n\r\t\0\037"],
|
124
|
+
parse('["\"\b\n\r\t\u0000\u001f"]'))
|
125
|
+
for i in 0 ... @ary.size
|
126
|
+
assert_equal(@ary[i], parse(@ary_to_parse[i]))
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def test_parse_array
|
131
|
+
assert_equal([], parse('[]'))
|
132
|
+
assert_equal([], parse(' [ ] '))
|
133
|
+
assert_equal([1], parse('[1]'))
|
134
|
+
assert_equal([1], parse(' [ 1 ] '))
|
135
|
+
assert_equal(@ary,
|
136
|
+
parse('[[1],["foo"],[3.14],[47.11e+2],[2718.0E-3],[null],[[1,-2,3]]'\
|
137
|
+
',[false],[true]]'))
|
138
|
+
assert_equal(@ary, parse(%Q{ [ [1] , ["foo"] , [3.14] \t , [47.11e+2]
|
139
|
+
, [2718.0E-3 ],\r[ null] , [[1, -2, 3 ]], [false ],[ true]\n ] }))
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_parse_object
|
143
|
+
assert_equal({}, parse('{}'))
|
144
|
+
assert_equal({}, parse(' { } '))
|
145
|
+
assert_equal({'foo'=>'bar'}, parse('{"foo":"bar"}'))
|
146
|
+
assert_equal({'foo'=>'bar'}, parse(' { "foo" : "bar" } '))
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_parser_reset
|
150
|
+
parser = Parser.new(@json)
|
151
|
+
assert_equal(@hash, parser.parse)
|
152
|
+
assert_equal(@hash, parser.parse)
|
153
|
+
end
|
154
|
+
|
155
|
+
def test_comments
|
156
|
+
json = <<EOT
|
157
|
+
{
|
158
|
+
"key1":"value1", // eol comment
|
159
|
+
"key2":"value2" /* multi line
|
160
|
+
* comment */,
|
161
|
+
"key3":"value3" /* multi line
|
162
|
+
// nested eol comment
|
163
|
+
* comment */
|
164
|
+
}
|
165
|
+
EOT
|
166
|
+
assert_equal(
|
167
|
+
{ "key1" => "value1", "key2" => "value2", "key3" => "value3" },
|
168
|
+
parse(json))
|
169
|
+
json = <<EOT
|
170
|
+
{
|
171
|
+
"key1":"value1" /* multi line
|
172
|
+
// nested eol comment
|
173
|
+
/* illegal nested multi line comment */
|
174
|
+
* comment */
|
175
|
+
}
|
176
|
+
EOT
|
177
|
+
assert_raises(ParserError) { parse(json) }
|
178
|
+
json = <<EOT
|
179
|
+
{
|
180
|
+
"key1":"value1" /* multi line
|
181
|
+
// nested eol comment
|
182
|
+
closed multi comment */
|
183
|
+
and again, throw an Error */
|
184
|
+
}
|
185
|
+
EOT
|
186
|
+
assert_raises(ParserError) { parse(json) }
|
187
|
+
json = <<EOT
|
188
|
+
{
|
189
|
+
"key1":"value1" /*/*/
|
190
|
+
}
|
191
|
+
EOT
|
192
|
+
assert_equal({ "key1" => "value1" }, parse(json))
|
193
|
+
end
|
194
|
+
|
195
|
+
def test_backslash
|
196
|
+
data = [ '\\.(?i:gif|jpe?g|png)$' ]
|
197
|
+
json = '["\\\\.(?i:gif|jpe?g|png)$"]'
|
198
|
+
assert_equal json, JSON.unparse(data)
|
199
|
+
assert_equal data, JSON.parse(json)
|
200
|
+
#
|
201
|
+
data = [ '\\"' ]
|
202
|
+
json = '["\\\\\""]'
|
203
|
+
assert_equal json, JSON.unparse(data)
|
204
|
+
assert_equal data, JSON.parse(json)
|
205
|
+
#
|
206
|
+
json = '["\/"]'
|
207
|
+
data = JSON.parse(json)
|
208
|
+
assert_equal ['/'], data
|
209
|
+
assert_equal json, JSON.unparse(data)
|
210
|
+
#
|
211
|
+
json = '["\""]'
|
212
|
+
data = JSON.parse(json)
|
213
|
+
assert_equal ['"'], data
|
214
|
+
assert_equal json, JSON.unparse(data)
|
215
|
+
end
|
216
|
+
|
217
|
+
def test_wrong_inputs
|
218
|
+
assert_raises(ParserError) { JSON.parse('"foo"') }
|
219
|
+
assert_raises(ParserError) { JSON.parse('123') }
|
220
|
+
assert_raises(ParserError) { JSON.parse('[] bla') }
|
221
|
+
assert_raises(ParserError) { JSON.parse('[] 1') }
|
222
|
+
assert_raises(ParserError) { JSON.parse('[] []') }
|
223
|
+
assert_raises(ParserError) { JSON.parse('[] {}') }
|
224
|
+
assert_raises(ParserError) { JSON.parse('{} []') }
|
225
|
+
assert_raises(ParserError) { JSON.parse('{} {}') }
|
226
|
+
assert_raises(ParserError) { JSON.parse('[NULL]') }
|
227
|
+
assert_raises(ParserError) { JSON.parse('[FALSE]') }
|
228
|
+
assert_raises(ParserError) { JSON.parse('[TRUE]') }
|
229
|
+
assert_raises(ParserError) { JSON.parse('[07] ') }
|
230
|
+
assert_raises(ParserError) { JSON.parse('[0a]') }
|
231
|
+
assert_raises(ParserError) { JSON.parse('[1.]') }
|
232
|
+
assert_raises(ParserError) { JSON.parse(' ') }
|
233
|
+
end
|
234
|
+
end
|
235
|
+
# 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
|