json 1.4.3-java
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/lib/json.rb +10 -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/add/core.rb +148 -0
- data/lib/json/add/rails.rb +58 -0
- data/lib/json/common.rb +397 -0
- data/lib/json/editor.rb +1371 -0
- data/lib/json/ext.rb +15 -0
- data/lib/json/ext/generator.jar +0 -0
- data/lib/json/ext/parser.jar +0 -0
- data/lib/json/json.xpm +1499 -0
- data/lib/json/pure.rb +77 -0
- data/lib/json/pure/generator.rb +452 -0
- data/lib/json/pure/parser.rb +307 -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/fail18.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/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/pass15.json +1 -0
- data/tests/fixtures/pass16.json +1 -0
- data/tests/fixtures/pass17.json +1 -0
- data/tests/fixtures/pass2.json +1 -0
- data/tests/fixtures/pass26.json +1 -0
- data/tests/fixtures/pass3.json +6 -0
- data/tests/test_json.rb +361 -0
- data/tests/test_json_addition.rb +162 -0
- data/tests/test_json_encoding.rb +68 -0
- data/tests/test_json_fixtures.rb +34 -0
- data/tests/test_json_generate.rb +122 -0
- data/tests/test_json_rails.rb +144 -0
- data/tests/test_json_unicode.rb +76 -0
- metadata +122 -0
@@ -0,0 +1 @@
|
|
1
|
+
["Illegal backslash escape: \'"]
|
@@ -0,0 +1 @@
|
|
1
|
+
["Illegal backslash escape: \017"]
|
@@ -0,0 +1 @@
|
|
1
|
+
[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]
|
@@ -0,0 +1 @@
|
|
1
|
+
["tab\ character\ in\ string\ "]
|
data/tests/test_json.rb
ADDED
@@ -0,0 +1,361 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
case ENV['JSON']
|
6
|
+
when 'pure' then require 'json/pure'
|
7
|
+
when 'ext' then require 'json/ext'
|
8
|
+
else require 'json'
|
9
|
+
end
|
10
|
+
require 'stringio'
|
11
|
+
|
12
|
+
unless Array.method_defined?(:permutation)
|
13
|
+
begin
|
14
|
+
require 'enumerator'
|
15
|
+
require 'permutation'
|
16
|
+
class Array
|
17
|
+
def permutation
|
18
|
+
Permutation.for(self).to_enum.map { |x| x.project }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
rescue LoadError
|
22
|
+
warn "Skipping permutation tests."
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class TC_JSON < Test::Unit::TestCase
|
27
|
+
include JSON
|
28
|
+
|
29
|
+
def setup
|
30
|
+
@ary = [1, "foo", 3.14, 4711.0, 2.718, nil, [1,-2,3], false, true].map do
|
31
|
+
|x| [x]
|
32
|
+
end
|
33
|
+
@ary_to_parse = ["1", '"foo"', "3.14", "4711.0", "2.718", "null",
|
34
|
+
"[1,-2,3]", "false", "true"].map do
|
35
|
+
|x| "[#{x}]"
|
36
|
+
end
|
37
|
+
@hash = {
|
38
|
+
'a' => 2,
|
39
|
+
'b' => 3.141,
|
40
|
+
'c' => 'c',
|
41
|
+
'd' => [ 1, "b", 3.14 ],
|
42
|
+
'e' => { 'foo' => 'bar' },
|
43
|
+
'g' => "\"\0\037",
|
44
|
+
'h' => 1000.0,
|
45
|
+
'i' => 0.001
|
46
|
+
}
|
47
|
+
@json = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},'\
|
48
|
+
'"g":"\\"\\u0000\\u001f","h":1.0E3,"i":1.0E-3}'
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_construction
|
52
|
+
parser = JSON::Parser.new('test')
|
53
|
+
assert_equal 'test', parser.source
|
54
|
+
end
|
55
|
+
|
56
|
+
def assert_equal_float(expected, is)
|
57
|
+
assert_in_delta(expected.first, is.first, 1e-2)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_parse_simple_arrays
|
61
|
+
assert_equal([], parse('[]'))
|
62
|
+
assert_equal([], parse(' [ ] '))
|
63
|
+
assert_equal([nil], parse('[null]'))
|
64
|
+
assert_equal([false], parse('[false]'))
|
65
|
+
assert_equal([true], parse('[true]'))
|
66
|
+
assert_equal([-23], parse('[-23]'))
|
67
|
+
assert_equal([23], parse('[23]'))
|
68
|
+
assert_equal([0.23], parse('[0.23]'))
|
69
|
+
assert_equal([0.0], parse('[0e0]'))
|
70
|
+
assert_raises(JSON::ParserError) { parse('[+23.2]') }
|
71
|
+
assert_raises(JSON::ParserError) { parse('[+23]') }
|
72
|
+
assert_raises(JSON::ParserError) { parse('[.23]') }
|
73
|
+
assert_raises(JSON::ParserError) { parse('[023]') }
|
74
|
+
assert_equal_float [3.141], parse('[3.141]')
|
75
|
+
assert_equal_float [-3.141], parse('[-3.141]')
|
76
|
+
assert_equal_float [3.141], parse('[3141e-3]')
|
77
|
+
assert_equal_float [3.141], parse('[3141.1e-3]')
|
78
|
+
assert_equal_float [3.141], parse('[3141E-3]')
|
79
|
+
assert_equal_float [3.141], parse('[3141.0E-3]')
|
80
|
+
assert_equal_float [-3.141], parse('[-3141.0e-3]')
|
81
|
+
assert_equal_float [-3.141], parse('[-3141e-3]')
|
82
|
+
assert_raises(ParserError) { parse('[NaN]') }
|
83
|
+
assert parse('[NaN]', :allow_nan => true).first.nan?
|
84
|
+
assert_raises(ParserError) { parse('[Infinity]') }
|
85
|
+
assert_equal [1.0/0], parse('[Infinity]', :allow_nan => true)
|
86
|
+
assert_raises(ParserError) { parse('[-Infinity]') }
|
87
|
+
assert_equal [-1.0/0], parse('[-Infinity]', :allow_nan => true)
|
88
|
+
assert_equal([""], parse('[""]'))
|
89
|
+
assert_equal(["foobar"], parse('["foobar"]'))
|
90
|
+
assert_equal([{}], parse('[{}]'))
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_parse_simple_objects
|
94
|
+
assert_equal({}, parse('{}'))
|
95
|
+
assert_equal({}, parse(' { } '))
|
96
|
+
assert_equal({ "a" => nil }, parse('{ "a" : null}'))
|
97
|
+
assert_equal({ "a" => nil }, parse('{"a":null}'))
|
98
|
+
assert_equal({ "a" => false }, parse('{ "a" : false } '))
|
99
|
+
assert_equal({ "a" => false }, parse('{"a":false}'))
|
100
|
+
assert_raises(JSON::ParserError) { parse('{false}') }
|
101
|
+
assert_equal({ "a" => true }, parse('{"a":true}'))
|
102
|
+
assert_equal({ "a" => true }, parse(' { "a" : true } '))
|
103
|
+
assert_equal({ "a" => -23 }, parse(' { "a" : -23 } '))
|
104
|
+
assert_equal({ "a" => -23 }, parse(' { "a" : -23 } '))
|
105
|
+
assert_equal({ "a" => 23 }, parse('{"a":23 } '))
|
106
|
+
assert_equal({ "a" => 23 }, parse(' { "a" : 23 } '))
|
107
|
+
assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
|
108
|
+
assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
|
109
|
+
end
|
110
|
+
|
111
|
+
if Array.method_defined?(:permutation)
|
112
|
+
def test_parse_more_complex_arrays
|
113
|
+
a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
|
114
|
+
a.permutation.each do |perm|
|
115
|
+
json = pretty_generate(perm)
|
116
|
+
assert_equal perm, parse(json)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_parse_complex_objects
|
121
|
+
a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
|
122
|
+
a.permutation.each do |perm|
|
123
|
+
s = "a"
|
124
|
+
orig_obj = perm.inject({}) { |h, x| h[s.dup] = x; s = s.succ; h }
|
125
|
+
json = pretty_generate(orig_obj)
|
126
|
+
assert_equal orig_obj, parse(json)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_parse_arrays
|
132
|
+
assert_equal([1,2,3], parse('[1,2,3]'))
|
133
|
+
assert_equal([1.2,2,3], parse('[1.2,2,3]'))
|
134
|
+
assert_equal([[],[[],[]]], parse('[[],[[],[]]]'))
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_parse_values
|
138
|
+
assert_equal([""], parse('[""]'))
|
139
|
+
assert_equal(["\\"], parse('["\\\\"]'))
|
140
|
+
assert_equal(['"'], parse('["\""]'))
|
141
|
+
assert_equal(['\\"\\'], parse('["\\\\\\"\\\\"]'))
|
142
|
+
assert_equal(["\"\b\n\r\t\0\037"],
|
143
|
+
parse('["\"\b\n\r\t\u0000\u001f"]'))
|
144
|
+
for i in 0 ... @ary.size
|
145
|
+
assert_equal(@ary[i], parse(@ary_to_parse[i]))
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_parse_array
|
150
|
+
assert_equal([], parse('[]'))
|
151
|
+
assert_equal([], parse(' [ ] '))
|
152
|
+
assert_equal([1], parse('[1]'))
|
153
|
+
assert_equal([1], parse(' [ 1 ] '))
|
154
|
+
assert_equal(@ary,
|
155
|
+
parse('[[1],["foo"],[3.14],[47.11e+2],[2718.0E-3],[null],[[1,-2,3]]'\
|
156
|
+
',[false],[true]]'))
|
157
|
+
assert_equal(@ary, parse(%Q{ [ [1] , ["foo"] , [3.14] \t , [47.11e+2]
|
158
|
+
, [2718.0E-3 ],\r[ null] , [[1, -2, 3 ]], [false ],[ true]\n ] }))
|
159
|
+
end
|
160
|
+
|
161
|
+
class SubArray < Array; end
|
162
|
+
|
163
|
+
def test_parse_array_custom_class
|
164
|
+
res = parse('[]', :array_class => SubArray)
|
165
|
+
assert_equal([], res)
|
166
|
+
assert_equal(SubArray, res.class)
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_parse_object
|
170
|
+
assert_equal({}, parse('{}'))
|
171
|
+
assert_equal({}, parse(' { } '))
|
172
|
+
assert_equal({'foo'=>'bar'}, parse('{"foo":"bar"}'))
|
173
|
+
assert_equal({'foo'=>'bar'}, parse(' { "foo" : "bar" } '))
|
174
|
+
end
|
175
|
+
|
176
|
+
class SubHash < Hash
|
177
|
+
def to_json(*a)
|
178
|
+
{
|
179
|
+
JSON.create_id => self.class.name,
|
180
|
+
}.merge(self).to_json(*a)
|
181
|
+
end
|
182
|
+
|
183
|
+
def self.json_create(o)
|
184
|
+
o.delete JSON.create_id
|
185
|
+
new.merge(o)
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
def test_parse_object_custom_class
|
190
|
+
res = parse('{}', :object_class => SubHash)
|
191
|
+
assert_equal({}, res)
|
192
|
+
assert_equal(SubHash, res.class)
|
193
|
+
end
|
194
|
+
|
195
|
+
def test_generation_of_core_subclasses
|
196
|
+
obj = SubHash.new.merge( "foo" => SubHash.new.merge("bar" => true))
|
197
|
+
obj_json = JSON(obj)
|
198
|
+
obj_again = JSON(obj_json)
|
199
|
+
assert_kind_of SubHash, obj_again
|
200
|
+
assert_kind_of SubHash, obj_again['foo']
|
201
|
+
assert obj_again['foo']['bar']
|
202
|
+
assert_equal obj, obj_again
|
203
|
+
end
|
204
|
+
|
205
|
+
def test_parser_reset
|
206
|
+
parser = Parser.new(@json)
|
207
|
+
assert_equal(@hash, parser.parse)
|
208
|
+
assert_equal(@hash, parser.parse)
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_comments
|
212
|
+
json = <<EOT
|
213
|
+
{
|
214
|
+
"key1":"value1", // eol comment
|
215
|
+
"key2":"value2" /* multi line
|
216
|
+
* comment */,
|
217
|
+
"key3":"value3" /* multi line
|
218
|
+
// nested eol comment
|
219
|
+
* comment */
|
220
|
+
}
|
221
|
+
EOT
|
222
|
+
assert_equal(
|
223
|
+
{ "key1" => "value1", "key2" => "value2", "key3" => "value3" },
|
224
|
+
parse(json))
|
225
|
+
json = <<EOT
|
226
|
+
{
|
227
|
+
"key1":"value1" /* multi line
|
228
|
+
// nested eol comment
|
229
|
+
/* illegal nested multi line comment */
|
230
|
+
* comment */
|
231
|
+
}
|
232
|
+
EOT
|
233
|
+
assert_raises(ParserError) { parse(json) }
|
234
|
+
json = <<EOT
|
235
|
+
{
|
236
|
+
"key1":"value1" /* multi line
|
237
|
+
// nested eol comment
|
238
|
+
closed multi comment */
|
239
|
+
and again, throw an Error */
|
240
|
+
}
|
241
|
+
EOT
|
242
|
+
assert_raises(ParserError) { parse(json) }
|
243
|
+
json = <<EOT
|
244
|
+
{
|
245
|
+
"key1":"value1" /*/*/
|
246
|
+
}
|
247
|
+
EOT
|
248
|
+
assert_equal({ "key1" => "value1" }, parse(json))
|
249
|
+
end
|
250
|
+
|
251
|
+
def test_backslash
|
252
|
+
data = [ '\\.(?i:gif|jpe?g|png)$' ]
|
253
|
+
json = '["\\\\.(?i:gif|jpe?g|png)$"]'
|
254
|
+
assert_equal json, JSON.generate(data)
|
255
|
+
assert_equal data, JSON.parse(json)
|
256
|
+
#
|
257
|
+
data = [ '\\"' ]
|
258
|
+
json = '["\\\\\""]'
|
259
|
+
assert_equal json, JSON.generate(data)
|
260
|
+
assert_equal data, JSON.parse(json)
|
261
|
+
#
|
262
|
+
json = '["/"]'
|
263
|
+
data = JSON.parse(json)
|
264
|
+
assert_equal ['/'], data
|
265
|
+
assert_equal json, JSON.generate(data)
|
266
|
+
#
|
267
|
+
json = '["\""]'
|
268
|
+
data = JSON.parse(json)
|
269
|
+
assert_equal ['"'], data
|
270
|
+
assert_equal json, JSON.generate(data)
|
271
|
+
json = '["\\\'"]'
|
272
|
+
data = JSON.parse(json)
|
273
|
+
assert_equal ["'"], data
|
274
|
+
assert_equal '["\'"]', JSON.generate(data)
|
275
|
+
end
|
276
|
+
|
277
|
+
def test_wrong_inputs
|
278
|
+
assert_raises(ParserError) { JSON.parse('"foo"') }
|
279
|
+
assert_raises(ParserError) { JSON.parse('123') }
|
280
|
+
assert_raises(ParserError) { JSON.parse('[] bla') }
|
281
|
+
assert_raises(ParserError) { JSON.parse('[] 1') }
|
282
|
+
assert_raises(ParserError) { JSON.parse('[] []') }
|
283
|
+
assert_raises(ParserError) { JSON.parse('[] {}') }
|
284
|
+
assert_raises(ParserError) { JSON.parse('{} []') }
|
285
|
+
assert_raises(ParserError) { JSON.parse('{} {}') }
|
286
|
+
assert_raises(ParserError) { JSON.parse('[NULL]') }
|
287
|
+
assert_raises(ParserError) { JSON.parse('[FALSE]') }
|
288
|
+
assert_raises(ParserError) { JSON.parse('[TRUE]') }
|
289
|
+
assert_raises(ParserError) { JSON.parse('[07] ') }
|
290
|
+
assert_raises(ParserError) { JSON.parse('[0a]') }
|
291
|
+
assert_raises(ParserError) { JSON.parse('[1.]') }
|
292
|
+
assert_raises(ParserError) { JSON.parse(' ') }
|
293
|
+
end
|
294
|
+
|
295
|
+
def test_nesting
|
296
|
+
assert_raises(JSON::NestingError) { JSON.parse '[[]]', :max_nesting => 1 }
|
297
|
+
assert_raises(JSON::NestingError) { JSON.parser.new('[[]]', :max_nesting => 1).parse }
|
298
|
+
assert_equal [[]], JSON.parse('[[]]', :max_nesting => 2)
|
299
|
+
too_deep = '[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]'
|
300
|
+
too_deep_ary = eval too_deep
|
301
|
+
assert_raises(JSON::NestingError) { JSON.parse too_deep }
|
302
|
+
assert_raises(JSON::NestingError) { JSON.parser.new(too_deep).parse }
|
303
|
+
assert_raises(JSON::NestingError) { JSON.parse too_deep, :max_nesting => 19 }
|
304
|
+
ok = JSON.parse too_deep, :max_nesting => 20
|
305
|
+
assert_equal too_deep_ary, ok
|
306
|
+
ok = JSON.parse too_deep, :max_nesting => nil
|
307
|
+
assert_equal too_deep_ary, ok
|
308
|
+
ok = JSON.parse too_deep, :max_nesting => false
|
309
|
+
assert_equal too_deep_ary, ok
|
310
|
+
ok = JSON.parse too_deep, :max_nesting => 0
|
311
|
+
assert_equal too_deep_ary, ok
|
312
|
+
assert_raises(JSON::NestingError) { JSON.generate [[]], :max_nesting => 1 }
|
313
|
+
assert_equal '[[]]', JSON.generate([[]], :max_nesting => 2)
|
314
|
+
assert_raises(JSON::NestingError) { JSON.generate too_deep_ary }
|
315
|
+
assert_raises(JSON::NestingError) { JSON.generate too_deep_ary, :max_nesting => 19 }
|
316
|
+
ok = JSON.generate too_deep_ary, :max_nesting => 20
|
317
|
+
assert_equal too_deep, ok
|
318
|
+
ok = JSON.generate too_deep_ary, :max_nesting => nil
|
319
|
+
assert_equal too_deep, ok
|
320
|
+
ok = JSON.generate too_deep_ary, :max_nesting => false
|
321
|
+
assert_equal too_deep, ok
|
322
|
+
ok = JSON.generate too_deep_ary, :max_nesting => 0
|
323
|
+
assert_equal too_deep, ok
|
324
|
+
end
|
325
|
+
|
326
|
+
def test_symbolize_names
|
327
|
+
assert_equal({ "foo" => "bar", "baz" => "quux" },
|
328
|
+
JSON.parse('{"foo":"bar", "baz":"quux"}'))
|
329
|
+
assert_equal({ :foo => "bar", :baz => "quux" },
|
330
|
+
JSON.parse('{"foo":"bar", "baz":"quux"}', :symbolize_names => true))
|
331
|
+
end
|
332
|
+
|
333
|
+
def test_load_dump
|
334
|
+
too_deep = '[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]'
|
335
|
+
assert_equal too_deep, JSON.dump(eval(too_deep))
|
336
|
+
assert_kind_of String, Marshal.dump(eval(too_deep))
|
337
|
+
assert_raises(ArgumentError) { JSON.dump(eval(too_deep), 19) }
|
338
|
+
assert_raises(ArgumentError) { Marshal.dump(eval(too_deep), 19) }
|
339
|
+
assert_equal too_deep, JSON.dump(eval(too_deep), 20)
|
340
|
+
assert_kind_of String, Marshal.dump(eval(too_deep), 20)
|
341
|
+
output = StringIO.new
|
342
|
+
JSON.dump(eval(too_deep), output)
|
343
|
+
assert_equal too_deep, output.string
|
344
|
+
output = StringIO.new
|
345
|
+
JSON.dump(eval(too_deep), output, 20)
|
346
|
+
assert_equal too_deep, output.string
|
347
|
+
end
|
348
|
+
|
349
|
+
def test_big_integers
|
350
|
+
json1 = JSON([orig = (1 << 31) - 1])
|
351
|
+
assert_equal orig, JSON[json1][0]
|
352
|
+
json2 = JSON([orig = 1 << 31])
|
353
|
+
assert_equal orig, JSON[json2][0]
|
354
|
+
json3 = JSON([orig = (1 << 62) - 1])
|
355
|
+
assert_equal orig, JSON[json3][0]
|
356
|
+
json4 = JSON([orig = 1 << 62])
|
357
|
+
assert_equal orig, JSON[json4][0]
|
358
|
+
json5 = JSON([orig = 1 << 64])
|
359
|
+
assert_equal orig, JSON[json5][0]
|
360
|
+
end
|
361
|
+
end
|
@@ -0,0 +1,162 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# -*- coding:utf-8 -*-
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
case ENV['JSON']
|
6
|
+
when 'pure' then require 'json/pure'
|
7
|
+
when 'ext' then require 'json/ext'
|
8
|
+
else require 'json'
|
9
|
+
end
|
10
|
+
require 'json/add/core'
|
11
|
+
require 'date'
|
12
|
+
|
13
|
+
class TC_JSONAddition < Test::Unit::TestCase
|
14
|
+
include JSON
|
15
|
+
|
16
|
+
class A
|
17
|
+
def initialize(a)
|
18
|
+
@a = a
|
19
|
+
end
|
20
|
+
|
21
|
+
attr_reader :a
|
22
|
+
|
23
|
+
def ==(other)
|
24
|
+
a == other.a
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.json_create(object)
|
28
|
+
new(*object['args'])
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_json(*args)
|
32
|
+
{
|
33
|
+
'json_class' => self.class.name,
|
34
|
+
'args' => [ @a ],
|
35
|
+
}.to_json(*args)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class B
|
40
|
+
def self.json_creatable?
|
41
|
+
false
|
42
|
+
end
|
43
|
+
|
44
|
+
def to_json(*args)
|
45
|
+
{
|
46
|
+
'json_class' => self.class.name,
|
47
|
+
}.to_json(*args)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class C
|
52
|
+
def self.json_creatable?
|
53
|
+
false
|
54
|
+
end
|
55
|
+
|
56
|
+
def to_json(*args)
|
57
|
+
{
|
58
|
+
'json_class' => 'TC_JSONAddition::Nix',
|
59
|
+
}.to_json(*args)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_extended_json
|
64
|
+
a = A.new(666)
|
65
|
+
assert A.json_creatable?
|
66
|
+
json = generate(a)
|
67
|
+
a_again = JSON.parse(json)
|
68
|
+
assert_kind_of a.class, a_again
|
69
|
+
assert_equal a, a_again
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_extended_json_disabled
|
73
|
+
a = A.new(666)
|
74
|
+
assert A.json_creatable?
|
75
|
+
json = generate(a)
|
76
|
+
a_again = JSON.parse(json, :create_additions => true)
|
77
|
+
assert_kind_of a.class, a_again
|
78
|
+
assert_equal a, a_again
|
79
|
+
a_hash = JSON.parse(json, :create_additions => false)
|
80
|
+
assert_kind_of Hash, a_hash
|
81
|
+
assert_equal(
|
82
|
+
{"args"=>[666], "json_class"=>"TC_JSONAddition::A"}.sort_by { |k,| k },
|
83
|
+
a_hash.sort_by { |k,| k }
|
84
|
+
)
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_extended_json_fail1
|
88
|
+
b = B.new
|
89
|
+
assert !B.json_creatable?
|
90
|
+
json = generate(b)
|
91
|
+
assert_equal({ "json_class"=>"TC_JSONAddition::B" }, JSON.parse(json))
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_extended_json_fail2
|
95
|
+
c = C.new
|
96
|
+
assert !C.json_creatable?
|
97
|
+
json = generate(c)
|
98
|
+
assert_raises(ArgumentError, NameError) { JSON.parse(json) }
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_raw_strings
|
102
|
+
raw = ''
|
103
|
+
raw.respond_to?(:encode!) and raw.encode!(Encoding::ASCII_8BIT)
|
104
|
+
raw_array = []
|
105
|
+
for i in 0..255
|
106
|
+
raw << i
|
107
|
+
raw_array << i
|
108
|
+
end
|
109
|
+
json = raw.to_json_raw
|
110
|
+
json_raw_object = raw.to_json_raw_object
|
111
|
+
hash = { 'json_class' => 'String', 'raw'=> raw_array }
|
112
|
+
assert_equal hash, json_raw_object
|
113
|
+
assert_match /\A\{.*\}\Z/, json
|
114
|
+
assert_match /"json_class":"String"/, json
|
115
|
+
assert_match /"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\]/, json
|
116
|
+
raw_again = JSON.parse(json)
|
117
|
+
assert_equal raw, raw_again
|
118
|
+
end
|
119
|
+
|
120
|
+
MyJsonStruct = Struct.new 'MyJsonStruct', :foo, :bar
|
121
|
+
|
122
|
+
def test_core
|
123
|
+
t = Time.now
|
124
|
+
assert_equal t.inspect, JSON(JSON(t)).inspect
|
125
|
+
d = Date.today
|
126
|
+
assert_equal d, JSON(JSON(d))
|
127
|
+
d = DateTime.civil(2007, 6, 14, 14, 57, 10, Rational(1, 12), 2299161)
|
128
|
+
assert_equal d, JSON(JSON(d))
|
129
|
+
assert_equal 1..10, JSON(JSON(1..10))
|
130
|
+
assert_equal 1...10, JSON(JSON(1...10))
|
131
|
+
assert_equal "a".."c", JSON(JSON("a".."c"))
|
132
|
+
assert_equal "a"..."c", JSON(JSON("a"..."c"))
|
133
|
+
s = MyJsonStruct.new 4711, 'foot'
|
134
|
+
assert_equal s, JSON(JSON(s))
|
135
|
+
struct = Struct.new :foo, :bar
|
136
|
+
s = struct.new 4711, 'foot'
|
137
|
+
assert_raises(JSONError) { JSON(s) }
|
138
|
+
begin
|
139
|
+
raise TypeError, "test me"
|
140
|
+
rescue TypeError => e
|
141
|
+
e_json = JSON.generate e
|
142
|
+
e_again = JSON e_json
|
143
|
+
assert_kind_of TypeError, e_again
|
144
|
+
assert_equal e.message, e_again.message
|
145
|
+
assert_equal e.backtrace, e_again.backtrace
|
146
|
+
end
|
147
|
+
assert_equal(/foo/, JSON(JSON(/foo/)))
|
148
|
+
assert_equal(/foo/i, JSON(JSON(/foo/i)))
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_utc_datetime
|
152
|
+
now = Time.now
|
153
|
+
d = DateTime.parse(now.to_s) # usual case
|
154
|
+
assert_equal d, JSON.parse(d.to_json)
|
155
|
+
d = DateTime.parse(now.utc.to_s) # of = 0
|
156
|
+
assert_equal d, JSON.parse(d.to_json)
|
157
|
+
d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(1,24))
|
158
|
+
assert_equal d, JSON.parse(d.to_json)
|
159
|
+
d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(12,24))
|
160
|
+
assert_equal d, JSON.parse(d.to_json)
|
161
|
+
end
|
162
|
+
end
|