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/test_json_addition.rb
DELETED
@@ -1,196 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# -*- coding:utf-8 -*-
|
3
|
-
|
4
|
-
require 'test/unit'
|
5
|
-
require File.join(File.dirname(__FILE__), 'setup_variant')
|
6
|
-
require 'json/add/core'
|
7
|
-
require 'json/add/complex'
|
8
|
-
require 'json/add/rational'
|
9
|
-
require 'json/add/bigdecimal'
|
10
|
-
require 'json/add/ostruct'
|
11
|
-
require 'date'
|
12
|
-
|
13
|
-
class TestJSONAddition < 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 A2 < A
|
40
|
-
def to_json(*args)
|
41
|
-
{
|
42
|
-
'json_class' => self.class.name,
|
43
|
-
'args' => [ @a ],
|
44
|
-
}.to_json(*args)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
class B
|
49
|
-
def self.json_creatable?
|
50
|
-
false
|
51
|
-
end
|
52
|
-
|
53
|
-
def to_json(*args)
|
54
|
-
{
|
55
|
-
'json_class' => self.class.name,
|
56
|
-
}.to_json(*args)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
class C
|
61
|
-
def self.json_creatable?
|
62
|
-
false
|
63
|
-
end
|
64
|
-
|
65
|
-
def to_json(*args)
|
66
|
-
{
|
67
|
-
'json_class' => 'TestJSONAddition::Nix',
|
68
|
-
}.to_json(*args)
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
def test_extended_json
|
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
|
-
end
|
80
|
-
|
81
|
-
def test_extended_json_default
|
82
|
-
a = A.new(666)
|
83
|
-
assert A.json_creatable?
|
84
|
-
json = generate(a)
|
85
|
-
a_hash = JSON.parse(json)
|
86
|
-
assert_kind_of Hash, a_hash
|
87
|
-
end
|
88
|
-
|
89
|
-
def test_extended_json_disabled
|
90
|
-
a = A.new(666)
|
91
|
-
assert A.json_creatable?
|
92
|
-
json = generate(a)
|
93
|
-
a_again = JSON.parse(json, :create_additions => true)
|
94
|
-
assert_kind_of a.class, a_again
|
95
|
-
assert_equal a, a_again
|
96
|
-
a_hash = JSON.parse(json, :create_additions => false)
|
97
|
-
assert_kind_of Hash, a_hash
|
98
|
-
assert_equal(
|
99
|
-
{"args"=>[666], "json_class"=>"TestJSONAddition::A"}.sort_by { |k,| k },
|
100
|
-
a_hash.sort_by { |k,| k }
|
101
|
-
)
|
102
|
-
end
|
103
|
-
|
104
|
-
def test_extended_json_fail1
|
105
|
-
b = B.new
|
106
|
-
assert !B.json_creatable?
|
107
|
-
json = generate(b)
|
108
|
-
assert_equal({ "json_class"=>"TestJSONAddition::B" }, JSON.parse(json))
|
109
|
-
end
|
110
|
-
|
111
|
-
def test_extended_json_fail2
|
112
|
-
c = C.new
|
113
|
-
assert !C.json_creatable?
|
114
|
-
json = generate(c)
|
115
|
-
assert_raises(ArgumentError, NameError) { JSON.parse(json, :create_additions => true) }
|
116
|
-
end
|
117
|
-
|
118
|
-
def test_raw_strings
|
119
|
-
raw = ''
|
120
|
-
raw.respond_to?(:encode!) and raw.encode!(Encoding::ASCII_8BIT)
|
121
|
-
raw_array = []
|
122
|
-
for i in 0..255
|
123
|
-
raw << i
|
124
|
-
raw_array << i
|
125
|
-
end
|
126
|
-
json = raw.to_json_raw
|
127
|
-
json_raw_object = raw.to_json_raw_object
|
128
|
-
hash = { 'json_class' => 'String', 'raw'=> raw_array }
|
129
|
-
assert_equal hash, json_raw_object
|
130
|
-
assert_match(/\A\{.*\}\z/, json)
|
131
|
-
assert_match(/"json_class":"String"/, json)
|
132
|
-
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)
|
133
|
-
raw_again = JSON.parse(json, :create_additions => true)
|
134
|
-
assert_equal raw, raw_again
|
135
|
-
end
|
136
|
-
|
137
|
-
MyJsonStruct = Struct.new 'MyJsonStruct', :foo, :bar
|
138
|
-
|
139
|
-
def test_core
|
140
|
-
t = Time.now
|
141
|
-
assert_equal t, JSON(JSON(t), :create_additions => true)
|
142
|
-
d = Date.today
|
143
|
-
assert_equal d, JSON(JSON(d), :create_additions => true)
|
144
|
-
d = DateTime.civil(2007, 6, 14, 14, 57, 10, Rational(1, 12), 2299161)
|
145
|
-
assert_equal d, JSON(JSON(d), :create_additions => true)
|
146
|
-
assert_equal 1..10, JSON(JSON(1..10), :create_additions => true)
|
147
|
-
assert_equal 1...10, JSON(JSON(1...10), :create_additions => true)
|
148
|
-
assert_equal "a".."c", JSON(JSON("a".."c"), :create_additions => true)
|
149
|
-
assert_equal "a"..."c", JSON(JSON("a"..."c"), :create_additions => true)
|
150
|
-
s = MyJsonStruct.new 4711, 'foot'
|
151
|
-
assert_equal s, JSON(JSON(s), :create_additions => true)
|
152
|
-
struct = Struct.new :foo, :bar
|
153
|
-
s = struct.new 4711, 'foot'
|
154
|
-
assert_raises(JSONError) { JSON(s) }
|
155
|
-
begin
|
156
|
-
raise TypeError, "test me"
|
157
|
-
rescue TypeError => e
|
158
|
-
e_json = JSON.generate e
|
159
|
-
e_again = JSON e_json, :create_additions => true
|
160
|
-
assert_kind_of TypeError, e_again
|
161
|
-
assert_equal e.message, e_again.message
|
162
|
-
assert_equal e.backtrace, e_again.backtrace
|
163
|
-
end
|
164
|
-
assert_equal(/foo/, JSON(JSON(/foo/), :create_additions => true))
|
165
|
-
assert_equal(/foo/i, JSON(JSON(/foo/i), :create_additions => true))
|
166
|
-
end
|
167
|
-
|
168
|
-
def test_utc_datetime
|
169
|
-
now = Time.now
|
170
|
-
d = DateTime.parse(now.to_s, :create_additions => true) # usual case
|
171
|
-
assert_equal d, JSON.parse(d.to_json, :create_additions => true)
|
172
|
-
d = DateTime.parse(now.utc.to_s) # of = 0
|
173
|
-
assert_equal d, JSON.parse(d.to_json, :create_additions => true)
|
174
|
-
d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(1,24))
|
175
|
-
assert_equal d, JSON.parse(d.to_json, :create_additions => true)
|
176
|
-
d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(12,24))
|
177
|
-
assert_equal d, JSON.parse(d.to_json, :create_additions => true)
|
178
|
-
end
|
179
|
-
|
180
|
-
def test_rational_complex
|
181
|
-
assert_equal Rational(2, 9), JSON.parse(JSON(Rational(2, 9)), :create_additions => true)
|
182
|
-
assert_equal Complex(2, 9), JSON.parse(JSON(Complex(2, 9)), :create_additions => true)
|
183
|
-
end
|
184
|
-
|
185
|
-
def test_bigdecimal
|
186
|
-
assert_equal BigDecimal('3.141', 23), JSON(JSON(BigDecimal('3.141', 23)), :create_additions => true)
|
187
|
-
assert_equal BigDecimal('3.141', 666), JSON(JSON(BigDecimal('3.141', 666)), :create_additions => true)
|
188
|
-
end
|
189
|
-
|
190
|
-
def test_ostruct
|
191
|
-
o = OpenStruct.new
|
192
|
-
# XXX this won't work; o.foo = { :bar => true }
|
193
|
-
o.foo = { 'bar' => true }
|
194
|
-
assert_equal o, JSON.parse(JSON(o), :create_additions => true)
|
195
|
-
end
|
196
|
-
end
|
data/tests/test_json_encoding.rb
DELETED
@@ -1,65 +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
|
-
|
7
|
-
class TestJSONEncoding < Test::Unit::TestCase
|
8
|
-
include JSON
|
9
|
-
|
10
|
-
def setup
|
11
|
-
@utf_8 = '["© ≠ €!"]'
|
12
|
-
@parsed = [ "© ≠ €!" ]
|
13
|
-
@generated = '["\u00a9 \u2260 \u20ac!"]'
|
14
|
-
if String.method_defined?(:encode)
|
15
|
-
@utf_16_data = [@parsed.first.encode('utf-16be', 'utf-8')]
|
16
|
-
@utf_8_ascii_8bit = @utf_8.dup.force_encoding(Encoding::ASCII_8BIT)
|
17
|
-
@utf_16be = @utf_8.encode('utf-16be', 'utf-8')
|
18
|
-
@utf_16be_ascii_8bit = @utf_16be.dup.force_encoding(Encoding::ASCII_8BIT)
|
19
|
-
@utf_16le = @utf_8.encode('utf-16le', 'utf-8')
|
20
|
-
@utf_16le_ascii_8bit = @utf_16le.dup.force_encoding(Encoding::ASCII_8BIT)
|
21
|
-
@utf_32be = @utf_8.encode('utf-32be', 'utf-8')
|
22
|
-
@utf_32be_ascii_8bit = @utf_32be.dup.force_encoding(Encoding::ASCII_8BIT)
|
23
|
-
@utf_32le = @utf_8.encode('utf-32le', 'utf-8')
|
24
|
-
@utf_32le_ascii_8bit = @utf_32le.dup.force_encoding(Encoding::ASCII_8BIT)
|
25
|
-
else
|
26
|
-
require 'iconv'
|
27
|
-
@utf_16_data = Iconv.iconv('utf-16be', 'utf-8', @parsed.first)
|
28
|
-
@utf_8_ascii_8bit = @utf_8.dup
|
29
|
-
@utf_16be, = Iconv.iconv('utf-16be', 'utf-8', @utf_8)
|
30
|
-
@utf_16be_ascii_8bit = @utf_16be.dup
|
31
|
-
@utf_16le, = Iconv.iconv('utf-16le', 'utf-8', @utf_8)
|
32
|
-
@utf_16le_ascii_8bit = @utf_16le.dup
|
33
|
-
@utf_32be, = Iconv.iconv('utf-32be', 'utf-8', @utf_8)
|
34
|
-
@utf_32be_ascii_8bit = @utf_32be.dup
|
35
|
-
@utf_32le, = Iconv.iconv('utf-32le', 'utf-8', @utf_8)
|
36
|
-
@utf_32le_ascii_8bit = @utf_32le.dup
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_parse
|
41
|
-
assert_equal @parsed, JSON.parse(@utf_8)
|
42
|
-
assert_equal @parsed, JSON.parse(@utf_16be)
|
43
|
-
assert_equal @parsed, JSON.parse(@utf_16le)
|
44
|
-
assert_equal @parsed, JSON.parse(@utf_32be)
|
45
|
-
assert_equal @parsed, JSON.parse(@utf_32le)
|
46
|
-
end
|
47
|
-
|
48
|
-
def test_parse_ascii_8bit
|
49
|
-
assert_equal @parsed, JSON.parse(@utf_8_ascii_8bit)
|
50
|
-
assert_equal @parsed, JSON.parse(@utf_16be_ascii_8bit)
|
51
|
-
assert_equal @parsed, JSON.parse(@utf_16le_ascii_8bit)
|
52
|
-
assert_equal @parsed, JSON.parse(@utf_32be_ascii_8bit)
|
53
|
-
assert_equal @parsed, JSON.parse(@utf_32le_ascii_8bit)
|
54
|
-
end
|
55
|
-
|
56
|
-
def test_generate
|
57
|
-
assert_equal @generated, JSON.generate(@parsed, :ascii_only => true)
|
58
|
-
if defined?(::Encoding)
|
59
|
-
assert_equal @generated, JSON.generate(@utf_16_data, :ascii_only => true)
|
60
|
-
else
|
61
|
-
# XXX checking of correct utf8 data is not as strict (yet?) without :ascii_only
|
62
|
-
assert_raises(JSON::GeneratorError) { JSON.generate(@utf_16_data, :ascii_only => true) }
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
data/tests/test_json_fixtures.rb
DELETED
@@ -1,35 +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
|
-
|
7
|
-
class TestJSONFixtures < Test::Unit::TestCase
|
8
|
-
def setup
|
9
|
-
fixtures = File.join(File.dirname(__FILE__), 'fixtures/*.json')
|
10
|
-
passed, failed = Dir[fixtures].partition { |f| f['pass'] }
|
11
|
-
@passed = passed.inject([]) { |a, f| a << [ f, File.read(f) ] }.sort
|
12
|
-
@failed = failed.inject([]) { |a, f| a << [ f, File.read(f) ] }.sort
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_passing
|
16
|
-
for name, source in @passed
|
17
|
-
begin
|
18
|
-
assert JSON.parse(source),
|
19
|
-
"Did not pass for fixture '#{name}': #{source.inspect}"
|
20
|
-
rescue => e
|
21
|
-
warn "\nCaught #{e.class}(#{e}) for fixture '#{name}': #{source.inspect}\n#{e.backtrace * "\n"}"
|
22
|
-
raise e
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_failing
|
28
|
-
for name, source in @failed
|
29
|
-
assert_raises(JSON::ParserError, JSON::NestingError,
|
30
|
-
"Did not fail for fixture '#{name}': #{source.inspect}") do
|
31
|
-
JSON.parse(source)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
data/tests/test_json_generate.rb
DELETED
@@ -1,348 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# encoding: utf-8
|
3
|
-
# frozen_string_literal: false
|
4
|
-
|
5
|
-
require 'test/unit'
|
6
|
-
require File.join(File.dirname(__FILE__), 'setup_variant')
|
7
|
-
|
8
|
-
class TestJSONGenerate < Test::Unit::TestCase
|
9
|
-
include JSON
|
10
|
-
|
11
|
-
def setup
|
12
|
-
@hash = {
|
13
|
-
'a' => 2,
|
14
|
-
'b' => 3.141,
|
15
|
-
'c' => 'c',
|
16
|
-
'd' => [ 1, "b", 3.14 ],
|
17
|
-
'e' => { 'foo' => 'bar' },
|
18
|
-
'g' => "\"\0\037",
|
19
|
-
'h' => 1000.0,
|
20
|
-
'i' => 0.001
|
21
|
-
}
|
22
|
-
@json2 = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' +
|
23
|
-
'"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}'
|
24
|
-
@json3 = <<'EOT'.chomp
|
25
|
-
{
|
26
|
-
"a": 2,
|
27
|
-
"b": 3.141,
|
28
|
-
"c": "c",
|
29
|
-
"d": [
|
30
|
-
1,
|
31
|
-
"b",
|
32
|
-
3.14
|
33
|
-
],
|
34
|
-
"e": {
|
35
|
-
"foo": "bar"
|
36
|
-
},
|
37
|
-
"g": "\"\u0000\u001f",
|
38
|
-
"h": 1000.0,
|
39
|
-
"i": 0.001
|
40
|
-
}
|
41
|
-
EOT
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_generate
|
45
|
-
json = generate(@hash)
|
46
|
-
assert_equal(JSON.parse(@json2), JSON.parse(json))
|
47
|
-
json = JSON[@hash]
|
48
|
-
assert_equal(JSON.parse(@json2), JSON.parse(json))
|
49
|
-
parsed_json = parse(json)
|
50
|
-
assert_equal(@hash, parsed_json)
|
51
|
-
json = generate({1=>2})
|
52
|
-
assert_equal('{"1":2}', json)
|
53
|
-
parsed_json = parse(json)
|
54
|
-
assert_equal({"1"=>2}, parsed_json)
|
55
|
-
assert_raise(GeneratorError) { generate(666) }
|
56
|
-
assert_equal '666', generate(666, :quirks_mode => true)
|
57
|
-
end
|
58
|
-
|
59
|
-
def test_generate_pretty
|
60
|
-
json = pretty_generate(@hash)
|
61
|
-
# hashes aren't (insertion) ordered on every ruby implementation assert_equal(@json3, json)
|
62
|
-
assert_equal(JSON.parse(@json3), JSON.parse(json))
|
63
|
-
parsed_json = parse(json)
|
64
|
-
assert_equal(@hash, parsed_json)
|
65
|
-
json = pretty_generate({1=>2})
|
66
|
-
assert_equal(<<'EOT'.chomp, json)
|
67
|
-
{
|
68
|
-
"1": 2
|
69
|
-
}
|
70
|
-
EOT
|
71
|
-
parsed_json = parse(json)
|
72
|
-
assert_equal({"1"=>2}, parsed_json)
|
73
|
-
assert_raise(GeneratorError) { pretty_generate(666) }
|
74
|
-
assert_equal '666', pretty_generate(666, :quirks_mode => true)
|
75
|
-
end
|
76
|
-
|
77
|
-
def test_generate_custom
|
78
|
-
state = State.new(:space_before => " ", :space => " ", :indent => "<i>", :object_nl => "\n", :array_nl => "<a_nl>")
|
79
|
-
json = generate({1=>{2=>3,4=>[5,6]}}, state)
|
80
|
-
assert_equal(<<'EOT'.chomp, json)
|
81
|
-
{
|
82
|
-
<i>"1" : {
|
83
|
-
<i><i>"2" : 3,
|
84
|
-
<i><i>"4" : [<a_nl><i><i><i>5,<a_nl><i><i><i>6<a_nl><i><i>]
|
85
|
-
<i>}
|
86
|
-
}
|
87
|
-
EOT
|
88
|
-
end
|
89
|
-
|
90
|
-
def test_fast_generate
|
91
|
-
json = fast_generate(@hash)
|
92
|
-
assert_equal(JSON.parse(@json2), JSON.parse(json))
|
93
|
-
parsed_json = parse(json)
|
94
|
-
assert_equal(@hash, parsed_json)
|
95
|
-
json = fast_generate({1=>2})
|
96
|
-
assert_equal('{"1":2}', json)
|
97
|
-
parsed_json = parse(json)
|
98
|
-
assert_equal({"1"=>2}, parsed_json)
|
99
|
-
assert_raise(GeneratorError) { fast_generate(666) }
|
100
|
-
assert_equal '666', fast_generate(666, :quirks_mode => true)
|
101
|
-
end
|
102
|
-
|
103
|
-
def test_own_state
|
104
|
-
state = State.new
|
105
|
-
json = generate(@hash, state)
|
106
|
-
assert_equal(JSON.parse(@json2), JSON.parse(json))
|
107
|
-
parsed_json = parse(json)
|
108
|
-
assert_equal(@hash, parsed_json)
|
109
|
-
json = generate({1=>2}, state)
|
110
|
-
assert_equal('{"1":2}', json)
|
111
|
-
parsed_json = parse(json)
|
112
|
-
assert_equal({"1"=>2}, parsed_json)
|
113
|
-
assert_raise(GeneratorError) { generate(666, state) }
|
114
|
-
state.quirks_mode = true
|
115
|
-
assert state.quirks_mode?
|
116
|
-
assert_equal '666', generate(666, state)
|
117
|
-
end
|
118
|
-
|
119
|
-
def test_states
|
120
|
-
json = generate({1=>2}, nil)
|
121
|
-
assert_equal('{"1":2}', json)
|
122
|
-
s = JSON.state.new
|
123
|
-
assert s.check_circular?
|
124
|
-
assert s[:check_circular?]
|
125
|
-
h = { 1=>2 }
|
126
|
-
h[3] = h
|
127
|
-
assert_raises(JSON::NestingError) { generate(h) }
|
128
|
-
assert_raises(JSON::NestingError) { generate(h, s) }
|
129
|
-
s = JSON.state.new
|
130
|
-
a = [ 1, 2 ]
|
131
|
-
a << a
|
132
|
-
assert_raises(JSON::NestingError) { generate(a, s) }
|
133
|
-
assert s.check_circular?
|
134
|
-
assert s[:check_circular?]
|
135
|
-
end
|
136
|
-
|
137
|
-
def test_pretty_state
|
138
|
-
state = PRETTY_STATE_PROTOTYPE.dup
|
139
|
-
assert_equal({
|
140
|
-
:allow_nan => false,
|
141
|
-
:array_nl => "\n",
|
142
|
-
:ascii_only => false,
|
143
|
-
:buffer_initial_length => 1024,
|
144
|
-
:quirks_mode => false,
|
145
|
-
:depth => 0,
|
146
|
-
:indent => " ",
|
147
|
-
:max_nesting => 100,
|
148
|
-
:object_nl => "\n",
|
149
|
-
:space => " ",
|
150
|
-
:space_before => "",
|
151
|
-
}.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
|
152
|
-
end
|
153
|
-
|
154
|
-
def test_safe_state
|
155
|
-
state = SAFE_STATE_PROTOTYPE.dup
|
156
|
-
assert_equal({
|
157
|
-
:allow_nan => false,
|
158
|
-
:array_nl => "",
|
159
|
-
:ascii_only => false,
|
160
|
-
:buffer_initial_length => 1024,
|
161
|
-
:quirks_mode => false,
|
162
|
-
:depth => 0,
|
163
|
-
:indent => "",
|
164
|
-
:max_nesting => 100,
|
165
|
-
:object_nl => "",
|
166
|
-
:space => "",
|
167
|
-
:space_before => "",
|
168
|
-
}.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
|
169
|
-
end
|
170
|
-
|
171
|
-
def test_fast_state
|
172
|
-
state = FAST_STATE_PROTOTYPE.dup
|
173
|
-
assert_equal({
|
174
|
-
:allow_nan => false,
|
175
|
-
:array_nl => "",
|
176
|
-
:ascii_only => false,
|
177
|
-
:buffer_initial_length => 1024,
|
178
|
-
:quirks_mode => false,
|
179
|
-
:depth => 0,
|
180
|
-
:indent => "",
|
181
|
-
:max_nesting => 0,
|
182
|
-
:object_nl => "",
|
183
|
-
:space => "",
|
184
|
-
:space_before => "",
|
185
|
-
}.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
|
186
|
-
end
|
187
|
-
|
188
|
-
def test_allow_nan
|
189
|
-
assert_raises(GeneratorError) { generate([JSON::NaN]) }
|
190
|
-
assert_equal '[NaN]', generate([JSON::NaN], :allow_nan => true)
|
191
|
-
assert_raises(GeneratorError) { fast_generate([JSON::NaN]) }
|
192
|
-
assert_raises(GeneratorError) { pretty_generate([JSON::NaN]) }
|
193
|
-
assert_equal "[\n NaN\n]", pretty_generate([JSON::NaN], :allow_nan => true)
|
194
|
-
assert_raises(GeneratorError) { generate([JSON::Infinity]) }
|
195
|
-
assert_equal '[Infinity]', generate([JSON::Infinity], :allow_nan => true)
|
196
|
-
assert_raises(GeneratorError) { fast_generate([JSON::Infinity]) }
|
197
|
-
assert_raises(GeneratorError) { pretty_generate([JSON::Infinity]) }
|
198
|
-
assert_equal "[\n Infinity\n]", pretty_generate([JSON::Infinity], :allow_nan => true)
|
199
|
-
assert_raises(GeneratorError) { generate([JSON::MinusInfinity]) }
|
200
|
-
assert_equal '[-Infinity]', generate([JSON::MinusInfinity], :allow_nan => true)
|
201
|
-
assert_raises(GeneratorError) { fast_generate([JSON::MinusInfinity]) }
|
202
|
-
assert_raises(GeneratorError) { pretty_generate([JSON::MinusInfinity]) }
|
203
|
-
assert_equal "[\n -Infinity\n]", pretty_generate([JSON::MinusInfinity], :allow_nan => true)
|
204
|
-
end
|
205
|
-
|
206
|
-
def test_depth
|
207
|
-
ary = []; ary << ary
|
208
|
-
assert_equal 0, JSON::SAFE_STATE_PROTOTYPE.depth
|
209
|
-
assert_raises(JSON::NestingError) { JSON.generate(ary) }
|
210
|
-
assert_equal 0, JSON::SAFE_STATE_PROTOTYPE.depth
|
211
|
-
assert_equal 0, JSON::PRETTY_STATE_PROTOTYPE.depth
|
212
|
-
assert_raises(JSON::NestingError) { JSON.pretty_generate(ary) }
|
213
|
-
assert_equal 0, JSON::PRETTY_STATE_PROTOTYPE.depth
|
214
|
-
s = JSON.state.new
|
215
|
-
assert_equal 0, s.depth
|
216
|
-
assert_raises(JSON::NestingError) { ary.to_json(s) }
|
217
|
-
assert_equal 100, s.depth
|
218
|
-
end
|
219
|
-
|
220
|
-
def test_buffer_initial_length
|
221
|
-
s = JSON.state.new
|
222
|
-
assert_equal 1024, s.buffer_initial_length
|
223
|
-
s.buffer_initial_length = 0
|
224
|
-
assert_equal 1024, s.buffer_initial_length
|
225
|
-
s.buffer_initial_length = -1
|
226
|
-
assert_equal 1024, s.buffer_initial_length
|
227
|
-
s.buffer_initial_length = 128
|
228
|
-
assert_equal 128, s.buffer_initial_length
|
229
|
-
end
|
230
|
-
|
231
|
-
def test_gc
|
232
|
-
if respond_to?(:assert_in_out_err)
|
233
|
-
assert_in_out_err(%w[-rjson --disable-gems], <<-EOS, [], [])
|
234
|
-
bignum_too_long_to_embed_as_string = 1234567890123456789012345
|
235
|
-
expect = bignum_too_long_to_embed_as_string.to_s
|
236
|
-
GC.stress = true
|
237
|
-
|
238
|
-
10.times do |i|
|
239
|
-
tmp = bignum_too_long_to_embed_as_string.to_json
|
240
|
-
raise "'\#{expect}' is expected, but '\#{tmp}'" unless tmp == expect
|
241
|
-
end
|
242
|
-
EOS
|
243
|
-
end
|
244
|
-
end if GC.respond_to?(:stress=)
|
245
|
-
|
246
|
-
def test_configure_using_configure_and_merge
|
247
|
-
numbered_state = {
|
248
|
-
:indent => "1",
|
249
|
-
:space => '2',
|
250
|
-
:space_before => '3',
|
251
|
-
:object_nl => '4',
|
252
|
-
:array_nl => '5'
|
253
|
-
}
|
254
|
-
state1 = JSON.state.new
|
255
|
-
state1.merge(numbered_state)
|
256
|
-
assert_equal '1', state1.indent
|
257
|
-
assert_equal '2', state1.space
|
258
|
-
assert_equal '3', state1.space_before
|
259
|
-
assert_equal '4', state1.object_nl
|
260
|
-
assert_equal '5', state1.array_nl
|
261
|
-
state2 = JSON.state.new
|
262
|
-
state2.configure(numbered_state)
|
263
|
-
assert_equal '1', state2.indent
|
264
|
-
assert_equal '2', state2.space
|
265
|
-
assert_equal '3', state2.space_before
|
266
|
-
assert_equal '4', state2.object_nl
|
267
|
-
assert_equal '5', state2.array_nl
|
268
|
-
end
|
269
|
-
|
270
|
-
def test_configure_hash_conversion
|
271
|
-
state = JSON.state.new
|
272
|
-
state.configure(:indent => '1')
|
273
|
-
assert_equal '1', state.indent
|
274
|
-
state = JSON.state.new
|
275
|
-
foo = 'foo'
|
276
|
-
assert_raise(TypeError) do
|
277
|
-
state.configure(foo)
|
278
|
-
end
|
279
|
-
def foo.to_h
|
280
|
-
{ :indent => '2' }
|
281
|
-
end
|
282
|
-
state.configure(foo)
|
283
|
-
assert_equal '2', state.indent
|
284
|
-
end
|
285
|
-
|
286
|
-
if defined?(JSON::Ext::Generator)
|
287
|
-
def test_broken_bignum # [ruby-core:38867]
|
288
|
-
pid = fork do
|
289
|
-
Bignum.class_eval do
|
290
|
-
def to_s
|
291
|
-
end
|
292
|
-
end
|
293
|
-
begin
|
294
|
-
JSON::Ext::Generator::State.new.generate(1<<64)
|
295
|
-
exit 1
|
296
|
-
rescue TypeError
|
297
|
-
exit 0
|
298
|
-
end
|
299
|
-
end
|
300
|
-
_, status = Process.waitpid2(pid)
|
301
|
-
assert status.success?
|
302
|
-
rescue NotImplementedError
|
303
|
-
# forking to avoid modifying core class of a parent process and
|
304
|
-
# introducing race conditions of tests are run in parallel
|
305
|
-
end
|
306
|
-
end
|
307
|
-
|
308
|
-
def test_hash_likeness_set_symbol
|
309
|
-
state = JSON.state.new
|
310
|
-
assert_equal nil, state[:foo]
|
311
|
-
assert_equal nil.class, state[:foo].class
|
312
|
-
assert_equal nil, state['foo']
|
313
|
-
state[:foo] = :bar
|
314
|
-
assert_equal :bar, state[:foo]
|
315
|
-
assert_equal :bar, state['foo']
|
316
|
-
state_hash = state.to_hash
|
317
|
-
assert_kind_of Hash, state_hash
|
318
|
-
assert_equal :bar, state_hash[:foo]
|
319
|
-
end
|
320
|
-
|
321
|
-
def test_hash_likeness_set_string
|
322
|
-
state = JSON.state.new
|
323
|
-
assert_equal nil, state[:foo]
|
324
|
-
assert_equal nil, state['foo']
|
325
|
-
state['foo'] = :bar
|
326
|
-
assert_equal :bar, state[:foo]
|
327
|
-
assert_equal :bar, state['foo']
|
328
|
-
state_hash = state.to_hash
|
329
|
-
assert_kind_of Hash, state_hash
|
330
|
-
assert_equal :bar, state_hash[:foo]
|
331
|
-
end
|
332
|
-
|
333
|
-
def test_json_generate
|
334
|
-
assert_raise JSON::GeneratorError do
|
335
|
-
assert_equal true, JSON.generate(["\xea"])
|
336
|
-
end
|
337
|
-
end
|
338
|
-
|
339
|
-
def test_string_subclass
|
340
|
-
s = Class.new(String) do
|
341
|
-
def to_s; self; end
|
342
|
-
undef to_json
|
343
|
-
end
|
344
|
-
assert_nothing_raised(SystemStackError) do
|
345
|
-
assert_equal '["foo"]', JSON.generate([s.new('foo')])
|
346
|
-
end
|
347
|
-
end
|
348
|
-
end
|