json 2.3.0-java → 2.3.1-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/json.rb +378 -29
- data/lib/json/common.rb +324 -89
- data/lib/json/pure/generator.rb +1 -1
- data/lib/json/pure/parser.rb +2 -2
- data/lib/json/version.rb +1 -1
- data/tests/json_fixtures_test.rb +6 -1
- metadata +13 -10
- data/lib/json/ext/generator.jar +0 -0
- data/lib/json/ext/parser.jar +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dab6c5a90b48e148a0987ce6cf13284c517b672bf9eedc6672b9b0cfb7123af5
|
4
|
+
data.tar.gz: 2668c1bbb2ece7579896373029a947671894b8882072cc5cc56908210a5973cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0715338d655adec7af365f721ac56a2737436f8771a002cb303f86b7b800717378e84e9401a09a5ab7de1a4116d3891ad15ad8aa598747296004d55adb2ff2bd'
|
7
|
+
data.tar.gz: c1b6446ef0b012974aed4595de272179109a28ce890e6459f39e991b2ed10b5e0997cb37c03a32bf84ba634b6347d80e8144afa7e2771b60e458f36beac8ee53
|
data/lib/json.rb
CHANGED
@@ -2,55 +2,404 @@
|
|
2
2
|
require 'json/common'
|
3
3
|
|
4
4
|
##
|
5
|
-
# = JavaScript Object Notation (JSON)
|
5
|
+
# = JavaScript \Object Notation (\JSON)
|
6
6
|
#
|
7
|
-
# JSON is a lightweight data-interchange format.
|
8
|
-
# humans to read and write. Plus, equally simple for machines to generate or parse.
|
9
|
-
# JSON is completely language agnostic, making it the ideal interchange format.
|
7
|
+
# \JSON is a lightweight data-interchange format.
|
10
8
|
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
9
|
+
# A \JSON value is one of the following:
|
10
|
+
# - Double-quoted text: <tt>"foo"</tt>.
|
11
|
+
# - Number: +1+, +1.0+, +2.0e2+.
|
12
|
+
# - Boolean: +true+, +false+.
|
13
|
+
# - Null: +null+.
|
14
|
+
# - \Array: an ordered list of values, enclosed by square brackets:
|
15
|
+
# ["foo", 1, 1.0, 2.0e2, true, false, null]
|
14
16
|
#
|
15
|
-
#
|
17
|
+
# - \Object: a collection of name/value pairs, enclosed by curly braces;
|
18
|
+
# each name is double-quoted text;
|
19
|
+
# the values may be any \JSON values:
|
20
|
+
# {"a": "foo", "b": 1, "c": 1.0, "d": 2.0e2, "e": true, "f": false, "g": null}
|
16
21
|
#
|
17
|
-
#
|
22
|
+
# A \JSON array or object may contain nested arrays, objects, and scalars
|
23
|
+
# to any depth:
|
24
|
+
# {"foo": {"bar": 1, "baz": 2}, "bat": [0, 1, 2]}
|
25
|
+
# [{"foo": 0, "bar": 1}, ["baz", 2]]
|
18
26
|
#
|
19
|
-
#
|
20
|
-
# your existing application:
|
27
|
+
# == Using \Module \JSON
|
21
28
|
#
|
29
|
+
# To make module \JSON available in your code, begin with:
|
22
30
|
# require 'json'
|
23
31
|
#
|
24
|
-
#
|
25
|
-
# puts my_hash["hello"] => "goodbye"
|
32
|
+
# All examples here assume that this has been done.
|
26
33
|
#
|
27
|
-
#
|
28
|
-
# the argument to be a string and can't convert objects like a hash or array.
|
34
|
+
# === Parsing \JSON
|
29
35
|
#
|
30
|
-
#
|
36
|
+
# You can parse a \String containing \JSON data using
|
37
|
+
# either of two methods:
|
38
|
+
# - <tt>JSON.parse(source, opts)</tt>
|
39
|
+
# - <tt>JSON.parse!(source, opts)</tt>
|
31
40
|
#
|
32
|
-
#
|
41
|
+
# where
|
42
|
+
# - +source+ is a Ruby object.
|
43
|
+
# - +opts+ is a \Hash object containing options
|
44
|
+
# that control both input allowed and output formatting.
|
33
45
|
#
|
34
|
-
#
|
35
|
-
#
|
46
|
+
# The difference between the two methods
|
47
|
+
# is that JSON.parse! omits some checks
|
48
|
+
# and may not be safe for some +source+ data;
|
49
|
+
# use it only for data from trusted sources.
|
50
|
+
# Use the safer method JSON.parse for less trusted sources.
|
36
51
|
#
|
37
|
-
#
|
52
|
+
# ==== Parsing \JSON Arrays
|
38
53
|
#
|
39
|
-
#
|
40
|
-
#
|
54
|
+
# When +source+ is a \JSON array, JSON.parse by default returns a Ruby \Array:
|
55
|
+
# json = '["foo", 1, 1.0, 2.0e2, true, false, null]'
|
56
|
+
# ruby = JSON.parse(json)
|
57
|
+
# ruby # => ["foo", 1, 1.0, 200.0, true, false, nil]
|
58
|
+
# ruby.class # => Array
|
41
59
|
#
|
42
|
-
#
|
60
|
+
# The \JSON array may contain nested arrays, objects, and scalars
|
61
|
+
# to any depth:
|
62
|
+
# json = '[{"foo": 0, "bar": 1}, ["baz", 2]]'
|
63
|
+
# JSON.parse(json) # => [{"foo"=>0, "bar"=>1}, ["baz", 2]]
|
43
64
|
#
|
44
|
-
#
|
45
|
-
#
|
65
|
+
# ==== Parsing \JSON \Objects
|
66
|
+
#
|
67
|
+
# When the source is a \JSON object, JSON.parse by default returns a Ruby \Hash:
|
68
|
+
# json = '{"a": "foo", "b": 1, "c": 1.0, "d": 2.0e2, "e": true, "f": false, "g": null}'
|
69
|
+
# ruby = JSON.parse(json)
|
70
|
+
# ruby # => {"a"=>"foo", "b"=>1, "c"=>1.0, "d"=>200.0, "e"=>true, "f"=>false, "g"=>nil}
|
71
|
+
# ruby.class # => Hash
|
72
|
+
#
|
73
|
+
# The \JSON object may contain nested arrays, objects, and scalars
|
74
|
+
# to any depth:
|
75
|
+
# json = '{"foo": {"bar": 1, "baz": 2}, "bat": [0, 1, 2]}'
|
76
|
+
# JSON.parse(json) # => {"foo"=>{"bar"=>1, "baz"=>2}, "bat"=>[0, 1, 2]}
|
77
|
+
#
|
78
|
+
# ==== Parsing \JSON Scalars
|
79
|
+
#
|
80
|
+
# When the source is a \JSON scalar (not an array or object),
|
81
|
+
# JSON.parse returns a Ruby scalar.
|
82
|
+
#
|
83
|
+
# \String:
|
84
|
+
# ruby = JSON.parse('"foo"')
|
85
|
+
# ruby # => 'foo'
|
86
|
+
# ruby.class # => String
|
87
|
+
# \Integer:
|
88
|
+
# ruby = JSON.parse('1')
|
89
|
+
# ruby # => 1
|
90
|
+
# ruby.class # => Integer
|
91
|
+
# \Float:
|
92
|
+
# ruby = JSON.parse('1.0')
|
93
|
+
# ruby # => 1.0
|
94
|
+
# ruby.class # => Float
|
95
|
+
# ruby = JSON.parse('2.0e2')
|
96
|
+
# ruby # => 200
|
97
|
+
# ruby.class # => Float
|
98
|
+
# Boolean:
|
99
|
+
# ruby = JSON.parse('true')
|
100
|
+
# ruby # => true
|
101
|
+
# ruby.class # => TrueClass
|
102
|
+
# ruby = JSON.parse('false')
|
103
|
+
# ruby # => false
|
104
|
+
# ruby.class # => FalseClass
|
105
|
+
# Null:
|
106
|
+
# ruby = JSON.parse('null')
|
107
|
+
# ruby # => nil
|
108
|
+
# ruby.class # => NilClass
|
109
|
+
#
|
110
|
+
# === Generating \JSON
|
111
|
+
#
|
112
|
+
# To generate a Ruby \String containing \JSON data,
|
113
|
+
# use method <tt>JSON.generate(source, opts)</tt>, where
|
114
|
+
# - +source+ is a Ruby object.
|
115
|
+
# - +opts+ is a \Hash object containing options
|
116
|
+
# that control both input allowed and output formatting.
|
117
|
+
#
|
118
|
+
# ==== Generating \JSON from Arrays
|
119
|
+
#
|
120
|
+
# When the source is a Ruby \Array, JSON.generate returns
|
121
|
+
# a \String containing a \JSON array:
|
122
|
+
# ruby = [0, 's', :foo]
|
123
|
+
# json = JSON.generate(ruby)
|
124
|
+
# json # => '[0,"s","foo"]'
|
125
|
+
#
|
126
|
+
# The Ruby \Array array may contain nested arrays, hashes, and scalars
|
127
|
+
# to any depth:
|
128
|
+
# ruby = [0, [1, 2], {foo: 3, bar: 4}]
|
129
|
+
# json = JSON.generate(ruby)
|
130
|
+
# json # => '[0,[1,2],{"foo":3,"bar":4}]'
|
131
|
+
#
|
132
|
+
# ==== Generating \JSON from Hashes
|
133
|
+
#
|
134
|
+
# When the source is a Ruby \Hash, JSON.generate returns
|
135
|
+
# a \String containing a \JSON object:
|
136
|
+
# ruby = {foo: 0, bar: 's', baz: :bat}
|
137
|
+
# json = JSON.generate(ruby)
|
138
|
+
# json # => '{"foo":0,"bar":"s","baz":"bat"}'
|
139
|
+
#
|
140
|
+
# The Ruby \Hash array may contain nested arrays, hashes, and scalars
|
141
|
+
# to any depth:
|
142
|
+
# ruby = {foo: [0, 1], bar: {baz: 2, bat: 3}, bam: :bad}
|
143
|
+
# json = JSON.generate(ruby)
|
144
|
+
# json # => '{"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"}'
|
145
|
+
#
|
146
|
+
# ==== Generating \JSON from Other Objects
|
147
|
+
#
|
148
|
+
# When the source is neither an \Array nor a \Hash,
|
149
|
+
# the generated \JSON data depends on the class of the source.
|
150
|
+
#
|
151
|
+
# When the source is a Ruby \Integer or \Float, JSON.generate returns
|
152
|
+
# a \String containing a \JSON number:
|
153
|
+
# JSON.generate(42) # => '42'
|
154
|
+
# JSON.generate(0.42) # => '0.42'
|
155
|
+
#
|
156
|
+
# When the source is a Ruby \String, JSON.generate returns
|
157
|
+
# a \String containing a \JSON string (with double-quotes):
|
158
|
+
# JSON.generate('A string') # => '"A string"'
|
159
|
+
#
|
160
|
+
# When the source is +true+, +false+ or +nil+, JSON.generate returns
|
161
|
+
# a \String containing the corresponding \JSON token:
|
162
|
+
# JSON.generate(true) # => 'true'
|
163
|
+
# JSON.generate(false) # => 'false'
|
164
|
+
# JSON.generate(nil) # => 'null'
|
165
|
+
#
|
166
|
+
# When the source is none of the above, JSON.generate returns
|
167
|
+
# a \String containing a \JSON string representation of the source:
|
168
|
+
# JSON.generate(:foo) # => '"foo"'
|
169
|
+
# JSON.generate(Complex(0, 0)) # => '"0+0i"'
|
170
|
+
# JSON.generate(Dir.new('.')) # => '"#<Dir>"'
|
171
|
+
#
|
172
|
+
# == \JSON Additions
|
173
|
+
#
|
174
|
+
# When you "round trip" a non-\String object from Ruby to \JSON and back,
|
175
|
+
# you have a new \String, instead of the object you began with:
|
176
|
+
# ruby0 = Range.new(0, 2)
|
177
|
+
# json = JSON.generate(ruby0)
|
178
|
+
# json # => '0..2"'
|
179
|
+
# ruby1 = JSON.parse(json)
|
180
|
+
# ruby1 # => '0..2'
|
181
|
+
# ruby1.class # => String
|
182
|
+
#
|
183
|
+
# You can use \JSON _additions_ to preserve the original object.
|
184
|
+
# The addition is an extension of a ruby class, so that:
|
185
|
+
# - \JSON.generate stores more information in the \JSON string.
|
186
|
+
# - \JSON.parse, called with option +create_additions+,
|
187
|
+
# uses that information to create a proper Ruby object.
|
46
188
|
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
189
|
+
# This example shows a \Range being generated into \JSON
|
190
|
+
# and parsed back into Ruby, both without and with
|
191
|
+
# the addition for \Range:
|
192
|
+
# ruby = Range.new(0, 2)
|
193
|
+
# # This passage does not use the addition for Range.
|
194
|
+
# json0 = JSON.generate(ruby)
|
195
|
+
# ruby0 = JSON.parse(json0)
|
196
|
+
# # This passage uses the addition for Range.
|
197
|
+
# require 'json/add/range'
|
198
|
+
# json1 = JSON.generate(ruby)
|
199
|
+
# ruby1 = JSON.parse(json1, create_additions: true)
|
200
|
+
# # Make a nice display.
|
201
|
+
# display = <<EOT
|
202
|
+
# Generated JSON:
|
203
|
+
# Without addition: #{json0} (#{json0.class})
|
204
|
+
# With addition: #{json1} (#{json1.class})
|
205
|
+
# Parsed JSON:
|
206
|
+
# Without addition: #{ruby0.inspect} (#{ruby0.class})
|
207
|
+
# With addition: #{ruby1.inspect} (#{ruby1.class})
|
208
|
+
# EOT
|
209
|
+
# puts display
|
50
210
|
#
|
211
|
+
# This output shows the different results:
|
212
|
+
# Generated JSON:
|
213
|
+
# Without addition: "0..2" (String)
|
214
|
+
# With addition: {"json_class":"Range","a":[0,2,false]} (String)
|
215
|
+
# Parsed JSON:
|
216
|
+
# Without addition: "0..2" (String)
|
217
|
+
# With addition: 0..2 (Range)
|
218
|
+
#
|
219
|
+
# The \JSON module includes additions for certain classes.
|
220
|
+
# You can also craft custom additions.
|
221
|
+
# See {Custom \JSON Additions}[#module-JSON-label-Custom+JSON+Additions].
|
222
|
+
#
|
223
|
+
# === Built-in Additions
|
224
|
+
#
|
225
|
+
# The \JSON module includes additions for certain classes.
|
226
|
+
# To use an addition, +require+ its source:
|
227
|
+
# - BigDecimal: <tt>require 'json/add/bigdecimal'</tt>
|
228
|
+
# - Complex: <tt>require 'json/add/complex'</tt>
|
229
|
+
# - Date: <tt>require 'json/add/date'</tt>
|
230
|
+
# - DateTime: <tt>require 'json/add/date_time'</tt>
|
231
|
+
# - Exception: <tt>require 'json/add/exception'</tt>
|
232
|
+
# - OpenStruct: <tt>require 'json/add/ostruct'</tt>
|
233
|
+
# - Range: <tt>require 'json/add/range'</tt>
|
234
|
+
# - Rational: <tt>require 'json/add/rational'</tt>
|
235
|
+
# - Regexp: <tt>require 'json/add/regexp'</tt>
|
236
|
+
# - Set: <tt>require 'json/add/set'</tt>
|
237
|
+
# - Struct: <tt>require 'json/add/struct'</tt>
|
238
|
+
# - Symbol: <tt>require 'json/add/symbol'</tt>
|
239
|
+
# - Time: <tt>require 'json/add/time'</tt>
|
240
|
+
#
|
241
|
+
# To reduce punctuation clutter, the examples below
|
242
|
+
# show the generated \JSON via +puts+, rather than the usual +inspect+,
|
243
|
+
#
|
244
|
+
# \BigDecimal:
|
245
|
+
# require 'json/add/bigdecimal'
|
246
|
+
# ruby0 = BigDecimal(0) # 0.0
|
247
|
+
# json = JSON.generate(ruby0) # {"json_class":"BigDecimal","b":"27:0.0"}
|
248
|
+
# ruby1 = JSON.parse(json, create_additions: true) # 0.0
|
249
|
+
# ruby1.class # => BigDecimal
|
250
|
+
#
|
251
|
+
# \Complex:
|
252
|
+
# require 'json/add/complex'
|
253
|
+
# ruby0 = Complex(1+0i) # 1+0i
|
254
|
+
# json = JSON.generate(ruby0) # {"json_class":"Complex","r":1,"i":0}
|
255
|
+
# ruby1 = JSON.parse(json, create_additions: true) # 1+0i
|
256
|
+
# ruby1.class # Complex
|
257
|
+
#
|
258
|
+
# \Date:
|
259
|
+
# require 'json/add/date'
|
260
|
+
# ruby0 = Date.today # 2020-05-02
|
261
|
+
# json = JSON.generate(ruby0) # {"json_class":"Date","y":2020,"m":5,"d":2,"sg":2299161.0}
|
262
|
+
# ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02
|
263
|
+
# ruby1.class # Date
|
264
|
+
#
|
265
|
+
# \DateTime:
|
266
|
+
# require 'json/add/date_time'
|
267
|
+
# ruby0 = DateTime.now # 2020-05-02T10:38:13-05:00
|
268
|
+
# json = JSON.generate(ruby0) # {"json_class":"DateTime","y":2020,"m":5,"d":2,"H":10,"M":38,"S":13,"of":"-5/24","sg":2299161.0}
|
269
|
+
# ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02T10:38:13-05:00
|
270
|
+
# ruby1.class # DateTime
|
271
|
+
#
|
272
|
+
# \Exception (and its subclasses including \RuntimeError):
|
273
|
+
# require 'json/add/exception'
|
274
|
+
# ruby0 = Exception.new('A message') # A message
|
275
|
+
# json = JSON.generate(ruby0) # {"json_class":"Exception","m":"A message","b":null}
|
276
|
+
# ruby1 = JSON.parse(json, create_additions: true) # A message
|
277
|
+
# ruby1.class # Exception
|
278
|
+
# ruby0 = RuntimeError.new('Another message') # Another message
|
279
|
+
# json = JSON.generate(ruby0) # {"json_class":"RuntimeError","m":"Another message","b":null}
|
280
|
+
# ruby1 = JSON.parse(json, create_additions: true) # Another message
|
281
|
+
# ruby1.class # RuntimeError
|
282
|
+
#
|
283
|
+
# \OpenStruct:
|
284
|
+
# require 'json/add/ostruct'
|
285
|
+
# ruby0 = OpenStruct.new(name: 'Matz', language: 'Ruby') # #<OpenStruct name="Matz", language="Ruby">
|
286
|
+
# json = JSON.generate(ruby0) # {"json_class":"OpenStruct","t":{"name":"Matz","language":"Ruby"}}
|
287
|
+
# ruby1 = JSON.parse(json, create_additions: true) # #<OpenStruct name="Matz", language="Ruby">
|
288
|
+
# ruby1.class # OpenStruct
|
289
|
+
#
|
290
|
+
# \Range:
|
291
|
+
# require 'json/add/range'
|
292
|
+
# ruby0 = Range.new(0, 2) # 0..2
|
293
|
+
# json = JSON.generate(ruby0) # {"json_class":"Range","a":[0,2,false]}
|
294
|
+
# ruby1 = JSON.parse(json, create_additions: true) # 0..2
|
295
|
+
# ruby1.class # Range
|
296
|
+
#
|
297
|
+
# \Rational:
|
298
|
+
# require 'json/add/rational'
|
299
|
+
# ruby0 = Rational(1, 3) # 1/3
|
300
|
+
# json = JSON.generate(ruby0) # {"json_class":"Rational","n":1,"d":3}
|
301
|
+
# ruby1 = JSON.parse(json, create_additions: true) # 1/3
|
302
|
+
# ruby1.class # Rational
|
303
|
+
#
|
304
|
+
# \Regexp:
|
305
|
+
# require 'json/add/regexp'
|
306
|
+
# ruby0 = Regexp.new('foo') # (?-mix:foo)
|
307
|
+
# json = JSON.generate(ruby0) # {"json_class":"Regexp","o":0,"s":"foo"}
|
308
|
+
# ruby1 = JSON.parse(json, create_additions: true) # (?-mix:foo)
|
309
|
+
# ruby1.class # Regexp
|
310
|
+
#
|
311
|
+
# \Set:
|
312
|
+
# require 'json/add/set'
|
313
|
+
# ruby0 = Set.new([0, 1, 2]) # #<Set: {0, 1, 2}>
|
314
|
+
# json = JSON.generate(ruby0) # {"json_class":"Set","a":[0,1,2]}
|
315
|
+
# ruby1 = JSON.parse(json, create_additions: true) # #<Set: {0, 1, 2}>
|
316
|
+
# ruby1.class # Set
|
317
|
+
#
|
318
|
+
# \Struct:
|
319
|
+
# require 'json/add/struct'
|
320
|
+
# Customer = Struct.new(:name, :address) # Customer
|
321
|
+
# ruby0 = Customer.new("Dave", "123 Main") # #<struct Customer name="Dave", address="123 Main">
|
322
|
+
# json = JSON.generate(ruby0) # {"json_class":"Customer","v":["Dave","123 Main"]}
|
323
|
+
# ruby1 = JSON.parse(json, create_additions: true) # #<struct Customer name="Dave", address="123 Main">
|
324
|
+
# ruby1.class # Customer
|
325
|
+
#
|
326
|
+
# \Symbol:
|
327
|
+
# require 'json/add/symbol'
|
328
|
+
# ruby0 = :foo # foo
|
329
|
+
# json = JSON.generate(ruby0) # {"json_class":"Symbol","s":"foo"}
|
330
|
+
# ruby1 = JSON.parse(json, create_additions: true) # foo
|
331
|
+
# ruby1.class # Symbol
|
332
|
+
#
|
333
|
+
# \Time:
|
334
|
+
# require 'json/add/time'
|
335
|
+
# ruby0 = Time.now # 2020-05-02 11:28:26 -0500
|
336
|
+
# json = JSON.generate(ruby0) # {"json_class":"Time","s":1588436906,"n":840560000}
|
337
|
+
# ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02 11:28:26 -0500
|
338
|
+
# ruby1.class # Time
|
339
|
+
#
|
340
|
+
#
|
341
|
+
# === Custom \JSON Additions
|
342
|
+
#
|
343
|
+
# In addition to the \JSON additions provided,
|
344
|
+
# you can craft \JSON additions of your own,
|
345
|
+
# either for Ruby built-in classes or for user-defined classes.
|
346
|
+
#
|
347
|
+
# Here's a user-defined class +Foo+:
|
348
|
+
# class Foo
|
349
|
+
# attr_accessor :bar, :baz
|
350
|
+
# def initialize(bar, baz)
|
351
|
+
# self.bar = bar
|
352
|
+
# self.baz = baz
|
353
|
+
# end
|
354
|
+
# end
|
355
|
+
#
|
356
|
+
# Here's the \JSON addition for it:
|
357
|
+
# # Extend class Foo with JSON addition.
|
358
|
+
# class Foo
|
359
|
+
# # Serialize Foo object with its class name and arguments
|
360
|
+
# def to_json(*args)
|
361
|
+
# {
|
362
|
+
# JSON.create_id => self.class.name,
|
363
|
+
# 'a' => [ bar, baz ]
|
364
|
+
# }.to_json(*args)
|
365
|
+
# end
|
366
|
+
# # Deserialize JSON string by constructing new Foo object with arguments.
|
367
|
+
# def self.json_create(object)
|
368
|
+
# new(*object['a'])
|
369
|
+
# end
|
370
|
+
# end
|
371
|
+
#
|
372
|
+
# Demonstration:
|
51
373
|
# require 'json'
|
374
|
+
# # This Foo object has no custom addition.
|
375
|
+
# foo0 = Foo.new(0, 1)
|
376
|
+
# json0 = JSON.generate(foo0)
|
377
|
+
# obj0 = JSON.parse(json0)
|
378
|
+
# # Lood the custom addition.
|
379
|
+
# require_relative 'foo_addition'
|
380
|
+
# # This foo has the custom addition.
|
381
|
+
# foo1 = Foo.new(0, 1)
|
382
|
+
# json1 = JSON.generate(foo1)
|
383
|
+
# obj1 = JSON.parse(json1, create_additions: true)
|
384
|
+
# # Make a nice display.
|
385
|
+
# display = <<EOT
|
386
|
+
# Generated JSON:
|
387
|
+
# Without custom addition: #{json0} (#{json0.class})
|
388
|
+
# With custom addition: #{json1} (#{json1.class})
|
389
|
+
# Parsed JSON:
|
390
|
+
# Without custom addition: #{obj0.inspect} (#{obj0.class})
|
391
|
+
# With custom addition: #{obj1.inspect} (#{obj1.class})
|
392
|
+
# EOT
|
393
|
+
# puts display
|
394
|
+
#
|
395
|
+
# Output:
|
52
396
|
#
|
53
|
-
#
|
397
|
+
# Generated JSON:
|
398
|
+
# Without custom addition: "#<Foo:0x0000000006534e80>" (String)
|
399
|
+
# With custom addition: {"json_class":"Foo","a":[0,1]} (String)
|
400
|
+
# Parsed JSON:
|
401
|
+
# Without custom addition: "#<Foo:0x0000000006534e80>" (String)
|
402
|
+
# With custom addition: #<Foo:0x0000000006473bb8 @bar=0, @baz=1> (Foo)
|
54
403
|
#
|
55
404
|
module JSON
|
56
405
|
require 'json/version'
|
data/lib/json/common.rb
CHANGED
@@ -4,12 +4,15 @@ require 'json/generic_object'
|
|
4
4
|
|
5
5
|
module JSON
|
6
6
|
class << self
|
7
|
-
# If
|
8
|
-
#
|
9
|
-
#
|
7
|
+
# If +object+ is a
|
8
|
+
# {String-convertible object}[doc/implicit_conversion_rdoc.html#label-String-Convertible+Objects]
|
9
|
+
# (implementing +to_str+), calls JSON.parse with +object+ and +opts+:
|
10
|
+
# json = '[0, 1, null]'
|
11
|
+
# JSON[json]# => [0, 1, nil]
|
10
12
|
#
|
11
|
-
#
|
12
|
-
#
|
13
|
+
# Otherwise, calls JSON.generate with +object+ and +opts+:
|
14
|
+
# ruby = [0, 1, nil]
|
15
|
+
# JSON[ruby] # => '[0,1,null]'
|
13
16
|
def [](object, opts = {})
|
14
17
|
if object.respond_to? :to_str
|
15
18
|
JSON.parse(object.to_str, opts)
|
@@ -19,7 +22,8 @@ module JSON
|
|
19
22
|
end
|
20
23
|
|
21
24
|
# Returns the JSON parser class that is used by JSON. This is either
|
22
|
-
# JSON::Ext::Parser or JSON::Pure::Parser
|
25
|
+
# JSON::Ext::Parser or JSON::Pure::Parser:
|
26
|
+
# JSON.parser # => JSON::Ext::Parser
|
23
27
|
attr_reader :parser
|
24
28
|
|
25
29
|
# Set the JSON parser class _parser_ to be used by JSON.
|
@@ -84,15 +88,18 @@ module JSON
|
|
84
88
|
end
|
85
89
|
|
86
90
|
# Returns the JSON generator module that is used by JSON. This is
|
87
|
-
# either JSON::Ext::Generator or JSON::Pure::Generator
|
91
|
+
# either JSON::Ext::Generator or JSON::Pure::Generator:
|
92
|
+
# JSON.generator # => JSON::Ext::Generator
|
88
93
|
attr_reader :generator
|
89
94
|
|
90
|
-
# Returns the JSON generator state class that is used by JSON. This is
|
91
|
-
# either JSON::Ext::Generator::State or JSON::Pure::Generator::State
|
95
|
+
# Sets or Returns the JSON generator state class that is used by JSON. This is
|
96
|
+
# either JSON::Ext::Generator::State or JSON::Pure::Generator::State:
|
97
|
+
# JSON.state # => JSON::Ext::Generator::State
|
92
98
|
attr_accessor :state
|
93
99
|
|
94
|
-
#
|
95
|
-
# hook of a class should be called
|
100
|
+
# Sets or returns create identifier, which is used to decide if the _json_create_
|
101
|
+
# hook of a class should be called; initial value is +json_class+:
|
102
|
+
# JSON.create_id # => 'json_class'
|
96
103
|
attr_accessor :create_id
|
97
104
|
end
|
98
105
|
self.create_id = 'json_class'
|
@@ -126,7 +133,7 @@ module JSON
|
|
126
133
|
# This exception is raised if a generator or unparser error occurs.
|
127
134
|
class GeneratorError < JSONError; end
|
128
135
|
# For backwards compatibility
|
129
|
-
UnparserError = GeneratorError
|
136
|
+
UnparserError = GeneratorError # :nodoc:
|
130
137
|
|
131
138
|
# This exception is raised if the required unicode support is missing on the
|
132
139
|
# system. Usually this means that the iconv library is not installed.
|
@@ -134,43 +141,139 @@ module JSON
|
|
134
141
|
|
135
142
|
module_function
|
136
143
|
|
137
|
-
#
|
138
|
-
#
|
139
|
-
#
|
140
|
-
#
|
141
|
-
#
|
142
|
-
#
|
143
|
-
#
|
144
|
-
#
|
145
|
-
#
|
146
|
-
#
|
147
|
-
#
|
148
|
-
#
|
149
|
-
#
|
150
|
-
#
|
151
|
-
#
|
152
|
-
#
|
153
|
-
#
|
154
|
-
#
|
144
|
+
# :call-seq:
|
145
|
+
# JSON.parse(source, opts) -> object
|
146
|
+
#
|
147
|
+
# Argument +source+ contains the \String to be parsed. It must be a
|
148
|
+
# {String-convertible object}[doc/implicit_conversion_rdoc.html#label-String-Convertible+Objects]
|
149
|
+
# (implementing +to_str+), and must contain valid \JSON data.
|
150
|
+
#
|
151
|
+
# Argument +opts+, if given, contains options for the parsing, and must be a
|
152
|
+
# {Hash-convertible object}[doc/implicit_conversion_rdoc.html#label-Hash-Convertible+Objects]
|
153
|
+
# (implementing +to_hash+).
|
154
|
+
#
|
155
|
+
# Returns the Ruby objects created by parsing the given +source+.
|
156
|
+
#
|
157
|
+
# ---
|
158
|
+
#
|
159
|
+
# When +source+ is a \JSON array, returns a Ruby \Array:
|
160
|
+
# source = '["foo", 1.0, true, false, null]'
|
161
|
+
# ruby = JSON.parse(source)
|
162
|
+
# ruby # => ["foo", 1.0, true, false, nil]
|
163
|
+
# ruby.class # => Array
|
164
|
+
#
|
165
|
+
# When +source+ is a \JSON object, returns a Ruby \Hash:
|
166
|
+
# source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}'
|
167
|
+
# ruby = JSON.parse(source)
|
168
|
+
# ruby # => {"a"=>"foo", "b"=>1.0, "c"=>true, "d"=>false, "e"=>nil}
|
169
|
+
# ruby.class # => Hash
|
170
|
+
#
|
171
|
+
# For examples of parsing for all \JSON data types, see
|
172
|
+
# {Parsing \JSON}[#module-JSON-label-Parsing+JSON].
|
173
|
+
#
|
174
|
+
# ====== Input Options
|
175
|
+
#
|
176
|
+
# Option +max_nesting+ (\Integer) specifies the maximum nesting depth allowed;
|
177
|
+
# defaults to +100+; specify +false+ to disable depth checking.
|
178
|
+
#
|
179
|
+
# With the default, +false+:
|
180
|
+
# source = '[0, [1, [2, [3]]]]'
|
181
|
+
# ruby = JSON.parse(source)
|
182
|
+
# ruby # => [0, [1, [2, [3]]]]
|
183
|
+
# Too deep:
|
184
|
+
# # Raises JSON::NestingError (nesting of 2 is too deep):
|
185
|
+
# JSON.parse(source, {max_nesting: 1})
|
186
|
+
# Bad value:
|
187
|
+
# # Raises TypeError (wrong argument type Symbol (expected Fixnum)):
|
188
|
+
# JSON.parse(source, {max_nesting: :foo})
|
189
|
+
#
|
190
|
+
# ---
|
191
|
+
#
|
192
|
+
# Option +allow_nan+ (boolean) specifies whether to allow
|
193
|
+
# NaN, Infinity, and MinusInfinity in +source+;
|
194
|
+
# defaults to +false+.
|
195
|
+
#
|
196
|
+
# With the default, +false+:
|
197
|
+
# # Raises JSON::ParserError (225: unexpected token at '[NaN]'):
|
198
|
+
# JSON.parse('[NaN]')
|
199
|
+
# # Raises JSON::ParserError (232: unexpected token at '[Infinity]'):
|
200
|
+
# JSON.parse('[Infinity]')
|
201
|
+
# # Raises JSON::ParserError (248: unexpected token at '[-Infinity]'):
|
202
|
+
# JSON.parse('[-Infinity]')
|
203
|
+
# Allow:
|
204
|
+
# source = '[NaN, Infinity, -Infinity]'
|
205
|
+
# ruby = JSON.parse(source, {allow_nan: true})
|
206
|
+
# ruby # => [NaN, Infinity, -Infinity]
|
207
|
+
#
|
208
|
+
# ====== Output Options
|
209
|
+
#
|
210
|
+
# Option +symbolize_names+ (boolean) specifies whether returned \Hash keys
|
211
|
+
# should be Symbols;
|
212
|
+
# defaults to +false+ (use Strings).
|
213
|
+
#
|
214
|
+
# With the default, +false+:
|
215
|
+
# source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}'
|
216
|
+
# ruby = JSON.parse(source)
|
217
|
+
# ruby # => {"a"=>"foo", "b"=>1.0, "c"=>true, "d"=>false, "e"=>nil}
|
218
|
+
# Use Symbols:
|
219
|
+
# ruby = JSON.parse(source, {symbolize_names: true})
|
220
|
+
# ruby # => {:a=>"foo", :b=>1.0, :c=>true, :d=>false, :e=>nil}
|
221
|
+
#
|
222
|
+
# ---
|
223
|
+
#
|
224
|
+
# Option +object_class+ (\Class) specifies the Ruby class to be used
|
225
|
+
# for each \JSON object;
|
226
|
+
# defaults to \Hash.
|
227
|
+
#
|
228
|
+
# With the default, \Hash:
|
229
|
+
# source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}'
|
230
|
+
# ruby = JSON.parse(source)
|
231
|
+
# ruby.class # => Hash
|
232
|
+
# Use class \OpenStruct:
|
233
|
+
# ruby = JSON.parse(source, {object_class: OpenStruct})
|
234
|
+
# ruby # => #<OpenStruct a="foo", b=1.0, c=true, d=false, e=nil>
|
235
|
+
#
|
236
|
+
# ---
|
237
|
+
#
|
238
|
+
# Option +array_class+ (\Class) specifies the Ruby class to be used
|
239
|
+
# for each \JSON array;
|
240
|
+
# defaults to \Array.
|
241
|
+
#
|
242
|
+
# With the default, \Array:
|
243
|
+
# source = '["foo", 1.0, true, false, null]'
|
244
|
+
# ruby = JSON.parse(source)
|
245
|
+
# ruby.class # => Array
|
246
|
+
# Use class \Set:
|
247
|
+
# ruby = JSON.parse(source, {array_class: Set})
|
248
|
+
# ruby # => #<Set: {"foo", 1.0, true, false, nil}>
|
249
|
+
#
|
250
|
+
# ---
|
251
|
+
#
|
252
|
+
# Option +create_additions+ (boolean) specifies whether to use \JSON additions in parsing.
|
253
|
+
# See {\JSON Additions}[#module-JSON-label-JSON+Additions].
|
254
|
+
#
|
255
|
+
# ====== Exceptions
|
256
|
+
#
|
257
|
+
# Raises an exception if +source+ is not valid JSON:
|
258
|
+
#
|
259
|
+
# # Raises JSON::ParserError (783: unexpected token at ''):
|
260
|
+
# JSON.parse('')
|
261
|
+
#
|
155
262
|
def parse(source, opts = {})
|
156
263
|
Parser.new(source, **(opts||{})).parse
|
157
264
|
end
|
158
265
|
|
159
|
-
#
|
160
|
-
#
|
161
|
-
#
|
162
|
-
#
|
163
|
-
#
|
164
|
-
#
|
165
|
-
#
|
166
|
-
#
|
167
|
-
#
|
168
|
-
#
|
169
|
-
#
|
170
|
-
# to true.
|
171
|
-
# * *create_additions*: If set to false, the Parser doesn't create
|
172
|
-
# additions even if a matching class and create_id was found. This option
|
173
|
-
# defaults to false.
|
266
|
+
# :call-seq:
|
267
|
+
# JSON.parse!(source, opts) -> object
|
268
|
+
#
|
269
|
+
# Calls
|
270
|
+
# parse(source, opts)
|
271
|
+
# with +source+ and possibly modified +opts+.
|
272
|
+
#
|
273
|
+
# Differences from JSON.parse:
|
274
|
+
# - Option +max_nesting+, if not provided, defaults to +false+,
|
275
|
+
# which disables checking for nesting depth.
|
276
|
+
# - Option +allow_nan+, if not provided, defaults to +true+.
|
174
277
|
def parse!(source, opts = {})
|
175
278
|
opts = {
|
176
279
|
:max_nesting => false,
|
@@ -179,32 +282,135 @@ module JSON
|
|
179
282
|
Parser.new(source, **(opts||{})).parse
|
180
283
|
end
|
181
284
|
|
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
|
-
#
|
285
|
+
# :call-seq:
|
286
|
+
# JSON.generate(obj, opts = nil) -> new_string
|
287
|
+
#
|
288
|
+
# Argument +obj+ is the Ruby object to be converted to \JSON.
|
289
|
+
#
|
290
|
+
# Argument +opts+, if given, contains options for the generation, and must be a
|
291
|
+
# {Hash-convertible object}[doc/implicit_conversion_rdoc.html#label-Hash-Convertible+Objects]
|
292
|
+
# (implementing +to_hash+).
|
293
|
+
#
|
294
|
+
# Returns a \String containing the generated \JSON data.
|
295
|
+
#
|
296
|
+
# See also JSON.fast_generate, JSON.pretty_generate.
|
297
|
+
#
|
298
|
+
# ---
|
299
|
+
#
|
300
|
+
# When +obj+ is an
|
301
|
+
# {Array-convertible object}[doc/implicit_conversion_rdoc.html#label-Array-Convertible+Objects]
|
302
|
+
# (implementing +to_ary+), returns a \String containing a \JSON array:
|
303
|
+
# obj = ["foo", 1.0, true, false, nil]
|
304
|
+
# json = JSON.generate(obj)
|
305
|
+
# json # => '["foo",1.0,true,false,null]'
|
306
|
+
#
|
307
|
+
# When +obj+ is a
|
308
|
+
# {Hash-convertible object}[doc/implicit_conversion_rdoc.html#label-Hash-Convertible+Objects],
|
309
|
+
# return a \String containing a \JSON object:
|
310
|
+
# obj = {foo: 0, bar: 's', baz: :bat}
|
311
|
+
# json = JSON.generate(obj)
|
312
|
+
# json # => '{"foo":0,"bar":"s","baz":"bat"}'
|
313
|
+
#
|
314
|
+
# For examples of generating from other Ruby objects, see
|
315
|
+
# {Generating \JSON from Other Objects}[#module-JSON-label-Generating+JSON+from+Other+Objects].
|
316
|
+
#
|
317
|
+
# ====== Input Options
|
318
|
+
#
|
319
|
+
# Option +allow_nan+ (boolean) specifies whether
|
320
|
+
# +NaN+, +Infinity+, and <tt>-Infinity</tt> may be generated;
|
321
|
+
# defaults to +false+.
|
322
|
+
#
|
323
|
+
# With the default, +false+:
|
324
|
+
# # Raises JSON::GeneratorError (920: NaN not allowed in JSON):
|
325
|
+
# JSON.generate(JSON::NaN)
|
326
|
+
# # Raises JSON::GeneratorError (917: Infinity not allowed in JSON):
|
327
|
+
# JSON.generate(JSON::Infinity)
|
328
|
+
# # Raises JSON::GeneratorError (917: -Infinity not allowed in JSON):
|
329
|
+
# JSON.generate(JSON::MinusInfinity)
|
330
|
+
#
|
331
|
+
# Allow:
|
332
|
+
# ruby = [Float::NaN, Float::Infinity, Float::MinusInfinity]
|
333
|
+
# JSON.generate(ruby, allow_nan: true) # => '[NaN,Infinity,-Infinity]'
|
334
|
+
#
|
335
|
+
# ---
|
336
|
+
#
|
337
|
+
# Option +max_nesting+ (\Integer) specifies the maximum nesting depth
|
338
|
+
# in +obj+; defaults to +100+.
|
339
|
+
#
|
340
|
+
# With the default, +100+:
|
341
|
+
# obj = [[[[[[0]]]]]]
|
342
|
+
# JSON.generate(obj) # => '[[[[[[0]]]]]]'
|
343
|
+
#
|
344
|
+
# Too deep:
|
345
|
+
# # Raises JSON::NestingError (nesting of 2 is too deep):
|
346
|
+
# JSON.generate(obj, max_nesting: 2)
|
347
|
+
#
|
348
|
+
# ====== Output Options
|
349
|
+
#
|
350
|
+
# The default formatting options generate the most compact
|
351
|
+
# \JSON data, all on one line and with no whitespace.
|
352
|
+
#
|
353
|
+
# You can use these formatting options to generate
|
354
|
+
# \JSON data in a more open format, using whitespace.
|
355
|
+
# See also JSON.pretty_generate.
|
356
|
+
#
|
357
|
+
# - Option +array_nl+ (\String) specifies a string (usually a newline)
|
358
|
+
# to be inserted after each \JSON array; defaults to the empty \String, <tt>''</tt>.
|
359
|
+
# - Option +object_nl+ (\String) specifies a string (usually a newline)
|
360
|
+
# to be inserted after each \JSON object; defaults to the empty \String, <tt>''</tt>.
|
361
|
+
# - Option +indent+ (\String) specifies the string (usually spaces) to be
|
362
|
+
# used for indentation; defaults to the empty \String, <tt>''</tt>;
|
363
|
+
# defaults to the empty \String, <tt>''</tt>;
|
364
|
+
# has no effect unless options +array_nl+ or +object_nl+ specify newlines.
|
365
|
+
# - Option +space+ (\String) specifies a string (usually a space) to be
|
366
|
+
# inserted after the colon in each \JSON object's pair;
|
367
|
+
# defaults to the empty \String, <tt>''</tt>.
|
368
|
+
# - Option +space_before+ (\String) specifies a string (usually a space) to be
|
369
|
+
# inserted before the colon in each \JSON object's pair;
|
370
|
+
# defaults to the empty \String, <tt>''</tt>.
|
371
|
+
#
|
372
|
+
# In this example, +obj+ is used first to generate the shortest
|
373
|
+
# \JSON data (no whitespace), then again with all formatting options
|
374
|
+
# specified:
|
375
|
+
#
|
376
|
+
# obj = {foo: [:bar, :baz], bat: {bam: 0, bad: 1}}
|
377
|
+
# json = JSON.generate(obj)
|
378
|
+
# puts 'Compact:', json
|
379
|
+
# opts = {
|
380
|
+
# array_nl: "\n",
|
381
|
+
# object_nl: "\n",
|
382
|
+
# indent+: ' ',
|
383
|
+
# space_before: ' ',
|
384
|
+
# space: ' '
|
385
|
+
# }
|
386
|
+
# puts 'Open:', JSON.generate(obj, opts)
|
387
|
+
#
|
388
|
+
# Output:
|
389
|
+
# Compact:
|
390
|
+
# {"foo":["bar","baz"],"bat":{"bam":0,"bad":1}}
|
391
|
+
# Open:
|
392
|
+
# {
|
393
|
+
# "foo" : [
|
394
|
+
# "bar",
|
395
|
+
# "baz"
|
396
|
+
# ],
|
397
|
+
# "bat" : {
|
398
|
+
# "bam" : 0,
|
399
|
+
# "bad" : 1
|
400
|
+
# }
|
401
|
+
# }
|
402
|
+
#
|
403
|
+
# ---
|
404
|
+
#
|
405
|
+
# Raises an exception if any formatting option is not a \String.
|
406
|
+
#
|
407
|
+
# ====== Exceptions
|
408
|
+
#
|
409
|
+
# Raises an exception if +obj+ contains circular references:
|
410
|
+
# a = []; b = []; a.push(b); b.push(a)
|
411
|
+
# # Raises JSON::NestingError (nesting of 100 is too deep):
|
412
|
+
# JSON.generate(a)
|
413
|
+
#
|
208
414
|
def generate(obj, opts = nil)
|
209
415
|
if State === opts
|
210
416
|
state, opts = opts, nil
|
@@ -231,11 +437,16 @@ module JSON
|
|
231
437
|
module_function :unparse
|
232
438
|
# :startdoc:
|
233
439
|
|
234
|
-
#
|
235
|
-
#
|
440
|
+
# Arguments +obj+ and +opts+ here are the same as
|
441
|
+
# arguments +obj+ and +opts+ in JSON.generate.
|
442
|
+
#
|
443
|
+
# By default, generates \JSON data without checking
|
444
|
+
# for circular references in +obj+ (option +max_nesting+ set to +false+, disabled).
|
236
445
|
#
|
237
|
-
#
|
238
|
-
#
|
446
|
+
# Raises an exception if +obj+ contains circular references:
|
447
|
+
# a = []; b = []; a.push(b); b.push(a)
|
448
|
+
# # Raises SystemStackError (stack level too deep):
|
449
|
+
# JSON.fast_generate(a)
|
239
450
|
def fast_generate(obj, opts = nil)
|
240
451
|
if State === opts
|
241
452
|
state, opts = opts, nil
|
@@ -261,12 +472,36 @@ module JSON
|
|
261
472
|
module_function :fast_unparse
|
262
473
|
# :startdoc:
|
263
474
|
|
264
|
-
#
|
265
|
-
#
|
266
|
-
#
|
475
|
+
# :call-seq:
|
476
|
+
# JSON.pretty_generate(obj, opts = nil) -> new_string
|
477
|
+
#
|
478
|
+
# Arguments +obj+ and +opts+ here are the same as
|
479
|
+
# arguments +obj+ and +opts+ in JSON.generate.
|
480
|
+
#
|
481
|
+
# Default options are:
|
482
|
+
# {
|
483
|
+
# indent: ' ', # Two spaces
|
484
|
+
# space: ' ', # One space
|
485
|
+
# array_nl: "\n", # Newline
|
486
|
+
# object_nl: "\n" # Newline
|
487
|
+
# }
|
488
|
+
#
|
489
|
+
# Example:
|
490
|
+
# obj = {foo: [:bar, :baz], bat: {bam: 0, bad: 1}}
|
491
|
+
# json = JSON.pretty_generate(obj)
|
492
|
+
# puts json
|
493
|
+
# Output:
|
494
|
+
# {
|
495
|
+
# "foo": [
|
496
|
+
# "bar",
|
497
|
+
# "baz"
|
498
|
+
# ],
|
499
|
+
# "bat": {
|
500
|
+
# "bam": 0,
|
501
|
+
# "bad": 1
|
502
|
+
# }
|
503
|
+
# }
|
267
504
|
#
|
268
|
-
# The _opts_ argument can be used to configure the generator. See the
|
269
|
-
# generate method for a more detailed explanation.
|
270
505
|
def pretty_generate(obj, opts = nil)
|
271
506
|
if State === opts
|
272
507
|
state, opts = opts, nil
|
@@ -293,10 +528,10 @@ module JSON
|
|
293
528
|
# :startdoc:
|
294
529
|
|
295
530
|
class << self
|
296
|
-
#
|
297
|
-
#
|
298
|
-
#
|
299
|
-
#
|
531
|
+
# Sets or returns default options for the JSON.load method.
|
532
|
+
# Initially:
|
533
|
+
# opts = JSON.load_default_options
|
534
|
+
# opts # => {:max_nesting=>false, :allow_nan=>true, :allow_blank=>true, :create_additions=>true}
|
300
535
|
attr_accessor :load_default_options
|
301
536
|
end
|
302
537
|
self.load_default_options = {
|
@@ -355,10 +590,10 @@ module JSON
|
|
355
590
|
module_function :restore
|
356
591
|
|
357
592
|
class << self
|
358
|
-
#
|
359
|
-
#
|
360
|
-
#
|
361
|
-
#
|
593
|
+
# Sets or returns the default options for the JSON.dump method.
|
594
|
+
# Initially:
|
595
|
+
# opts = JSON.dump_default_options
|
596
|
+
# opts # => {:max_nesting=>false, :allow_nan=>true}
|
362
597
|
attr_accessor :dump_default_options
|
363
598
|
end
|
364
599
|
self.dump_default_options = {
|
@@ -402,7 +637,7 @@ module JSON
|
|
402
637
|
raise ArgumentError, "exceed depth limit"
|
403
638
|
end
|
404
639
|
|
405
|
-
# Encodes string using
|
640
|
+
# Encodes string using String.encode.
|
406
641
|
def self.iconv(to, from, string)
|
407
642
|
string.encode(to, from)
|
408
643
|
end
|
data/lib/json/pure/generator.rb
CHANGED
@@ -405,7 +405,7 @@ module JSON
|
|
405
405
|
end
|
406
406
|
end
|
407
407
|
|
408
|
-
# Module that holds the
|
408
|
+
# Module that holds the extending methods if, the String module is
|
409
409
|
# included.
|
410
410
|
module Extend
|
411
411
|
# Raw Strings are JSON Objects (the raw bytes are stored in an
|
data/lib/json/pure/parser.rb
CHANGED
@@ -49,7 +49,7 @@ module JSON
|
|
49
49
|
)+
|
50
50
|
)mx
|
51
51
|
|
52
|
-
|
52
|
+
UNPARSED = Object.new.freeze
|
53
53
|
|
54
54
|
# Creates a new JSON::Pure::Parser instance for the string _source_.
|
55
55
|
#
|
@@ -66,7 +66,7 @@ module JSON
|
|
66
66
|
# also the default. It's not possible to use this option in
|
67
67
|
# conjunction with the *create_additions* option.
|
68
68
|
# * *create_additions*: If set to true, the Parser creates
|
69
|
-
# additions when
|
69
|
+
# additions when a matching class and create_id are found. This
|
70
70
|
# option defaults to false.
|
71
71
|
# * *object_class*: Defaults to Hash
|
72
72
|
# * *array_class*: Defaults to Array
|
data/lib/json/version.rb
CHANGED
data/tests/json_fixtures_test.rb
CHANGED
@@ -3,7 +3,7 @@ require 'test_helper'
|
|
3
3
|
|
4
4
|
class JSONFixturesTest < Test::Unit::TestCase
|
5
5
|
def setup
|
6
|
-
fixtures = File.join(File.dirname(__FILE__), 'fixtures/{fail,pass}
|
6
|
+
fixtures = File.join(File.dirname(__FILE__), 'fixtures/{fail,pass}*.json')
|
7
7
|
passed, failed = Dir[fixtures].partition { |f| f['pass'] }
|
8
8
|
@passed = passed.inject([]) { |a, f| a << [ f, File.read(f) ] }.sort
|
9
9
|
@failed = failed.inject([]) { |a, f| a << [ f, File.read(f) ] }.sort
|
@@ -29,4 +29,9 @@ class JSONFixturesTest < Test::Unit::TestCase
|
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
32
|
+
|
33
|
+
def test_sanity
|
34
|
+
assert(@passed.size > 5)
|
35
|
+
assert(@failed.size > 20)
|
36
|
+
end
|
32
37
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.3.
|
4
|
+
version: 2.3.1
|
5
5
|
platform: java
|
6
6
|
authors:
|
7
7
|
- Daniel Luz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-06-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
requirement: !ruby/object:Gem::Requirement
|
@@ -17,8 +17,8 @@ dependencies:
|
|
17
17
|
- !ruby/object:Gem::Version
|
18
18
|
version: '0'
|
19
19
|
name: rake
|
20
|
-
prerelease: false
|
21
20
|
type: :development
|
21
|
+
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
@@ -27,17 +27,23 @@ dependencies:
|
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
requirement: !ruby/object:Gem::Requirement
|
29
29
|
requirements:
|
30
|
-
- - "
|
30
|
+
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '2.0'
|
33
|
+
- - "<"
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '4.0'
|
33
36
|
name: test-unit
|
34
|
-
prerelease: false
|
35
37
|
type: :development
|
38
|
+
prerelease: false
|
36
39
|
version_requirements: !ruby/object:Gem::Requirement
|
37
40
|
requirements:
|
38
|
-
- - "
|
41
|
+
- - ">="
|
39
42
|
- !ruby/object:Gem::Version
|
40
43
|
version: '2.0'
|
44
|
+
- - "<"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '4.0'
|
41
47
|
description: A JSON implementation as a JRuby extension.
|
42
48
|
email: dev+ruby@mernen.com
|
43
49
|
executables: []
|
@@ -61,8 +67,6 @@ files:
|
|
61
67
|
- lib/json/add/time.rb
|
62
68
|
- lib/json/common.rb
|
63
69
|
- lib/json/ext.rb
|
64
|
-
- lib/json/ext/generator.jar
|
65
|
-
- lib/json/ext/parser.jar
|
66
70
|
- lib/json/generic_object.rb
|
67
71
|
- lib/json/pure.rb
|
68
72
|
- lib/json/pure/generator.rb
|
@@ -128,8 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
132
|
- !ruby/object:Gem::Version
|
129
133
|
version: '0'
|
130
134
|
requirements: []
|
131
|
-
|
132
|
-
rubygems_version: 2.7.10
|
135
|
+
rubygems_version: 3.0.6
|
133
136
|
signing_key:
|
134
137
|
specification_version: 4
|
135
138
|
summary: JSON implementation for JRuby
|
data/lib/json/ext/generator.jar
DELETED
Binary file
|
data/lib/json/ext/parser.jar
DELETED
Binary file
|