json 1.8.3 → 2.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +2 -0
- data/.travis.yml +9 -11
- data/{CHANGES → CHANGES.md} +186 -90
- data/Gemfile +10 -6
- data/{COPYING-json-jruby → LICENSE} +5 -6
- data/{README-json-jruby.markdown → README-json-jruby.md} +0 -0
- data/{README.rdoc → README.md} +185 -134
- data/Rakefile +41 -40
- data/VERSION +1 -1
- data/ext/json/ext/fbuffer/fbuffer.h +0 -3
- data/ext/json/ext/generator/generator.c +142 -101
- data/ext/json/ext/generator/generator.h +7 -2
- data/ext/json/ext/parser/extconf.rb +3 -0
- data/ext/json/ext/parser/parser.c +383 -463
- data/ext/json/ext/parser/parser.h +4 -5
- data/ext/json/ext/parser/parser.rl +141 -184
- data/ext/json/extconf.rb +0 -1
- data/java/src/json/ext/ByteListTranscoder.java +1 -2
- data/java/src/json/ext/Generator.java +44 -22
- data/java/src/json/ext/GeneratorMethods.java +1 -2
- data/java/src/json/ext/GeneratorService.java +1 -2
- data/java/src/json/ext/GeneratorState.java +3 -56
- data/java/src/json/ext/OptionsReader.java +2 -3
- data/java/src/json/ext/Parser.java +132 -415
- data/java/src/json/ext/Parser.rl +48 -124
- data/java/src/json/ext/ParserService.java +1 -2
- data/java/src/json/ext/RuntimeInfo.java +1 -6
- data/java/src/json/ext/StringDecoder.java +1 -2
- data/java/src/json/ext/StringEncoder.java +5 -0
- data/java/src/json/ext/Utils.java +1 -2
- data/json-java.gemspec +16 -2
- data/json.gemspec +0 -0
- data/json_pure.gemspec +24 -26
- data/lib/json/add/bigdecimal.rb +3 -2
- data/lib/json/add/complex.rb +4 -3
- 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 +3 -3
- data/lib/json/add/range.rb +1 -1
- data/lib/json/add/rational.rb +3 -2
- data/lib/json/add/regexp.rb +3 -3
- data/lib/json/add/set.rb +29 -0
- 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 +26 -54
- data/lib/json/ext.rb +0 -6
- data/lib/json/generic_object.rb +5 -4
- data/lib/json/pure/generator.rb +63 -126
- data/lib/json/pure/parser.rb +41 -81
- data/lib/json/pure.rb +2 -8
- data/lib/json/version.rb +2 -1
- data/lib/json.rb +1 -0
- data/references/rfc7159.txt +899 -0
- data/tests/fixtures/obsolete_fail1.json +1 -0
- data/tests/{test_json_addition.rb → json_addition_test.rb} +32 -25
- data/tests/json_common_interface_test.rb +126 -0
- data/tests/json_encoding_test.rb +107 -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} +123 -39
- data/tests/{test_json_generic_object.rb → json_generic_object_test.rb} +15 -8
- data/tests/json_parser_test.rb +472 -0
- data/tests/json_string_matching_test.rb +38 -0
- data/tests/{setup_variant.rb → test_helper.rb} +6 -0
- data/tools/diff.sh +18 -0
- data/tools/fuzz.rb +1 -9
- metadata +30 -47
- data/COPYING +0 -58
- data/GPL +0 -340
- data/TODO +0 -1
- data/data/example.json +0 -1
- data/data/index.html +0 -38
- data/data/prototype.js +0 -4184
- data/tests/fixtures/fail1.json +0 -1
- data/tests/test_json.rb +0 -553
- 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
data/lib/json/pure/parser.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
#frozen_string_literal: false
|
1
2
|
require 'strscan'
|
2
3
|
|
3
4
|
module JSON
|
@@ -48,7 +49,7 @@ module JSON
|
|
48
49
|
)+
|
49
50
|
)mx
|
50
51
|
|
51
|
-
|
52
|
+
UNPARSED = Object.new.freeze
|
52
53
|
|
53
54
|
# Creates a new JSON::Pure::Parser instance for the string _source_.
|
54
55
|
#
|
@@ -58,23 +59,23 @@ module JSON
|
|
58
59
|
# structures. Disable depth checking with :max_nesting => false|nil|0,
|
59
60
|
# it defaults to 100.
|
60
61
|
# * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
|
61
|
-
# defiance of RFC
|
62
|
+
# defiance of RFC 7159 to be parsed by the Parser. This option defaults
|
62
63
|
# to false.
|
63
64
|
# * *symbolize_names*: If set to true, returns symbols for the names
|
64
|
-
# (keys) in a JSON object. Otherwise strings are returned, which is
|
65
|
-
# the default.
|
65
|
+
# (keys) in a JSON object. Otherwise strings are returned, which is
|
66
|
+
# also the default. It's not possible to use this option in
|
67
|
+
# conjunction with the *create_additions* option.
|
66
68
|
# * *create_additions*: If set to true, the Parser creates
|
67
69
|
# additions when if a matching class and create_id was found. This
|
68
70
|
# option defaults to false.
|
69
71
|
# * *object_class*: Defaults to Hash
|
70
72
|
# * *array_class*: Defaults to Array
|
71
|
-
# * *
|
72
|
-
#
|
73
|
+
# * *decimal_class*: Specifies which class to use instead of the default
|
74
|
+
# (Float) when parsing decimal numbers. This class must accept a single
|
75
|
+
# string argument in its constructor.
|
73
76
|
def initialize(source, opts = {})
|
74
77
|
opts ||= {}
|
75
|
-
|
76
|
-
source = convert_encoding source
|
77
|
-
end
|
78
|
+
source = convert_encoding source
|
78
79
|
super source
|
79
80
|
if !opts.key?(:max_nesting) # defaults to 100
|
80
81
|
@max_nesting = 100
|
@@ -90,56 +91,38 @@ module JSON
|
|
90
91
|
else
|
91
92
|
@create_additions = false
|
92
93
|
end
|
94
|
+
@symbolize_names && @create_additions and raise ArgumentError,
|
95
|
+
'options :symbolize_names and :create_additions cannot be used '\
|
96
|
+
'in conjunction'
|
93
97
|
@create_id = @create_additions ? JSON.create_id : nil
|
94
98
|
@object_class = opts[:object_class] || Hash
|
95
99
|
@array_class = opts[:array_class] || Array
|
100
|
+
@decimal_class = opts[:decimal_class]
|
96
101
|
@match_string = opts[:match_string]
|
97
102
|
end
|
98
103
|
|
99
104
|
alias source string
|
100
105
|
|
101
|
-
def quirks_mode?
|
102
|
-
!!@quirks_mode
|
103
|
-
end
|
104
|
-
|
105
106
|
def reset
|
106
107
|
super
|
107
108
|
@current_nesting = 0
|
108
109
|
end
|
109
110
|
|
110
|
-
# Parses the current JSON string _source_ and returns the
|
111
|
-
# structure as a result.
|
111
|
+
# Parses the current JSON string _source_ and returns the
|
112
|
+
# complete data structure as a result.
|
112
113
|
def parse
|
113
114
|
reset
|
114
115
|
obj = nil
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
if eos?
|
119
|
-
raise ParserError, "source did not contain any JSON!"
|
120
|
-
else
|
121
|
-
obj = parse_value
|
122
|
-
obj == UNPARSED and raise ParserError, "source did not contain any JSON!"
|
123
|
-
end
|
116
|
+
while !eos? && skip(IGNORE) do end
|
117
|
+
if eos?
|
118
|
+
raise ParserError, "source is not valid JSON!"
|
124
119
|
else
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
obj and raise ParserError, "source '#{peek(20)}' not in JSON!"
|
129
|
-
@current_nesting = 1
|
130
|
-
obj = parse_object
|
131
|
-
when scan(ARRAY_OPEN)
|
132
|
-
obj and raise ParserError, "source '#{peek(20)}' not in JSON!"
|
133
|
-
@current_nesting = 1
|
134
|
-
obj = parse_array
|
135
|
-
when skip(IGNORE)
|
136
|
-
;
|
137
|
-
else
|
138
|
-
raise ParserError, "source '#{peek(20)}' not in JSON!"
|
139
|
-
end
|
140
|
-
end
|
141
|
-
obj or raise ParserError, "source did not contain any JSON!"
|
120
|
+
obj = parse_value
|
121
|
+
UNPARSED.equal?(obj) and raise ParserError,
|
122
|
+
"source is not valid JSON!"
|
142
123
|
end
|
124
|
+
while !eos? && skip(IGNORE) do end
|
125
|
+
eos? or raise ParserError, "source is not valid JSON!"
|
143
126
|
obj
|
144
127
|
end
|
145
128
|
|
@@ -149,43 +132,12 @@ module JSON
|
|
149
132
|
if source.respond_to?(:to_str)
|
150
133
|
source = source.to_str
|
151
134
|
else
|
152
|
-
raise TypeError,
|
135
|
+
raise TypeError,
|
136
|
+
"#{source.inspect} is not like a string"
|
153
137
|
end
|
154
|
-
if
|
155
|
-
|
156
|
-
b = source[0, 4].bytes.to_a
|
157
|
-
source =
|
158
|
-
case
|
159
|
-
when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0
|
160
|
-
source.dup.force_encoding(::Encoding::UTF_32BE).encode!(::Encoding::UTF_8)
|
161
|
-
when b.size >= 4 && b[0] == 0 && b[2] == 0
|
162
|
-
source.dup.force_encoding(::Encoding::UTF_16BE).encode!(::Encoding::UTF_8)
|
163
|
-
when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0
|
164
|
-
source.dup.force_encoding(::Encoding::UTF_32LE).encode!(::Encoding::UTF_8)
|
165
|
-
when b.size >= 4 && b[1] == 0 && b[3] == 0
|
166
|
-
source.dup.force_encoding(::Encoding::UTF_16LE).encode!(::Encoding::UTF_8)
|
167
|
-
else
|
168
|
-
source.dup
|
169
|
-
end
|
170
|
-
else
|
171
|
-
source = source.encode(::Encoding::UTF_8)
|
172
|
-
end
|
138
|
+
if source.encoding != ::Encoding::ASCII_8BIT
|
139
|
+
source = source.encode(::Encoding::UTF_8)
|
173
140
|
source.force_encoding(::Encoding::ASCII_8BIT)
|
174
|
-
else
|
175
|
-
b = source
|
176
|
-
source =
|
177
|
-
case
|
178
|
-
when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0
|
179
|
-
JSON.iconv('utf-8', 'utf-32be', b)
|
180
|
-
when b.size >= 4 && b[0] == 0 && b[2] == 0
|
181
|
-
JSON.iconv('utf-8', 'utf-16be', b)
|
182
|
-
when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0
|
183
|
-
JSON.iconv('utf-8', 'utf-32le', b)
|
184
|
-
when b.size >= 4 && b[1] == 0 && b[3] == 0
|
185
|
-
JSON.iconv('utf-8', 'utf-16le', b)
|
186
|
-
else
|
187
|
-
b
|
188
|
-
end
|
189
141
|
end
|
190
142
|
source
|
191
143
|
end
|
@@ -245,7 +197,15 @@ module JSON
|
|
245
197
|
def parse_value
|
246
198
|
case
|
247
199
|
when scan(FLOAT)
|
248
|
-
|
200
|
+
if @decimal_class then
|
201
|
+
if @decimal_class == BigDecimal then
|
202
|
+
BigDecimal(self[1])
|
203
|
+
else
|
204
|
+
@decimal_class.new(self[1]) || Float(self[1])
|
205
|
+
end
|
206
|
+
else
|
207
|
+
Float(self[1])
|
208
|
+
end
|
249
209
|
when scan(INTEGER)
|
250
210
|
Integer(self[1])
|
251
211
|
when scan(TRUE)
|
@@ -254,7 +214,7 @@ module JSON
|
|
254
214
|
false
|
255
215
|
when scan(NULL)
|
256
216
|
nil
|
257
|
-
when (string = parse_string)
|
217
|
+
when !UNPARSED.equal?(string = parse_string)
|
258
218
|
string
|
259
219
|
when scan(ARRAY_OPEN)
|
260
220
|
@current_nesting += 1
|
@@ -284,7 +244,7 @@ module JSON
|
|
284
244
|
delim = false
|
285
245
|
until eos?
|
286
246
|
case
|
287
|
-
when (value = parse_value)
|
247
|
+
when !UNPARSED.equal?(value = parse_value)
|
288
248
|
delim = false
|
289
249
|
result << value
|
290
250
|
skip(IGNORE)
|
@@ -316,13 +276,13 @@ module JSON
|
|
316
276
|
delim = false
|
317
277
|
until eos?
|
318
278
|
case
|
319
|
-
when (string = parse_string)
|
279
|
+
when !UNPARSED.equal?(string = parse_string)
|
320
280
|
skip(IGNORE)
|
321
281
|
unless scan(PAIR_DELIMITER)
|
322
282
|
raise ParserError, "expected ':' in object at '#{peek(20)}'!"
|
323
283
|
end
|
324
284
|
skip(IGNORE)
|
325
|
-
unless (value = parse_value)
|
285
|
+
unless UNPARSED.equal?(value = parse_value)
|
326
286
|
result[@symbolize_names ? string.to_sym : string] = value
|
327
287
|
delim = false
|
328
288
|
skip(IGNORE)
|
data/lib/json/pure.rb
CHANGED
@@ -1,17 +1,11 @@
|
|
1
|
-
if ENV['SIMPLECOV_COVERAGE'].to_i == 1
|
2
|
-
require 'simplecov'
|
3
|
-
SimpleCov.start do
|
4
|
-
add_filter "/tests/"
|
5
|
-
end
|
6
|
-
end
|
7
1
|
require 'json/common'
|
8
|
-
require 'json/pure/parser'
|
9
|
-
require 'json/pure/generator'
|
10
2
|
|
11
3
|
module JSON
|
12
4
|
# This module holds all the modules/classes that implement JSON's
|
13
5
|
# functionality in pure ruby.
|
14
6
|
module Pure
|
7
|
+
require 'json/pure/parser'
|
8
|
+
require 'json/pure/generator'
|
15
9
|
$DEBUG and warn "Using Pure library for JSON."
|
16
10
|
JSON.parser = Parser
|
17
11
|
JSON.generator = Generator
|
data/lib/json/version.rb
CHANGED
data/lib/json.rb
CHANGED