json 1.1.5-x86-linux
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 +106 -0
- data/GPL +340 -0
- data/README +78 -0
- data/RUBY +58 -0
- data/Rakefile +268 -0
- data/TODO +1 -0
- data/VERSION +1 -0
- data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkComparison.log +52 -0
- data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast-autocorrelation.dat +1000 -0
- data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast.dat +1001 -0
- data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty-autocorrelation.dat +900 -0
- data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty.dat +901 -0
- data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe-autocorrelation.dat +1000 -0
- data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe.dat +1001 -0
- data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt.log +261 -0
- data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast-autocorrelation.dat +1000 -0
- data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast.dat +1001 -0
- data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty-autocorrelation.dat +1000 -0
- data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty.dat +1001 -0
- data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe-autocorrelation.dat +1000 -0
- data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe.dat +1001 -0
- data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure.log +262 -0
- data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator-autocorrelation.dat +1000 -0
- data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator.dat +1001 -0
- data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails.log +82 -0
- data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkComparison.log +34 -0
- data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser-autocorrelation.dat +900 -0
- data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser.dat +901 -0
- data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt.log +81 -0
- data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser-autocorrelation.dat +1000 -0
- data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser.dat +1001 -0
- data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure.log +82 -0
- data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser-autocorrelation.dat +1000 -0
- data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser.dat +1001 -0
- data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails.log +82 -0
- data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser-autocorrelation.dat +1000 -0
- data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser.dat +1001 -0
- data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML.log +82 -0
- data/benchmarks/generator_benchmark.rb +165 -0
- data/benchmarks/parser_benchmark.rb +197 -0
- data/bin/edit_json.rb +9 -0
- data/bin/prettify_json.rb +75 -0
- data/data/example.json +1 -0
- data/data/index.html +38 -0
- data/data/prototype.js +4184 -0
- data/doc-templates/main.txt +283 -0
- data/ext/json/ext/generator/extconf.rb +11 -0
- data/ext/json/ext/generator/generator.c +919 -0
- data/ext/json/ext/generator/unicode.c +182 -0
- data/ext/json/ext/generator/unicode.h +53 -0
- data/ext/json/ext/parser/extconf.rb +11 -0
- data/ext/json/ext/parser/parser.c +1829 -0
- data/ext/json/ext/parser/parser.rl +686 -0
- data/ext/json/ext/parser/unicode.c +154 -0
- data/ext/json/ext/parser/unicode.h +58 -0
- data/install.rb +26 -0
- 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 +135 -0
- data/lib/json/add/rails.rb +58 -0
- data/lib/json/common.rb +354 -0
- data/lib/json/editor.rb +1371 -0
- data/lib/json/ext.rb +15 -0
- data/lib/json/ext/generator.so +0 -0
- data/lib/json/ext/parser.so +0 -0
- data/lib/json/json.xpm +1499 -0
- data/lib/json/pure.rb +77 -0
- data/lib/json/pure/generator.rb +430 -0
- data/lib/json/pure/parser.rb +267 -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 +312 -0
- data/tests/test_json_addition.rb +164 -0
- data/tests/test_json_fixtures.rb +34 -0
- data/tests/test_json_generate.rb +106 -0
- data/tests/test_json_rails.rb +146 -0
- data/tests/test_json_unicode.rb +62 -0
- data/tools/fuzz.rb +139 -0
- data/tools/server.rb +61 -0
- metadata +200 -0
@@ -0,0 +1,164 @@
|
|
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) { 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
|
+
json_raw = <<EOT.chomp
|
114
|
+
{\"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]}
|
115
|
+
EOT
|
116
|
+
# "
|
117
|
+
assert_equal json_raw, json
|
118
|
+
raw_again = JSON.parse(json)
|
119
|
+
assert_equal raw, raw_again
|
120
|
+
end
|
121
|
+
|
122
|
+
MyJsonStruct = Struct.new 'MyJsonStruct', :foo, :bar
|
123
|
+
|
124
|
+
def test_core
|
125
|
+
t = Time.now
|
126
|
+
assert_equal t.inspect, JSON(JSON(t)).inspect
|
127
|
+
d = Date.today
|
128
|
+
assert_equal d, JSON(JSON(d))
|
129
|
+
d = DateTime.civil(2007, 6, 14, 14, 57, 10, Rational(1, 12), 2299161)
|
130
|
+
assert_equal d, JSON(JSON(d))
|
131
|
+
assert_equal 1..10, JSON(JSON(1..10))
|
132
|
+
assert_equal 1...10, JSON(JSON(1...10))
|
133
|
+
assert_equal "a".."c", JSON(JSON("a".."c"))
|
134
|
+
assert_equal "a"..."c", JSON(JSON("a"..."c"))
|
135
|
+
s = MyJsonStruct.new 4711, 'foot'
|
136
|
+
assert_equal s, JSON(JSON(s))
|
137
|
+
struct = Struct.new :foo, :bar
|
138
|
+
s = struct.new 4711, 'foot'
|
139
|
+
assert_raises(JSONError) { JSON(s) }
|
140
|
+
begin
|
141
|
+
raise TypeError, "test me"
|
142
|
+
rescue TypeError => e
|
143
|
+
e_json = JSON.generate e
|
144
|
+
e_again = JSON e_json
|
145
|
+
assert_kind_of TypeError, e_again
|
146
|
+
assert_equal e.message, e_again.message
|
147
|
+
assert_equal e.backtrace, e_again.backtrace
|
148
|
+
end
|
149
|
+
assert_equal(/foo/, JSON(JSON(/foo/)))
|
150
|
+
assert_equal(/foo/i, JSON(JSON(/foo/i)))
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_utc_datetime
|
154
|
+
now = Time.now
|
155
|
+
d = DateTime.parse(now.to_s) # usual case
|
156
|
+
assert d, JSON.parse(d.to_json)
|
157
|
+
d = DateTime.parse(now.utc.to_s) # of = 0
|
158
|
+
assert d, JSON.parse(d.to_json)
|
159
|
+
d = DateTime.civil(2008, 6, 17, 11, 48, 32, 1) # of = 1 / 12 => 1/12
|
160
|
+
assert d, JSON.parse(d.to_json)
|
161
|
+
d = DateTime.civil(2008, 6, 17, 11, 48, 32, 12) # of = 12 / 12 => 12
|
162
|
+
assert d, JSON.parse(d.to_json)
|
163
|
+
end
|
164
|
+
end
|
@@ -0,0 +1,34 @@
|
|
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
|
+
|
11
|
+
class TC_JSONFixtures < Test::Unit::TestCase
|
12
|
+
def setup
|
13
|
+
fixtures = File.join(File.dirname(__FILE__), 'fixtures/*.json')
|
14
|
+
passed, failed = Dir[fixtures].partition { |f| f['pass'] }
|
15
|
+
@passed = passed.inject([]) { |a, f| a << [ f, File.read(f) ] }.sort
|
16
|
+
@failed = failed.inject([]) { |a, f| a << [ f, File.read(f) ] }.sort
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_passing
|
20
|
+
for name, source in @passed
|
21
|
+
assert JSON.parse(source),
|
22
|
+
"Did not pass for fixture '#{name}'"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_failing
|
27
|
+
for name, source in @failed
|
28
|
+
assert_raises(JSON::ParserError, JSON::NestingError,
|
29
|
+
"Did not fail for fixture '#{name}'") do
|
30
|
+
JSON.parse(source)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,106 @@
|
|
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
|
+
|
11
|
+
class TC_JSONGenerate < Test::Unit::TestCase
|
12
|
+
include JSON
|
13
|
+
|
14
|
+
def setup
|
15
|
+
@hash = {
|
16
|
+
'a' => 2,
|
17
|
+
'b' => 3.141,
|
18
|
+
'c' => 'c',
|
19
|
+
'd' => [ 1, "b", 3.14 ],
|
20
|
+
'e' => { 'foo' => 'bar' },
|
21
|
+
'g' => "\"\0\037",
|
22
|
+
'h' => 1000.0,
|
23
|
+
'i' => 0.001
|
24
|
+
}
|
25
|
+
@json2 = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' +
|
26
|
+
'"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}'
|
27
|
+
@json3 = <<'EOT'.chomp
|
28
|
+
{
|
29
|
+
"a": 2,
|
30
|
+
"b": 3.141,
|
31
|
+
"c": "c",
|
32
|
+
"d": [
|
33
|
+
1,
|
34
|
+
"b",
|
35
|
+
3.14
|
36
|
+
],
|
37
|
+
"e": {
|
38
|
+
"foo": "bar"
|
39
|
+
},
|
40
|
+
"g": "\"\u0000\u001f",
|
41
|
+
"h": 1000.0,
|
42
|
+
"i": 0.001
|
43
|
+
}
|
44
|
+
EOT
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_unparse
|
48
|
+
json = unparse(@hash)
|
49
|
+
assert_equal(JSON.parse(@json2), JSON.parse(json))
|
50
|
+
parsed_json = parse(json)
|
51
|
+
assert_equal(@hash, parsed_json)
|
52
|
+
json = generate({1=>2})
|
53
|
+
assert_equal('{"1":2}', json)
|
54
|
+
parsed_json = parse(json)
|
55
|
+
assert_equal({"1"=>2}, parsed_json)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_unparse_pretty
|
59
|
+
json = pretty_unparse(@hash)
|
60
|
+
assert_equal(JSON.parse(@json3), JSON.parse(json))
|
61
|
+
parsed_json = parse(json)
|
62
|
+
assert_equal(@hash, parsed_json)
|
63
|
+
json = pretty_generate({1=>2})
|
64
|
+
assert_equal(<<'EOT'.chomp, json)
|
65
|
+
{
|
66
|
+
"1": 2
|
67
|
+
}
|
68
|
+
EOT
|
69
|
+
parsed_json = parse(json)
|
70
|
+
assert_equal({"1"=>2}, parsed_json)
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_states
|
74
|
+
json = generate({1=>2}, nil)
|
75
|
+
assert_equal('{"1":2}', json)
|
76
|
+
s = JSON.state.new(:check_circular => true)
|
77
|
+
#assert s.check_circular
|
78
|
+
h = { 1=>2 }
|
79
|
+
h[3] = h
|
80
|
+
assert_raises(JSON::CircularDatastructure) { generate(h) }
|
81
|
+
assert_raises(JSON::CircularDatastructure) { generate(h, s) }
|
82
|
+
s = JSON.state.new(:check_circular => true)
|
83
|
+
#assert s.check_circular
|
84
|
+
a = [ 1, 2 ]
|
85
|
+
a << a
|
86
|
+
assert_raises(JSON::CircularDatastructure) { generate(a, s) }
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_allow_nan
|
90
|
+
assert_raises(GeneratorError) { generate([JSON::NaN]) }
|
91
|
+
assert_equal '[NaN]', generate([JSON::NaN], :allow_nan => true)
|
92
|
+
assert_equal '[NaN]', fast_generate([JSON::NaN])
|
93
|
+
assert_raises(GeneratorError) { pretty_generate([JSON::NaN]) }
|
94
|
+
assert_equal "[\n NaN\n]", pretty_generate([JSON::NaN], :allow_nan => true)
|
95
|
+
assert_raises(GeneratorError) { generate([JSON::Infinity]) }
|
96
|
+
assert_equal '[Infinity]', generate([JSON::Infinity], :allow_nan => true)
|
97
|
+
assert_equal '[Infinity]', fast_generate([JSON::Infinity])
|
98
|
+
assert_raises(GeneratorError) { pretty_generate([JSON::Infinity]) }
|
99
|
+
assert_equal "[\n Infinity\n]", pretty_generate([JSON::Infinity], :allow_nan => true)
|
100
|
+
assert_raises(GeneratorError) { generate([JSON::MinusInfinity]) }
|
101
|
+
assert_equal '[-Infinity]', generate([JSON::MinusInfinity], :allow_nan => true)
|
102
|
+
assert_equal '[-Infinity]', fast_generate([JSON::MinusInfinity])
|
103
|
+
assert_raises(GeneratorError) { pretty_generate([JSON::MinusInfinity]) }
|
104
|
+
assert_equal "[\n -Infinity\n]", pretty_generate([JSON::MinusInfinity], :allow_nan => true)
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,146 @@
|
|
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/rails'
|
11
|
+
require 'date'
|
12
|
+
|
13
|
+
class TC_JSONRails < 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 to_json(*args)
|
53
|
+
{
|
54
|
+
'json_class' => 'TC_JSONRails::Nix',
|
55
|
+
}.to_json(*args)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class D
|
60
|
+
def initialize
|
61
|
+
@foo = 666
|
62
|
+
end
|
63
|
+
|
64
|
+
attr_reader :foo
|
65
|
+
|
66
|
+
def ==(other)
|
67
|
+
foo == other.foo
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_extended_json
|
72
|
+
a = A.new(666)
|
73
|
+
assert A.json_creatable?
|
74
|
+
assert_equal 666, a.a
|
75
|
+
json = generate(a)
|
76
|
+
a_again = JSON.parse(json)
|
77
|
+
assert_kind_of a.class, a_again
|
78
|
+
assert_equal a, a_again
|
79
|
+
assert_equal 666, a_again.a
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_extended_json_generic_object
|
83
|
+
d = D.new
|
84
|
+
assert D.json_creatable?
|
85
|
+
assert_equal 666, d.foo
|
86
|
+
json = generate(d)
|
87
|
+
d_again = JSON.parse(json)
|
88
|
+
assert_kind_of d.class, d_again
|
89
|
+
assert_equal d, d_again
|
90
|
+
assert_equal 666, d_again.foo
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_extended_json_disabled
|
94
|
+
a = A.new(666)
|
95
|
+
assert A.json_creatable?
|
96
|
+
json = generate(a)
|
97
|
+
a_again = JSON.parse(json, :create_additions => true)
|
98
|
+
assert_kind_of a.class, a_again
|
99
|
+
assert_equal a, a_again
|
100
|
+
a_hash = JSON.parse(json, :create_additions => false)
|
101
|
+
assert_kind_of Hash, a_hash
|
102
|
+
assert_equal(
|
103
|
+
{"args"=>[666], "json_class"=>"TC_JSONRails::A"}.sort_by { |k,| k },
|
104
|
+
a_hash.sort_by { |k,| k }
|
105
|
+
)
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_extended_json_fail1
|
109
|
+
b = B.new
|
110
|
+
assert !B.json_creatable?
|
111
|
+
json = generate(b)
|
112
|
+
assert_equal({ 'json_class' => B.name }, JSON.parse(json))
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_extended_json_fail2
|
116
|
+
c = C.new # with rails addition all objects are theoretically creatable
|
117
|
+
assert C.json_creatable?
|
118
|
+
json = generate(c)
|
119
|
+
assert_raises(ArgumentError) { JSON.parse(json) }
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_raw_strings
|
123
|
+
raw = ''
|
124
|
+
raw.respond_to?(:encode!) and raw.encode!(Encoding::ASCII_8BIT)
|
125
|
+
raw_array = []
|
126
|
+
for i in 0..255
|
127
|
+
raw << i
|
128
|
+
raw_array << i
|
129
|
+
end
|
130
|
+
json = raw.to_json_raw
|
131
|
+
json_raw_object = raw.to_json_raw_object
|
132
|
+
hash = { 'json_class' => 'String', 'raw'=> raw_array }
|
133
|
+
assert_equal hash, json_raw_object
|
134
|
+
json_raw = <<EOT.chomp
|
135
|
+
{\"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]}
|
136
|
+
EOT
|
137
|
+
# "
|
138
|
+
assert_equal json_raw, json
|
139
|
+
raw_again = JSON.parse(json)
|
140
|
+
assert_equal raw, raw_again
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_symbol
|
144
|
+
assert_equal '"foo"', JSON(:foo) # we don't want an object here
|
145
|
+
end
|
146
|
+
end
|