json_pure 1.8.6 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -1
- data/.travis.yml +5 -3
- data/CHANGES +5 -5
- data/Gemfile +3 -1
- data/README.md +131 -84
- data/Rakefile +17 -10
- data/VERSION +1 -1
- data/ext/json/ext/generator/generator.c +1 -52
- data/ext/json/ext/generator/generator.h +0 -5
- data/ext/json/ext/parser/extconf.rb +3 -0
- data/ext/json/ext/parser/parser.c +304 -458
- data/ext/json/ext/parser/parser.h +0 -1
- data/ext/json/ext/parser/parser.rl +35 -152
- data/ext/json/extconf.rb +0 -1
- data/java/src/json/ext/Generator.java +2 -5
- data/java/src/json/ext/GeneratorState.java +2 -54
- data/java/src/json/ext/OptionsReader.java +1 -1
- data/java/src/json/ext/Parser.java +109 -409
- data/java/src/json/ext/Parser.rl +24 -117
- data/java/src/json/ext/RuntimeInfo.java +0 -4
- data/json.gemspec +0 -0
- data/json_pure.gemspec +7 -7
- data/lib/json.rb +1 -0
- data/lib/json/add/bigdecimal.rb +1 -0
- data/lib/json/add/complex.rb +2 -1
- data/lib/json/add/core.rb +1 -0
- data/lib/json/add/date.rb +1 -1
- data/lib/json/add/date_time.rb +1 -1
- data/lib/json/add/exception.rb +1 -1
- data/lib/json/add/ostruct.rb +1 -1
- data/lib/json/add/range.rb +1 -1
- data/lib/json/add/rational.rb +1 -0
- data/lib/json/add/regexp.rb +1 -1
- data/lib/json/add/struct.rb +1 -1
- data/lib/json/add/symbol.rb +1 -1
- data/lib/json/add/time.rb +1 -1
- data/lib/json/common.rb +24 -52
- data/lib/json/ext.rb +0 -6
- data/lib/json/generic_object.rb +5 -4
- data/lib/json/pure.rb +2 -8
- data/lib/json/pure/generator.rb +51 -123
- data/lib/json/pure/parser.rb +28 -80
- data/lib/json/version.rb +2 -1
- data/references/rfc7159.txt +899 -0
- data/tests/fixtures/obsolete_fail1.json +1 -0
- data/tests/{test_json_addition.rb → json_addition_test.rb} +22 -25
- data/tests/json_common_interface_test.rb +126 -0
- data/tests/json_encoding_test.rb +105 -0
- data/tests/json_ext_parser_test.rb +15 -0
- data/tests/{test_json_fixtures.rb → json_fixtures_test.rb} +5 -8
- data/tests/{test_json_generate.rb → json_generator_test.rb} +65 -37
- data/tests/{test_json_generic_object.rb → json_generic_object_test.rb} +15 -8
- data/tests/json_parser_test.rb +448 -0
- data/tests/json_string_matching_test.rb +38 -0
- data/tests/test_helper.rb +23 -0
- data/tools/fuzz.rb +1 -9
- metadata +18 -31
- data/TODO +0 -1
- data/tests/fixtures/fail1.json +0 -1
- data/tests/setup_variant.rb +0 -11
- data/tests/test_json.rb +0 -519
- data/tests/test_json_encoding.rb +0 -65
- data/tests/test_json_string_matching.rb +0 -39
- data/tests/test_json_unicode.rb +0 -72
@@ -0,0 +1,38 @@
|
|
1
|
+
#frozen_string_literal: false
|
2
|
+
require 'test_helper'
|
3
|
+
require 'time'
|
4
|
+
|
5
|
+
class JSONStringMatchingTest < Test::Unit::TestCase
|
6
|
+
include JSON
|
7
|
+
|
8
|
+
class TestTime < ::Time
|
9
|
+
def self.json_create(string)
|
10
|
+
Time.parse(string)
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_json(*)
|
14
|
+
%{"#{strftime('%FT%T%z')}"}
|
15
|
+
end
|
16
|
+
|
17
|
+
def ==(other)
|
18
|
+
to_i == other.to_i
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_match_date
|
23
|
+
t = TestTime.new
|
24
|
+
t_json = [ t ].to_json
|
25
|
+
time_regexp = /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}\z/
|
26
|
+
assert_equal [ t ],
|
27
|
+
parse(
|
28
|
+
t_json,
|
29
|
+
:create_additions => true,
|
30
|
+
:match_string => { time_regexp => TestTime }
|
31
|
+
)
|
32
|
+
assert_equal [ t.strftime('%FT%T%z') ],
|
33
|
+
parse(
|
34
|
+
t_json,
|
35
|
+
:match_string => { time_regexp => TestTime }
|
36
|
+
)
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
gem 'json', File.read('VERSION').chomp
|
2
|
+
|
3
|
+
case ENV['JSON']
|
4
|
+
when 'pure'
|
5
|
+
$:.unshift 'lib'
|
6
|
+
require 'json/pure'
|
7
|
+
when 'ext'
|
8
|
+
$:.unshift 'ext', 'lib'
|
9
|
+
require 'json/ext'
|
10
|
+
else
|
11
|
+
$:.unshift 'ext', 'lib'
|
12
|
+
require 'json'
|
13
|
+
end
|
14
|
+
|
15
|
+
require 'test/unit'
|
16
|
+
begin
|
17
|
+
require 'byebug'
|
18
|
+
rescue LoadError
|
19
|
+
end
|
20
|
+
if ENV['START_SIMPLECOV'].to_i == 1
|
21
|
+
require 'simplecov'
|
22
|
+
SimpleCov.start
|
23
|
+
end
|
data/tools/fuzz.rb
CHANGED
@@ -1,13 +1,5 @@
|
|
1
1
|
require 'json'
|
2
2
|
|
3
|
-
require 'iconv'
|
4
|
-
ISO_8859_1_TO_UTF8 = Iconv.new('utf-8', 'iso-8859-15')
|
5
|
-
class ::String
|
6
|
-
def to_utf8
|
7
|
-
ISO_8859_1_TO_UTF8.iconv self
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
3
|
class Fuzzer
|
12
4
|
def initialize(n, freqs = {})
|
13
5
|
sum = freqs.inject(0.0) { |s, x| s + x.last }
|
@@ -25,7 +17,7 @@ class Fuzzer
|
|
25
17
|
def random_string
|
26
18
|
s = ''
|
27
19
|
30.times { s << @alpha[rand(@alpha.size)] }
|
28
|
-
s
|
20
|
+
s
|
29
21
|
end
|
30
22
|
|
31
23
|
def pick
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_pure
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Florian Frank
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-07-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -45,14 +45,7 @@ extensions: []
|
|
45
45
|
extra_rdoc_files:
|
46
46
|
- README.md
|
47
47
|
files:
|
48
|
-
- "./tests/
|
49
|
-
- "./tests/test_json_addition.rb"
|
50
|
-
- "./tests/test_json_encoding.rb"
|
51
|
-
- "./tests/test_json_fixtures.rb"
|
52
|
-
- "./tests/test_json_generate.rb"
|
53
|
-
- "./tests/test_json_generic_object.rb"
|
54
|
-
- "./tests/test_json_string_matching.rb"
|
55
|
-
- "./tests/test_json_unicode.rb"
|
48
|
+
- "./tests/test_helper.rb"
|
56
49
|
- ".gitignore"
|
57
50
|
- ".travis.yml"
|
58
51
|
- CHANGES
|
@@ -60,7 +53,6 @@ files:
|
|
60
53
|
- README-json-jruby.markdown
|
61
54
|
- README.md
|
62
55
|
- Rakefile
|
63
|
-
- TODO
|
64
56
|
- VERSION
|
65
57
|
- data/example.json
|
66
58
|
- data/index.html
|
@@ -116,7 +108,7 @@ files:
|
|
116
108
|
- lib/json/pure/generator.rb
|
117
109
|
- lib/json/pure/parser.rb
|
118
110
|
- lib/json/version.rb
|
119
|
-
-
|
111
|
+
- references/rfc7159.txt
|
120
112
|
- tests/fixtures/fail10.json
|
121
113
|
- tests/fixtures/fail11.json
|
122
114
|
- tests/fixtures/fail12.json
|
@@ -140,6 +132,7 @@ files:
|
|
140
132
|
- tests/fixtures/fail7.json
|
141
133
|
- tests/fixtures/fail8.json
|
142
134
|
- tests/fixtures/fail9.json
|
135
|
+
- tests/fixtures/obsolete_fail1.json
|
143
136
|
- tests/fixtures/pass1.json
|
144
137
|
- tests/fixtures/pass15.json
|
145
138
|
- tests/fixtures/pass16.json
|
@@ -147,15 +140,16 @@ files:
|
|
147
140
|
- tests/fixtures/pass2.json
|
148
141
|
- tests/fixtures/pass26.json
|
149
142
|
- tests/fixtures/pass3.json
|
150
|
-
- tests/
|
151
|
-
- tests/
|
152
|
-
- tests/
|
153
|
-
- tests/
|
154
|
-
- tests/
|
155
|
-
- tests/
|
156
|
-
- tests/
|
157
|
-
- tests/
|
158
|
-
- tests/
|
143
|
+
- tests/json_addition_test.rb
|
144
|
+
- tests/json_common_interface_test.rb
|
145
|
+
- tests/json_encoding_test.rb
|
146
|
+
- tests/json_ext_parser_test.rb
|
147
|
+
- tests/json_fixtures_test.rb
|
148
|
+
- tests/json_generator_test.rb
|
149
|
+
- tests/json_generic_object_test.rb
|
150
|
+
- tests/json_parser_test.rb
|
151
|
+
- tests/json_string_matching_test.rb
|
152
|
+
- tests/test_helper.rb
|
159
153
|
- tools/diff.sh
|
160
154
|
- tools/fuzz.rb
|
161
155
|
- tools/server.rb
|
@@ -166,7 +160,7 @@ metadata: {}
|
|
166
160
|
post_install_message:
|
167
161
|
rdoc_options:
|
168
162
|
- "--title"
|
169
|
-
- JSON implemention for
|
163
|
+
- JSON implemention for ruby
|
170
164
|
- "--main"
|
171
165
|
- README.md
|
172
166
|
require_paths:
|
@@ -183,16 +177,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
183
177
|
version: '0'
|
184
178
|
requirements: []
|
185
179
|
rubyforge_project:
|
186
|
-
rubygems_version: 2.6.
|
180
|
+
rubygems_version: 2.6.4
|
187
181
|
signing_key:
|
188
182
|
specification_version: 4
|
189
183
|
summary: JSON Implementation for Ruby
|
190
184
|
test_files:
|
191
|
-
- "./tests/
|
192
|
-
- "./tests/test_json_addition.rb"
|
193
|
-
- "./tests/test_json_encoding.rb"
|
194
|
-
- "./tests/test_json_fixtures.rb"
|
195
|
-
- "./tests/test_json_generate.rb"
|
196
|
-
- "./tests/test_json_generic_object.rb"
|
197
|
-
- "./tests/test_json_string_matching.rb"
|
198
|
-
- "./tests/test_json_unicode.rb"
|
185
|
+
- "./tests/test_helper.rb"
|
data/tests/fixtures/fail1.json
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
"A JSON payload should be an object or array, not a string."
|
data/tests/setup_variant.rb
DELETED
data/tests/test_json.rb
DELETED
@@ -1,519 +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
|
-
require 'stringio'
|
7
|
-
require 'tempfile'
|
8
|
-
require 'ostruct'
|
9
|
-
|
10
|
-
class TestJSON < Test::Unit::TestCase
|
11
|
-
include JSON
|
12
|
-
|
13
|
-
def setup
|
14
|
-
@ary = [1, "foo", 3.14, 4711.0, 2.718, nil, [1,-2,3], false, true].map do
|
15
|
-
|x| [x]
|
16
|
-
end
|
17
|
-
@ary_to_parse = ["1", '"foo"', "3.14", "4711.0", "2.718", "null",
|
18
|
-
"[1,-2,3]", "false", "true"].map do
|
19
|
-
|x| "[#{x}]"
|
20
|
-
end
|
21
|
-
@hash = {
|
22
|
-
'a' => 2,
|
23
|
-
'b' => 3.141,
|
24
|
-
'c' => 'c',
|
25
|
-
'd' => [ 1, "b", 3.14 ],
|
26
|
-
'e' => { 'foo' => 'bar' },
|
27
|
-
'g' => "\"\0\037",
|
28
|
-
'h' => 1000.0,
|
29
|
-
'i' => 0.001
|
30
|
-
}
|
31
|
-
@json = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},'\
|
32
|
-
'"g":"\\"\\u0000\\u001f","h":1.0E3,"i":1.0E-3}'
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_construction
|
36
|
-
parser = JSON::Parser.new('test')
|
37
|
-
assert_equal 'test', parser.source
|
38
|
-
end
|
39
|
-
|
40
|
-
def assert_equal_float(expected, is)
|
41
|
-
assert_in_delta(expected.first, is.first, 1e-2)
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_parse_simple_arrays
|
45
|
-
assert_equal([], parse('[]'))
|
46
|
-
assert_equal([], parse(' [ ] '))
|
47
|
-
assert_equal([nil], parse('[null]'))
|
48
|
-
assert_equal([false], parse('[false]'))
|
49
|
-
assert_equal([true], parse('[true]'))
|
50
|
-
assert_equal([-23], parse('[-23]'))
|
51
|
-
assert_equal([23], parse('[23]'))
|
52
|
-
assert_equal([0.23], parse('[0.23]'))
|
53
|
-
assert_equal([0.0], parse('[0e0]'))
|
54
|
-
assert_raises(JSON::ParserError) { parse('[+23.2]') }
|
55
|
-
assert_raises(JSON::ParserError) { parse('[+23]') }
|
56
|
-
assert_raises(JSON::ParserError) { parse('[.23]') }
|
57
|
-
assert_raises(JSON::ParserError) { parse('[023]') }
|
58
|
-
assert_equal_float [3.141], parse('[3.141]')
|
59
|
-
assert_equal_float [-3.141], parse('[-3.141]')
|
60
|
-
assert_equal_float [3.141], parse('[3141e-3]')
|
61
|
-
assert_equal_float [3.141], parse('[3141.1e-3]')
|
62
|
-
assert_equal_float [3.141], parse('[3141E-3]')
|
63
|
-
assert_equal_float [3.141], parse('[3141.0E-3]')
|
64
|
-
assert_equal_float [-3.141], parse('[-3141.0e-3]')
|
65
|
-
assert_equal_float [-3.141], parse('[-3141e-3]')
|
66
|
-
assert_raises(ParserError) { parse('[NaN]') }
|
67
|
-
assert parse('[NaN]', :allow_nan => true).first.nan?
|
68
|
-
assert_raises(ParserError) { parse('[Infinity]') }
|
69
|
-
assert_equal [1.0/0], parse('[Infinity]', :allow_nan => true)
|
70
|
-
assert_raises(ParserError) { parse('[-Infinity]') }
|
71
|
-
assert_equal [-1.0/0], parse('[-Infinity]', :allow_nan => true)
|
72
|
-
assert_equal([""], parse('[""]'))
|
73
|
-
assert_equal(["foobar"], parse('["foobar"]'))
|
74
|
-
assert_equal([{}], parse('[{}]'))
|
75
|
-
end
|
76
|
-
|
77
|
-
def test_parse_simple_objects
|
78
|
-
assert_equal({}, parse('{}'))
|
79
|
-
assert_equal({}, parse(' { } '))
|
80
|
-
assert_equal({ "a" => nil }, parse('{ "a" : null}'))
|
81
|
-
assert_equal({ "a" => nil }, parse('{"a":null}'))
|
82
|
-
assert_equal({ "a" => false }, parse('{ "a" : false } '))
|
83
|
-
assert_equal({ "a" => false }, parse('{"a":false}'))
|
84
|
-
assert_raises(JSON::ParserError) { parse('{false}') }
|
85
|
-
assert_equal({ "a" => true }, parse('{"a":true}'))
|
86
|
-
assert_equal({ "a" => true }, parse(' { "a" : true } '))
|
87
|
-
assert_equal({ "a" => -23 }, parse(' { "a" : -23 } '))
|
88
|
-
assert_equal({ "a" => -23 }, parse(' { "a" : -23 } '))
|
89
|
-
assert_equal({ "a" => 23 }, parse('{"a":23 } '))
|
90
|
-
assert_equal({ "a" => 23 }, parse(' { "a" : 23 } '))
|
91
|
-
assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
|
92
|
-
assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
|
93
|
-
end
|
94
|
-
|
95
|
-
def test_parse_json_primitive_values
|
96
|
-
assert_raise(JSON::ParserError) { JSON.parse('') }
|
97
|
-
assert_raise(JSON::ParserError) { JSON.parse('', :quirks_mode => true) }
|
98
|
-
assert_raise(TypeError) { JSON::Parser.new(nil).parse }
|
99
|
-
assert_raise(TypeError) { JSON::Parser.new(nil, :quirks_mode => true).parse }
|
100
|
-
assert_raise(TypeError) { JSON.parse(nil) }
|
101
|
-
assert_raise(TypeError) { JSON.parse(nil, :quirks_mode => true) }
|
102
|
-
assert_raise(JSON::ParserError) { JSON.parse(' /* foo */ ') }
|
103
|
-
assert_raise(JSON::ParserError) { JSON.parse(' /* foo */ ', :quirks_mode => true) }
|
104
|
-
parser = JSON::Parser.new('null')
|
105
|
-
assert_equal false, parser.quirks_mode?
|
106
|
-
assert_raise(JSON::ParserError) { parser.parse }
|
107
|
-
assert_raise(JSON::ParserError) { JSON.parse('null') }
|
108
|
-
assert_equal nil, JSON.parse('null', :quirks_mode => true)
|
109
|
-
parser = JSON::Parser.new('null', :quirks_mode => true)
|
110
|
-
assert_equal true, parser.quirks_mode?
|
111
|
-
assert_equal nil, parser.parse
|
112
|
-
assert_raise(JSON::ParserError) { JSON.parse('false') }
|
113
|
-
assert_equal false, JSON.parse('false', :quirks_mode => true)
|
114
|
-
assert_raise(JSON::ParserError) { JSON.parse('true') }
|
115
|
-
assert_equal true, JSON.parse('true', :quirks_mode => true)
|
116
|
-
assert_raise(JSON::ParserError) { JSON.parse('23') }
|
117
|
-
assert_equal 23, JSON.parse('23', :quirks_mode => true)
|
118
|
-
assert_raise(JSON::ParserError) { JSON.parse('1') }
|
119
|
-
assert_equal 1, JSON.parse('1', :quirks_mode => true)
|
120
|
-
assert_raise(JSON::ParserError) { JSON.parse('3.141') }
|
121
|
-
assert_in_delta 3.141, JSON.parse('3.141', :quirks_mode => true), 1E-3
|
122
|
-
assert_raise(JSON::ParserError) { JSON.parse('18446744073709551616') }
|
123
|
-
assert_equal 2 ** 64, JSON.parse('18446744073709551616', :quirks_mode => true)
|
124
|
-
assert_raise(JSON::ParserError) { JSON.parse('"foo"') }
|
125
|
-
assert_equal 'foo', JSON.parse('"foo"', :quirks_mode => true)
|
126
|
-
assert_raise(JSON::ParserError) { JSON.parse('NaN', :allow_nan => true) }
|
127
|
-
assert JSON.parse('NaN', :quirks_mode => true, :allow_nan => true).nan?
|
128
|
-
assert_raise(JSON::ParserError) { JSON.parse('Infinity', :allow_nan => true) }
|
129
|
-
assert JSON.parse('Infinity', :quirks_mode => true, :allow_nan => true).infinite?
|
130
|
-
assert_raise(JSON::ParserError) { JSON.parse('-Infinity', :allow_nan => true) }
|
131
|
-
assert JSON.parse('-Infinity', :quirks_mode => true, :allow_nan => true).infinite?
|
132
|
-
assert_raise(JSON::ParserError) { JSON.parse('[ 1, ]', :quirks_mode => true) }
|
133
|
-
end
|
134
|
-
|
135
|
-
def test_parse_arrays
|
136
|
-
assert_equal([1,2,3], parse('[1,2,3]'))
|
137
|
-
assert_equal([1.2,2,3], parse('[1.2,2,3]'))
|
138
|
-
assert_equal([[],[[],[]]], parse('[[],[[],[]]]'))
|
139
|
-
end
|
140
|
-
|
141
|
-
def test_parse_values
|
142
|
-
assert_equal([""], parse('[""]'))
|
143
|
-
assert_equal(["\\"], parse('["\\\\"]'))
|
144
|
-
assert_equal(['"'], parse('["\""]'))
|
145
|
-
assert_equal(['\\"\\'], parse('["\\\\\\"\\\\"]'))
|
146
|
-
assert_equal(["\"\b\n\r\t\0\037"],
|
147
|
-
parse('["\"\b\n\r\t\u0000\u001f"]'))
|
148
|
-
for i in 0 ... @ary.size
|
149
|
-
assert_equal(@ary[i], parse(@ary_to_parse[i]))
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
def test_parse_array
|
154
|
-
assert_equal([], parse('[]'))
|
155
|
-
assert_equal([], parse(' [ ] '))
|
156
|
-
assert_equal([1], parse('[1]'))
|
157
|
-
assert_equal([1], parse(' [ 1 ] '))
|
158
|
-
assert_equal(@ary,
|
159
|
-
parse('[[1],["foo"],[3.14],[47.11e+2],[2718.0E-3],[null],[[1,-2,3]]'\
|
160
|
-
',[false],[true]]'))
|
161
|
-
assert_equal(@ary, parse(%Q{ [ [1] , ["foo"] , [3.14] \t , [47.11e+2]\s
|
162
|
-
, [2718.0E-3 ],\r[ null] , [[1, -2, 3 ]], [false ],[ true]\n ] }))
|
163
|
-
end
|
164
|
-
|
165
|
-
class SubArray < Array
|
166
|
-
def <<(v)
|
167
|
-
@shifted = true
|
168
|
-
super
|
169
|
-
end
|
170
|
-
|
171
|
-
def shifted?
|
172
|
-
@shifted
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
class SubArray2 < Array
|
177
|
-
def to_json(*a)
|
178
|
-
{
|
179
|
-
JSON.create_id => self.class.name,
|
180
|
-
'ary' => to_a,
|
181
|
-
}.to_json(*a)
|
182
|
-
end
|
183
|
-
|
184
|
-
def self.json_create(o)
|
185
|
-
o.delete JSON.create_id
|
186
|
-
o['ary']
|
187
|
-
end
|
188
|
-
end
|
189
|
-
|
190
|
-
class SubArrayWrapper
|
191
|
-
def initialize
|
192
|
-
@data = []
|
193
|
-
end
|
194
|
-
|
195
|
-
attr_reader :data
|
196
|
-
|
197
|
-
def [](index)
|
198
|
-
@data[index]
|
199
|
-
end
|
200
|
-
|
201
|
-
def <<(value)
|
202
|
-
@data << value
|
203
|
-
@shifted = true
|
204
|
-
end
|
205
|
-
|
206
|
-
def shifted?
|
207
|
-
@shifted
|
208
|
-
end
|
209
|
-
end
|
210
|
-
|
211
|
-
def test_parse_array_custom_array_derived_class
|
212
|
-
res = parse('[1,2]', :array_class => SubArray)
|
213
|
-
assert_equal([1,2], res)
|
214
|
-
assert_equal(SubArray, res.class)
|
215
|
-
assert res.shifted?
|
216
|
-
end
|
217
|
-
|
218
|
-
def test_parse_array_custom_non_array_derived_class
|
219
|
-
res = parse('[1,2]', :array_class => SubArrayWrapper)
|
220
|
-
assert_equal([1,2], res.data)
|
221
|
-
assert_equal(SubArrayWrapper, res.class)
|
222
|
-
assert res.shifted?
|
223
|
-
end
|
224
|
-
|
225
|
-
def test_parse_object
|
226
|
-
assert_equal({}, parse('{}'))
|
227
|
-
assert_equal({}, parse(' { } '))
|
228
|
-
assert_equal({'foo'=>'bar'}, parse('{"foo":"bar"}'))
|
229
|
-
assert_equal({'foo'=>'bar'}, parse(' { "foo" : "bar" } '))
|
230
|
-
end
|
231
|
-
|
232
|
-
class SubHash < Hash
|
233
|
-
def []=(k, v)
|
234
|
-
@item_set = true
|
235
|
-
super
|
236
|
-
end
|
237
|
-
|
238
|
-
def item_set?
|
239
|
-
@item_set
|
240
|
-
end
|
241
|
-
end
|
242
|
-
|
243
|
-
class SubHash2 < Hash
|
244
|
-
def to_json(*a)
|
245
|
-
{
|
246
|
-
JSON.create_id => self.class.name,
|
247
|
-
}.merge(self).to_json(*a)
|
248
|
-
end
|
249
|
-
|
250
|
-
def self.json_create(o)
|
251
|
-
o.delete JSON.create_id
|
252
|
-
self[o]
|
253
|
-
end
|
254
|
-
end
|
255
|
-
|
256
|
-
class SubOpenStruct < OpenStruct
|
257
|
-
def [](k)
|
258
|
-
__send__(k)
|
259
|
-
end
|
260
|
-
|
261
|
-
def []=(k, v)
|
262
|
-
@item_set = true
|
263
|
-
__send__("#{k}=", v)
|
264
|
-
end
|
265
|
-
|
266
|
-
def item_set?
|
267
|
-
@item_set
|
268
|
-
end
|
269
|
-
end
|
270
|
-
|
271
|
-
def test_parse_object_custom_hash_derived_class
|
272
|
-
res = parse('{"foo":"bar"}', :object_class => SubHash)
|
273
|
-
assert_equal({"foo" => "bar"}, res)
|
274
|
-
assert_equal(SubHash, res.class)
|
275
|
-
assert res.item_set?
|
276
|
-
end
|
277
|
-
|
278
|
-
def test_parse_object_custom_non_hash_derived_class
|
279
|
-
res = parse('{"foo":"bar"}', :object_class => SubOpenStruct)
|
280
|
-
assert_equal "bar", res.foo
|
281
|
-
assert_equal(SubOpenStruct, res.class)
|
282
|
-
assert res.item_set?
|
283
|
-
end
|
284
|
-
|
285
|
-
def test_parse_generic_object
|
286
|
-
res = parse('{"foo":"bar", "baz":{}}', :object_class => JSON::GenericObject)
|
287
|
-
assert_equal(JSON::GenericObject, res.class)
|
288
|
-
assert_equal "bar", res.foo
|
289
|
-
assert_equal "bar", res["foo"]
|
290
|
-
assert_equal "bar", res[:foo]
|
291
|
-
assert_equal "bar", res.to_hash[:foo]
|
292
|
-
assert_equal(JSON::GenericObject, res.baz.class)
|
293
|
-
end
|
294
|
-
|
295
|
-
def test_generate_core_subclasses_with_new_to_json
|
296
|
-
obj = SubHash2["foo" => SubHash2["bar" => true]]
|
297
|
-
obj_json = JSON(obj)
|
298
|
-
obj_again = JSON.parse(obj_json, :create_additions => true)
|
299
|
-
assert_kind_of SubHash2, obj_again
|
300
|
-
assert_kind_of SubHash2, obj_again['foo']
|
301
|
-
assert obj_again['foo']['bar']
|
302
|
-
assert_equal obj, obj_again
|
303
|
-
assert_equal ["foo"], JSON(JSON(SubArray2["foo"]), :create_additions => true)
|
304
|
-
end
|
305
|
-
|
306
|
-
def test_generate_core_subclasses_with_default_to_json
|
307
|
-
assert_equal '{"foo":"bar"}', JSON(SubHash["foo" => "bar"])
|
308
|
-
assert_equal '["foo"]', JSON(SubArray["foo"])
|
309
|
-
end
|
310
|
-
|
311
|
-
def test_generate_of_core_subclasses
|
312
|
-
obj = SubHash["foo" => SubHash["bar" => true]]
|
313
|
-
obj_json = JSON(obj)
|
314
|
-
obj_again = JSON(obj_json)
|
315
|
-
assert_kind_of Hash, obj_again
|
316
|
-
assert_kind_of Hash, obj_again['foo']
|
317
|
-
assert obj_again['foo']['bar']
|
318
|
-
assert_equal obj, obj_again
|
319
|
-
end
|
320
|
-
|
321
|
-
def test_parser_reset
|
322
|
-
parser = Parser.new(@json)
|
323
|
-
assert_equal(@hash, parser.parse)
|
324
|
-
assert_equal(@hash, parser.parse)
|
325
|
-
end
|
326
|
-
|
327
|
-
def test_comments
|
328
|
-
json = <<EOT
|
329
|
-
{
|
330
|
-
"key1":"value1", // eol comment
|
331
|
-
"key2":"value2" /* multi line
|
332
|
-
* comment */,
|
333
|
-
"key3":"value3" /* multi line
|
334
|
-
// nested eol comment
|
335
|
-
* comment */
|
336
|
-
}
|
337
|
-
EOT
|
338
|
-
assert_equal(
|
339
|
-
{ "key1" => "value1", "key2" => "value2", "key3" => "value3" },
|
340
|
-
parse(json))
|
341
|
-
json = <<EOT
|
342
|
-
{
|
343
|
-
"key1":"value1" /* multi line
|
344
|
-
// nested eol comment
|
345
|
-
/* illegal nested multi line comment */
|
346
|
-
* comment */
|
347
|
-
}
|
348
|
-
EOT
|
349
|
-
assert_raises(ParserError) { parse(json) }
|
350
|
-
json = <<EOT
|
351
|
-
{
|
352
|
-
"key1":"value1" /* multi line
|
353
|
-
// nested eol comment
|
354
|
-
closed multi comment */
|
355
|
-
and again, throw an Error */
|
356
|
-
}
|
357
|
-
EOT
|
358
|
-
assert_raises(ParserError) { parse(json) }
|
359
|
-
json = <<EOT
|
360
|
-
{
|
361
|
-
"key1":"value1" /*/*/
|
362
|
-
}
|
363
|
-
EOT
|
364
|
-
assert_equal({ "key1" => "value1" }, parse(json))
|
365
|
-
end
|
366
|
-
|
367
|
-
def test_backslash
|
368
|
-
data = [ '\\.(?i:gif|jpe?g|png)$' ]
|
369
|
-
json = '["\\\\.(?i:gif|jpe?g|png)$"]'
|
370
|
-
assert_equal json, JSON.generate(data)
|
371
|
-
assert_equal data, JSON.parse(json)
|
372
|
-
#
|
373
|
-
data = [ '\\"' ]
|
374
|
-
json = '["\\\\\""]'
|
375
|
-
assert_equal json, JSON.generate(data)
|
376
|
-
assert_equal data, JSON.parse(json)
|
377
|
-
#
|
378
|
-
json = '["/"]'
|
379
|
-
data = JSON.parse(json)
|
380
|
-
assert_equal ['/'], data
|
381
|
-
assert_equal json, JSON.generate(data)
|
382
|
-
#
|
383
|
-
json = '["\""]'
|
384
|
-
data = JSON.parse(json)
|
385
|
-
assert_equal ['"'], data
|
386
|
-
assert_equal json, JSON.generate(data)
|
387
|
-
json = '["\\\'"]'
|
388
|
-
data = JSON.parse(json)
|
389
|
-
assert_equal ["'"], data
|
390
|
-
assert_equal '["\'"]', JSON.generate(data)
|
391
|
-
end
|
392
|
-
|
393
|
-
def test_wrong_inputs
|
394
|
-
assert_raises(ParserError) { JSON.parse('"foo"') }
|
395
|
-
assert_raises(ParserError) { JSON.parse('123') }
|
396
|
-
assert_raises(ParserError) { JSON.parse('[] bla') }
|
397
|
-
assert_raises(ParserError) { JSON.parse('[] 1') }
|
398
|
-
assert_raises(ParserError) { JSON.parse('[] []') }
|
399
|
-
assert_raises(ParserError) { JSON.parse('[] {}') }
|
400
|
-
assert_raises(ParserError) { JSON.parse('{} []') }
|
401
|
-
assert_raises(ParserError) { JSON.parse('{} {}') }
|
402
|
-
assert_raises(ParserError) { JSON.parse('[NULL]') }
|
403
|
-
assert_raises(ParserError) { JSON.parse('[FALSE]') }
|
404
|
-
assert_raises(ParserError) { JSON.parse('[TRUE]') }
|
405
|
-
assert_raises(ParserError) { JSON.parse('[07] ') }
|
406
|
-
assert_raises(ParserError) { JSON.parse('[0a]') }
|
407
|
-
assert_raises(ParserError) { JSON.parse('[1.]') }
|
408
|
-
assert_raises(ParserError) { JSON.parse(' ') }
|
409
|
-
end
|
410
|
-
|
411
|
-
def test_nesting
|
412
|
-
assert_raises(JSON::NestingError) { JSON.parse '[[]]', :max_nesting => 1 }
|
413
|
-
assert_raises(JSON::NestingError) { JSON.parser.new('[[]]', :max_nesting => 1).parse }
|
414
|
-
assert_equal [[]], JSON.parse('[[]]', :max_nesting => 2)
|
415
|
-
too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
|
416
|
-
too_deep_ary = eval too_deep
|
417
|
-
assert_raises(JSON::NestingError) { JSON.parse too_deep }
|
418
|
-
assert_raises(JSON::NestingError) { JSON.parser.new(too_deep).parse }
|
419
|
-
assert_raises(JSON::NestingError) { JSON.parse too_deep, :max_nesting => 100 }
|
420
|
-
ok = JSON.parse too_deep, :max_nesting => 101
|
421
|
-
assert_equal too_deep_ary, ok
|
422
|
-
ok = JSON.parse too_deep, :max_nesting => nil
|
423
|
-
assert_equal too_deep_ary, ok
|
424
|
-
ok = JSON.parse too_deep, :max_nesting => false
|
425
|
-
assert_equal too_deep_ary, ok
|
426
|
-
ok = JSON.parse too_deep, :max_nesting => 0
|
427
|
-
assert_equal too_deep_ary, ok
|
428
|
-
assert_raises(JSON::NestingError) { JSON.generate [[]], :max_nesting => 1 }
|
429
|
-
assert_equal '[[]]', JSON.generate([[]], :max_nesting => 2)
|
430
|
-
assert_raises(JSON::NestingError) { JSON.generate too_deep_ary }
|
431
|
-
assert_raises(JSON::NestingError) { JSON.generate too_deep_ary, :max_nesting => 100 }
|
432
|
-
ok = JSON.generate too_deep_ary, :max_nesting => 101
|
433
|
-
assert_equal too_deep, ok
|
434
|
-
ok = JSON.generate too_deep_ary, :max_nesting => nil
|
435
|
-
assert_equal too_deep, ok
|
436
|
-
ok = JSON.generate too_deep_ary, :max_nesting => false
|
437
|
-
assert_equal too_deep, ok
|
438
|
-
ok = JSON.generate too_deep_ary, :max_nesting => 0
|
439
|
-
assert_equal too_deep, ok
|
440
|
-
end
|
441
|
-
|
442
|
-
def test_symbolize_names
|
443
|
-
assert_equal({ "foo" => "bar", "baz" => "quux" },
|
444
|
-
JSON.parse('{"foo":"bar", "baz":"quux"}'))
|
445
|
-
assert_equal({ :foo => "bar", :baz => "quux" },
|
446
|
-
JSON.parse('{"foo":"bar", "baz":"quux"}', :symbolize_names => true))
|
447
|
-
end
|
448
|
-
|
449
|
-
def test_load
|
450
|
-
assert_equal @hash, JSON.load(@json)
|
451
|
-
tempfile = Tempfile.open('json')
|
452
|
-
tempfile.write @json
|
453
|
-
tempfile.rewind
|
454
|
-
assert_equal @hash, JSON.load(tempfile)
|
455
|
-
stringio = StringIO.new(@json)
|
456
|
-
stringio.rewind
|
457
|
-
assert_equal @hash, JSON.load(stringio)
|
458
|
-
assert_equal nil, JSON.load(nil)
|
459
|
-
assert_equal nil, JSON.load('')
|
460
|
-
ensure
|
461
|
-
tempfile.close!
|
462
|
-
end
|
463
|
-
|
464
|
-
def test_load_with_options
|
465
|
-
small_hash = JSON("foo" => 'bar')
|
466
|
-
symbol_hash = { :foo => 'bar' }
|
467
|
-
assert_equal symbol_hash, JSON.load(small_hash, nil, :symbolize_names => true)
|
468
|
-
end
|
469
|
-
|
470
|
-
def test_dump
|
471
|
-
too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
|
472
|
-
assert_equal too_deep, JSON.dump(eval(too_deep))
|
473
|
-
assert_kind_of String, Marshal.dump(eval(too_deep))
|
474
|
-
assert_raises(ArgumentError) { JSON.dump(eval(too_deep), 100) }
|
475
|
-
assert_raises(ArgumentError) { Marshal.dump(eval(too_deep), 100) }
|
476
|
-
assert_equal too_deep, JSON.dump(eval(too_deep), 101)
|
477
|
-
assert_kind_of String, Marshal.dump(eval(too_deep), 101)
|
478
|
-
output = StringIO.new
|
479
|
-
JSON.dump(eval(too_deep), output)
|
480
|
-
assert_equal too_deep, output.string
|
481
|
-
output = StringIO.new
|
482
|
-
JSON.dump(eval(too_deep), output, 101)
|
483
|
-
assert_equal too_deep, output.string
|
484
|
-
end
|
485
|
-
|
486
|
-
def test_dump_should_modify_defaults
|
487
|
-
max_nesting = JSON.dump_default_options[:max_nesting]
|
488
|
-
JSON.dump([], StringIO.new, 10)
|
489
|
-
assert_equal max_nesting, JSON.dump_default_options[:max_nesting]
|
490
|
-
end
|
491
|
-
|
492
|
-
def test_big_integers
|
493
|
-
json1 = JSON([orig = (1 << 31) - 1])
|
494
|
-
assert_equal orig, JSON[json1][0]
|
495
|
-
json2 = JSON([orig = 1 << 31])
|
496
|
-
assert_equal orig, JSON[json2][0]
|
497
|
-
json3 = JSON([orig = (1 << 62) - 1])
|
498
|
-
assert_equal orig, JSON[json3][0]
|
499
|
-
json4 = JSON([orig = 1 << 62])
|
500
|
-
assert_equal orig, JSON[json4][0]
|
501
|
-
json5 = JSON([orig = 1 << 64])
|
502
|
-
assert_equal orig, JSON[json5][0]
|
503
|
-
end
|
504
|
-
|
505
|
-
if defined?(JSON::Ext::Parser)
|
506
|
-
def test_allocate
|
507
|
-
parser = JSON::Ext::Parser.new("{}")
|
508
|
-
assert_raise(TypeError, '[ruby-core:35079]') {parser.__send__(:initialize, "{}")}
|
509
|
-
parser = JSON::Ext::Parser.allocate
|
510
|
-
assert_raise(TypeError, '[ruby-core:35079]') {parser.source}
|
511
|
-
end
|
512
|
-
end
|
513
|
-
|
514
|
-
def test_argument_encoding
|
515
|
-
source = "{}".force_encoding("ascii-8bit")
|
516
|
-
JSON::Parser.new(source)
|
517
|
-
assert_equal Encoding::ASCII_8BIT, source.encoding
|
518
|
-
end if defined?(Encoding::ASCII_8BIT)
|
519
|
-
end
|