json 2.21.2-java → 3.0.0.rc1-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/CHANGES.md +42 -1
- data/README.md +4 -84
- data/lib/json/common.rb +86 -402
- data/lib/json/ext/generator/state.rb +0 -27
- data/lib/json/ext/generator.jar +0 -0
- data/lib/json/ext/parser.jar +0 -0
- data/lib/json/ext.rb +0 -2
- data/lib/json/truffle_ruby/generator.rb +38 -80
- data/lib/json/version.rb +1 -1
- data/lib/json.rb +13 -265
- metadata +2 -18
- data/lib/json/add/bigdecimal.rb +0 -58
- data/lib/json/add/complex.rb +0 -51
- data/lib/json/add/core.rb +0 -13
- data/lib/json/add/date.rb +0 -54
- data/lib/json/add/date_time.rb +0 -67
- data/lib/json/add/exception.rb +0 -49
- data/lib/json/add/ostruct.rb +0 -54
- data/lib/json/add/range.rb +0 -54
- data/lib/json/add/rational.rb +0 -49
- data/lib/json/add/regexp.rb +0 -48
- data/lib/json/add/set.rb +0 -48
- data/lib/json/add/string.rb +0 -35
- data/lib/json/add/struct.rb +0 -52
- data/lib/json/add/symbol.rb +0 -52
- data/lib/json/add/time.rb +0 -52
- data/lib/json/generic_object.rb +0 -67
|
@@ -71,33 +71,6 @@ module JSON
|
|
|
71
71
|
end
|
|
72
72
|
|
|
73
73
|
alias_method :to_hash, :to_h
|
|
74
|
-
|
|
75
|
-
# call-seq: [](name)
|
|
76
|
-
#
|
|
77
|
-
# Returns the value returned by method +name+.
|
|
78
|
-
def [](name)
|
|
79
|
-
::JSON.deprecation_warning("JSON::State#[] is deprecated and will be removed in json 3.0.0")
|
|
80
|
-
|
|
81
|
-
if respond_to?(name)
|
|
82
|
-
__send__(name)
|
|
83
|
-
else
|
|
84
|
-
instance_variable_get("@#{name}") if
|
|
85
|
-
instance_variables.include?("@#{name}".to_sym) # avoid warning
|
|
86
|
-
end
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
# call-seq: []=(name, value)
|
|
90
|
-
#
|
|
91
|
-
# Sets the attribute name to value.
|
|
92
|
-
def []=(name, value)
|
|
93
|
-
::JSON.deprecation_warning("JSON::State#[]= is deprecated and will be removed in json 3.0.0")
|
|
94
|
-
|
|
95
|
-
if respond_to?(name_writer = "#{name}=")
|
|
96
|
-
__send__ name_writer, value
|
|
97
|
-
else
|
|
98
|
-
instance_variable_set "@#{name}", value
|
|
99
|
-
end
|
|
100
|
-
end
|
|
101
74
|
end
|
|
102
75
|
end
|
|
103
76
|
end
|
data/lib/json/ext/generator.jar
CHANGED
|
Binary file
|
data/lib/json/ext/parser.jar
CHANGED
|
Binary file
|
data/lib/json/ext.rb
CHANGED
|
@@ -167,7 +167,8 @@ module JSON
|
|
|
167
167
|
@strict = false
|
|
168
168
|
@max_nesting = 100
|
|
169
169
|
@sort_keys = false
|
|
170
|
-
|
|
170
|
+
@allow_duplicate_key = false
|
|
171
|
+
_configure(**opts) if opts
|
|
171
172
|
end
|
|
172
173
|
|
|
173
174
|
# This string is used to indent levels in the JSON text.
|
|
@@ -289,65 +290,48 @@ module JSON
|
|
|
289
290
|
|
|
290
291
|
# Configure this State instance with the Hash _opts_, and return
|
|
291
292
|
# itself.
|
|
292
|
-
def configure(
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
end
|
|
300
|
-
|
|
301
|
-
if opts[:depth]&.negative?
|
|
302
|
-
raise ArgumentError, "depth must be >= 0 (got #{opts[:depth]})"
|
|
303
|
-
end
|
|
304
|
-
|
|
305
|
-
opts.each do |key, value|
|
|
306
|
-
instance_variable_set "@#{key}", value
|
|
293
|
+
def configure(options)
|
|
294
|
+
unless Hash == options
|
|
295
|
+
if options.respond_to?(:to_hash)
|
|
296
|
+
options = options.to_hash
|
|
297
|
+
elsif options.respond_to?(:to_h)
|
|
298
|
+
options = options.to_h
|
|
299
|
+
end
|
|
307
300
|
end
|
|
301
|
+
_configure(**options)
|
|
302
|
+
end
|
|
303
|
+
alias merge configure
|
|
308
304
|
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
@as_json = opts[:as_json].to_proc if opts[:as_json]
|
|
317
|
-
@ascii_only = opts[:ascii_only] if opts.key?(:ascii_only)
|
|
318
|
-
self.sort_keys = opts[:sort_keys] if opts.key?(:sort_keys)
|
|
319
|
-
@depth = opts[:depth] || 0
|
|
320
|
-
@buffer_initial_length ||= opts[:buffer_initial_length]
|
|
321
|
-
|
|
322
|
-
@script_safe = if opts.key?(:script_safe)
|
|
323
|
-
!!opts[:script_safe]
|
|
324
|
-
elsif opts.key?(:escape_slash)
|
|
325
|
-
!!opts[:escape_slash]
|
|
326
|
-
else
|
|
327
|
-
false
|
|
305
|
+
private def _configure(
|
|
306
|
+
indent: '', space: '', space_before: '', object_nl: '', array_nl: '', allow_nan: false,
|
|
307
|
+
as_json: false, ascii_only: false, sort_keys: false, depth: 0, buffer_initial_length: 1024,
|
|
308
|
+
allow_duplicate_key: false, script_safe: false, strict: false, max_nesting: 100
|
|
309
|
+
)
|
|
310
|
+
if depth.negative?
|
|
311
|
+
raise ArgumentError, "depth must be >= 0 (got #{depth})"
|
|
328
312
|
end
|
|
329
313
|
|
|
330
|
-
if
|
|
331
|
-
|
|
332
|
-
else
|
|
333
|
-
@allow_duplicate_key = nil # nil is deprecation
|
|
314
|
+
if max_nesting && !(Integer === max_nesting)
|
|
315
|
+
raise TypeError, ":max_nesting must be `false` or an Integer, got: #{max_nesting.class}"
|
|
334
316
|
end
|
|
335
317
|
|
|
336
|
-
@
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
318
|
+
@indent = indent || ''
|
|
319
|
+
@space = space || ''
|
|
320
|
+
@space_before = space_before || ''
|
|
321
|
+
@object_nl = object_nl || ''
|
|
322
|
+
@array_nl = array_nl || ''
|
|
323
|
+
@allow_nan = allow_nan || false
|
|
324
|
+
@as_json = as_json || false
|
|
325
|
+
@ascii_only = ascii_only || false
|
|
326
|
+
self.sort_keys = sort_keys
|
|
327
|
+
@depth = depth
|
|
328
|
+
@buffer_initial_length = buffer_initial_length
|
|
329
|
+
@allow_duplicate_key = allow_duplicate_key
|
|
330
|
+
@script_safe = script_safe
|
|
331
|
+
@strict = strict
|
|
332
|
+
@max_nesting = max_nesting || 0
|
|
348
333
|
self
|
|
349
334
|
end
|
|
350
|
-
alias merge configure
|
|
351
335
|
|
|
352
336
|
def allow_duplicate_key? # :nodoc:
|
|
353
337
|
@allow_duplicate_key
|
|
@@ -362,10 +346,6 @@ module JSON
|
|
|
362
346
|
result[key.to_sym] = instance_variable_get(iv)
|
|
363
347
|
end
|
|
364
348
|
|
|
365
|
-
if result[:allow_duplicate_key].nil?
|
|
366
|
-
result.delete(:allow_duplicate_key)
|
|
367
|
-
end
|
|
368
|
-
|
|
369
349
|
result
|
|
370
350
|
end
|
|
371
351
|
|
|
@@ -416,7 +396,7 @@ module JSON
|
|
|
416
396
|
else
|
|
417
397
|
if key_type && !@allow_duplicate_key && key_type != k.class
|
|
418
398
|
key_type = nil # stop checking
|
|
419
|
-
JSON.send(:on_mixed_keys_hash, obj
|
|
399
|
+
JSON.send(:on_mixed_keys_hash, obj)
|
|
420
400
|
end
|
|
421
401
|
buf << ','
|
|
422
402
|
end
|
|
@@ -483,28 +463,6 @@ module JSON
|
|
|
483
463
|
end
|
|
484
464
|
buf << '"'
|
|
485
465
|
end
|
|
486
|
-
|
|
487
|
-
# Return the value returned by method +name+.
|
|
488
|
-
def [](name)
|
|
489
|
-
::JSON.deprecation_warning("JSON::State#[] is deprecated and will be removed in json 3.0.0")
|
|
490
|
-
|
|
491
|
-
if respond_to?(name)
|
|
492
|
-
__send__(name)
|
|
493
|
-
else
|
|
494
|
-
instance_variable_get("@#{name}") if
|
|
495
|
-
instance_variables.include?("@#{name}".to_sym) # avoid warning
|
|
496
|
-
end
|
|
497
|
-
end
|
|
498
|
-
|
|
499
|
-
def []=(name, value)
|
|
500
|
-
::JSON.deprecation_warning("JSON::State#[]= is deprecated and will be removed in json 3.0.0")
|
|
501
|
-
|
|
502
|
-
if respond_to?(name_writer = "#{name}=")
|
|
503
|
-
__send__ name_writer, value
|
|
504
|
-
else
|
|
505
|
-
instance_variable_set "@#{name}", value
|
|
506
|
-
end
|
|
507
|
-
end
|
|
508
466
|
end
|
|
509
467
|
|
|
510
468
|
module GeneratorMethods
|
|
@@ -569,7 +527,7 @@ module JSON
|
|
|
569
527
|
else
|
|
570
528
|
if key_type && !state.allow_duplicate_key? && key_type != key.class
|
|
571
529
|
key_type = nil # stop checking
|
|
572
|
-
JSON.send(:on_mixed_keys_hash, self
|
|
530
|
+
JSON.send(:on_mixed_keys_hash, self)
|
|
573
531
|
end
|
|
574
532
|
result << delim
|
|
575
533
|
end
|
data/lib/json/version.rb
CHANGED
data/lib/json.rb
CHANGED
|
@@ -141,18 +141,12 @@ require 'json/common'
|
|
|
141
141
|
# Option +allow_duplicate_key+ specifies whether duplicate keys in objects
|
|
142
142
|
# should be ignored or cause an error to be raised:
|
|
143
143
|
#
|
|
144
|
-
# When
|
|
145
|
-
#
|
|
146
|
-
# JSON.parse('{"a": 1, "a":2}') => {"a" => 2}
|
|
147
|
-
# # warning: detected duplicate keys in JSON object.
|
|
148
|
-
# # This will raise an error in json 3.0 unless enabled via `allow_duplicate_key: true`
|
|
144
|
+
# When set to +false+, the default:
|
|
145
|
+
# JSON.parse('{"a": 1, "a":2}') => duplicate key at line 1 column 1 (JSON::ParserError)
|
|
149
146
|
#
|
|
150
147
|
# When set to +true+:
|
|
151
148
|
# # The last value is used.
|
|
152
|
-
# JSON.parse('{"a": 1, "a":2}') => {"a" => 2}
|
|
153
|
-
#
|
|
154
|
-
# When set to +false+, the future default:
|
|
155
|
-
# JSON.parse('{"a": 1, "a":2}') => duplicate key at line 1 column 1 (JSON::ParserError)
|
|
149
|
+
# JSON.parse('{"a": 1, "a":2}', allow_duplicate_key: true) => {"a" => 2}
|
|
156
150
|
#
|
|
157
151
|
# ---
|
|
158
152
|
#
|
|
@@ -190,13 +184,11 @@ require 'json/common'
|
|
|
190
184
|
# JavaScript style comments (either <tt>// comment</tt> or <tt>/* comment */</tt>);
|
|
191
185
|
# defaults to +false+.
|
|
192
186
|
#
|
|
193
|
-
# When
|
|
187
|
+
# When set to +false+, the default:
|
|
188
|
+
# JSON.parse('/* comment */ {"a": 1, "a":2}') # unexpected character: '/' at line 1 column 1 (JSON::ParserError)
|
|
194
189
|
#
|
|
195
190
|
# When set to +true+, comments are ignored:
|
|
196
|
-
# JSON.parse('/* comment */ {"a": 1, "a":2}') # => {"a" => 2}
|
|
197
|
-
#
|
|
198
|
-
# When set to +false+, the future default:
|
|
199
|
-
# JSON.parse('/* comment */ {"a": 1, "a":2}') # unexpected character: '/' at line 1 column 1 (JSON::ParserError)
|
|
191
|
+
# JSON.parse('/* comment */ {"a": 1, "a":2} // more comment') # => {"a" => 2}
|
|
200
192
|
#
|
|
201
193
|
# ---
|
|
202
194
|
#
|
|
@@ -267,11 +259,6 @@ require 'json/common'
|
|
|
267
259
|
# ruby = JSON.parse(source, {array_class: Set})
|
|
268
260
|
# ruby # => #<Set: {"foo", 1.0, true, false, nil}>
|
|
269
261
|
#
|
|
270
|
-
# ---
|
|
271
|
-
#
|
|
272
|
-
# Option +create_additions+ (boolean) specifies whether to use \JSON additions in parsing.
|
|
273
|
-
# See {\JSON Additions}[#module-JSON-label-JSON+Additions].
|
|
274
|
-
#
|
|
275
262
|
# === Generating \JSON
|
|
276
263
|
#
|
|
277
264
|
# To generate a Ruby \String containing \JSON data,
|
|
@@ -360,18 +347,13 @@ require 'json/common'
|
|
|
360
347
|
# hashes with duplicate keys should be allowed or produce an error.
|
|
361
348
|
# defaults to emit a deprecation warning.
|
|
362
349
|
#
|
|
363
|
-
# With the default,
|
|
364
|
-
# Warning[:deprecated] = true
|
|
350
|
+
# With the default, <tt>false</tt>:
|
|
365
351
|
# JSON.generate({ foo: 1, "foo" => 2 })
|
|
366
|
-
# # warning: detected duplicate key "foo" in {foo: 1, "foo" => 2}.
|
|
367
|
-
# # This will raise an error in json 3.0 unless enabled via `allow_duplicate_key: true`
|
|
368
|
-
# # => '{"foo":1,"foo":2}'
|
|
369
|
-
#
|
|
370
|
-
# With <tt>false</tt>
|
|
371
|
-
# JSON.generate({ foo: 1, "foo" => 2 }, allow_duplicate_key: false)
|
|
372
352
|
# # detected duplicate key "foo" in {foo: 1, "foo" => 2} (JSON::GeneratorError)
|
|
373
353
|
#
|
|
374
|
-
#
|
|
354
|
+
# With <tt>true</tt>
|
|
355
|
+
# JSON.generate({ foo: 1, "foo" => 2 }, allow_duplicate_key: true)
|
|
356
|
+
# # => '{"foo":1,"foo":2}'
|
|
375
357
|
#
|
|
376
358
|
# ---
|
|
377
359
|
#
|
|
@@ -392,8 +374,9 @@ require 'json/common'
|
|
|
392
374
|
# # Raises SystemStackError: stack level too deep
|
|
393
375
|
# JSON.generate(obj, max_nesting: false)
|
|
394
376
|
#
|
|
395
|
-
# Setting +max_nesting+ to +false+ can lead to a
|
|
396
|
-
# in an unrecoverable state.
|
|
377
|
+
# Setting +max_nesting+ to +false+ or a very large number can lead to a stack overflow
|
|
378
|
+
# which may leave the process in an unrecoverable state.
|
|
379
|
+
# It is highly discouraged.
|
|
397
380
|
#
|
|
398
381
|
# ====== Escaping Options
|
|
399
382
|
#
|
|
@@ -463,241 +446,6 @@ require 'json/common'
|
|
|
463
446
|
# }
|
|
464
447
|
# }
|
|
465
448
|
#
|
|
466
|
-
# == \JSON Additions
|
|
467
|
-
#
|
|
468
|
-
# Note that JSON Additions must only be used with trusted data, and is
|
|
469
|
-
# deprecated.
|
|
470
|
-
#
|
|
471
|
-
# When you "round trip" a non-\String object from Ruby to \JSON and back,
|
|
472
|
-
# you have a new \String, instead of the object you began with:
|
|
473
|
-
# ruby0 = Range.new(0, 2)
|
|
474
|
-
# json = JSON.generate(ruby0)
|
|
475
|
-
# json # => '0..2"'
|
|
476
|
-
# ruby1 = JSON.parse(json)
|
|
477
|
-
# ruby1 # => '0..2'
|
|
478
|
-
# ruby1.class # => String
|
|
479
|
-
#
|
|
480
|
-
# You can use \JSON _additions_ to preserve the original object.
|
|
481
|
-
# The addition is an extension of a ruby class, so that:
|
|
482
|
-
# - \JSON.generate stores more information in the \JSON string.
|
|
483
|
-
# - \JSON.parse, called with option +create_additions+,
|
|
484
|
-
# uses that information to create a proper Ruby object.
|
|
485
|
-
#
|
|
486
|
-
# This example shows a \Range being generated into \JSON
|
|
487
|
-
# and parsed back into Ruby, both without and with
|
|
488
|
-
# the addition for \Range:
|
|
489
|
-
# ruby = Range.new(0, 2)
|
|
490
|
-
# # This passage does not use the addition for Range.
|
|
491
|
-
# json0 = JSON.generate(ruby)
|
|
492
|
-
# ruby0 = JSON.parse(json0)
|
|
493
|
-
# # This passage uses the addition for Range.
|
|
494
|
-
# require 'json/add/range'
|
|
495
|
-
# json1 = JSON.generate(ruby)
|
|
496
|
-
# ruby1 = JSON.parse(json1, create_additions: true)
|
|
497
|
-
# # Make a nice display.
|
|
498
|
-
# display = <<~EOT
|
|
499
|
-
# Generated JSON:
|
|
500
|
-
# Without addition: #{json0} (#{json0.class})
|
|
501
|
-
# With addition: #{json1} (#{json1.class})
|
|
502
|
-
# Parsed JSON:
|
|
503
|
-
# Without addition: #{ruby0.inspect} (#{ruby0.class})
|
|
504
|
-
# With addition: #{ruby1.inspect} (#{ruby1.class})
|
|
505
|
-
# EOT
|
|
506
|
-
# puts display
|
|
507
|
-
#
|
|
508
|
-
# This output shows the different results:
|
|
509
|
-
# Generated JSON:
|
|
510
|
-
# Without addition: "0..2" (String)
|
|
511
|
-
# With addition: {"json_class":"Range","a":[0,2,false]} (String)
|
|
512
|
-
# Parsed JSON:
|
|
513
|
-
# Without addition: "0..2" (String)
|
|
514
|
-
# With addition: 0..2 (Range)
|
|
515
|
-
#
|
|
516
|
-
# The \JSON module includes additions for certain classes.
|
|
517
|
-
# You can also craft custom additions.
|
|
518
|
-
# See {Custom \JSON Additions}[#module-JSON-label-Custom+JSON+Additions].
|
|
519
|
-
#
|
|
520
|
-
# === Built-in Additions
|
|
521
|
-
#
|
|
522
|
-
# The \JSON module includes additions for certain classes.
|
|
523
|
-
# To use an addition, +require+ its source:
|
|
524
|
-
# - BigDecimal: <tt>require 'json/add/bigdecimal'</tt>
|
|
525
|
-
# - Complex: <tt>require 'json/add/complex'</tt>
|
|
526
|
-
# - Date: <tt>require 'json/add/date'</tt>
|
|
527
|
-
# - DateTime: <tt>require 'json/add/date_time'</tt>
|
|
528
|
-
# - Exception: <tt>require 'json/add/exception'</tt>
|
|
529
|
-
# - OpenStruct: <tt>require 'json/add/ostruct'</tt>
|
|
530
|
-
# - Range: <tt>require 'json/add/range'</tt>
|
|
531
|
-
# - Rational: <tt>require 'json/add/rational'</tt>
|
|
532
|
-
# - Regexp: <tt>require 'json/add/regexp'</tt>
|
|
533
|
-
# - Set: <tt>require 'json/add/set'</tt>
|
|
534
|
-
# - Struct: <tt>require 'json/add/struct'</tt>
|
|
535
|
-
# - Symbol: <tt>require 'json/add/symbol'</tt>
|
|
536
|
-
# - Time: <tt>require 'json/add/time'</tt>
|
|
537
|
-
#
|
|
538
|
-
# To reduce punctuation clutter, the examples below
|
|
539
|
-
# show the generated \JSON via +puts+, rather than the usual +inspect+,
|
|
540
|
-
#
|
|
541
|
-
# \BigDecimal:
|
|
542
|
-
# require 'json/add/bigdecimal'
|
|
543
|
-
# ruby0 = BigDecimal(0) # 0.0
|
|
544
|
-
# json = JSON.generate(ruby0) # {"json_class":"BigDecimal","b":"27:0.0"}
|
|
545
|
-
# ruby1 = JSON.parse(json, create_additions: true) # 0.0
|
|
546
|
-
# ruby1.class # => BigDecimal
|
|
547
|
-
#
|
|
548
|
-
# \Complex:
|
|
549
|
-
# require 'json/add/complex'
|
|
550
|
-
# ruby0 = Complex(1+0i) # 1+0i
|
|
551
|
-
# json = JSON.generate(ruby0) # {"json_class":"Complex","r":1,"i":0}
|
|
552
|
-
# ruby1 = JSON.parse(json, create_additions: true) # 1+0i
|
|
553
|
-
# ruby1.class # Complex
|
|
554
|
-
#
|
|
555
|
-
# \Date:
|
|
556
|
-
# require 'json/add/date'
|
|
557
|
-
# ruby0 = Date.today # 2020-05-02
|
|
558
|
-
# json = JSON.generate(ruby0) # {"json_class":"Date","y":2020,"m":5,"d":2,"sg":2299161.0}
|
|
559
|
-
# ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02
|
|
560
|
-
# ruby1.class # Date
|
|
561
|
-
#
|
|
562
|
-
# \DateTime:
|
|
563
|
-
# require 'json/add/date_time'
|
|
564
|
-
# ruby0 = DateTime.now # 2020-05-02T10:38:13-05:00
|
|
565
|
-
# 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}
|
|
566
|
-
# ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02T10:38:13-05:00
|
|
567
|
-
# ruby1.class # DateTime
|
|
568
|
-
#
|
|
569
|
-
# \Exception (and its subclasses including \RuntimeError):
|
|
570
|
-
# require 'json/add/exception'
|
|
571
|
-
# ruby0 = Exception.new('A message') # A message
|
|
572
|
-
# json = JSON.generate(ruby0) # {"json_class":"Exception","m":"A message","b":null}
|
|
573
|
-
# ruby1 = JSON.parse(json, create_additions: true) # A message
|
|
574
|
-
# ruby1.class # Exception
|
|
575
|
-
# ruby0 = RuntimeError.new('Another message') # Another message
|
|
576
|
-
# json = JSON.generate(ruby0) # {"json_class":"RuntimeError","m":"Another message","b":null}
|
|
577
|
-
# ruby1 = JSON.parse(json, create_additions: true) # Another message
|
|
578
|
-
# ruby1.class # RuntimeError
|
|
579
|
-
#
|
|
580
|
-
# \OpenStruct:
|
|
581
|
-
# require 'json/add/ostruct'
|
|
582
|
-
# ruby0 = OpenStruct.new(name: 'Matz', language: 'Ruby') # #<OpenStruct name="Matz", language="Ruby">
|
|
583
|
-
# json = JSON.generate(ruby0) # {"json_class":"OpenStruct","t":{"name":"Matz","language":"Ruby"}}
|
|
584
|
-
# ruby1 = JSON.parse(json, create_additions: true) # #<OpenStruct name="Matz", language="Ruby">
|
|
585
|
-
# ruby1.class # OpenStruct
|
|
586
|
-
#
|
|
587
|
-
# \Range:
|
|
588
|
-
# require 'json/add/range'
|
|
589
|
-
# ruby0 = Range.new(0, 2) # 0..2
|
|
590
|
-
# json = JSON.generate(ruby0) # {"json_class":"Range","a":[0,2,false]}
|
|
591
|
-
# ruby1 = JSON.parse(json, create_additions: true) # 0..2
|
|
592
|
-
# ruby1.class # Range
|
|
593
|
-
#
|
|
594
|
-
# \Rational:
|
|
595
|
-
# require 'json/add/rational'
|
|
596
|
-
# ruby0 = Rational(1, 3) # 1/3
|
|
597
|
-
# json = JSON.generate(ruby0) # {"json_class":"Rational","n":1,"d":3}
|
|
598
|
-
# ruby1 = JSON.parse(json, create_additions: true) # 1/3
|
|
599
|
-
# ruby1.class # Rational
|
|
600
|
-
#
|
|
601
|
-
# \Regexp:
|
|
602
|
-
# require 'json/add/regexp'
|
|
603
|
-
# ruby0 = Regexp.new('foo') # (?-mix:foo)
|
|
604
|
-
# json = JSON.generate(ruby0) # {"json_class":"Regexp","o":0,"s":"foo"}
|
|
605
|
-
# ruby1 = JSON.parse(json, create_additions: true) # (?-mix:foo)
|
|
606
|
-
# ruby1.class # Regexp
|
|
607
|
-
#
|
|
608
|
-
# \Set:
|
|
609
|
-
# require 'json/add/set'
|
|
610
|
-
# ruby0 = Set.new([0, 1, 2]) # #<Set: {0, 1, 2}>
|
|
611
|
-
# json = JSON.generate(ruby0) # {"json_class":"Set","a":[0,1,2]}
|
|
612
|
-
# ruby1 = JSON.parse(json, create_additions: true) # #<Set: {0, 1, 2}>
|
|
613
|
-
# ruby1.class # Set
|
|
614
|
-
#
|
|
615
|
-
# \Struct:
|
|
616
|
-
# require 'json/add/struct'
|
|
617
|
-
# Customer = Struct.new(:name, :address) # Customer
|
|
618
|
-
# ruby0 = Customer.new("Dave", "123 Main") # #<struct Customer name="Dave", address="123 Main">
|
|
619
|
-
# json = JSON.generate(ruby0) # {"json_class":"Customer","v":["Dave","123 Main"]}
|
|
620
|
-
# ruby1 = JSON.parse(json, create_additions: true) # #<struct Customer name="Dave", address="123 Main">
|
|
621
|
-
# ruby1.class # Customer
|
|
622
|
-
#
|
|
623
|
-
# \Symbol:
|
|
624
|
-
# require 'json/add/symbol'
|
|
625
|
-
# ruby0 = :foo # foo
|
|
626
|
-
# json = JSON.generate(ruby0) # {"json_class":"Symbol","s":"foo"}
|
|
627
|
-
# ruby1 = JSON.parse(json, create_additions: true) # foo
|
|
628
|
-
# ruby1.class # Symbol
|
|
629
|
-
#
|
|
630
|
-
# \Time:
|
|
631
|
-
# require 'json/add/time'
|
|
632
|
-
# ruby0 = Time.now # 2020-05-02 11:28:26 -0500
|
|
633
|
-
# json = JSON.generate(ruby0) # {"json_class":"Time","s":1588436906,"n":840560000}
|
|
634
|
-
# ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02 11:28:26 -0500
|
|
635
|
-
# ruby1.class # Time
|
|
636
|
-
#
|
|
637
|
-
#
|
|
638
|
-
# === Custom \JSON Additions
|
|
639
|
-
#
|
|
640
|
-
# In addition to the \JSON additions provided,
|
|
641
|
-
# you can craft \JSON additions of your own,
|
|
642
|
-
# either for Ruby built-in classes or for user-defined classes.
|
|
643
|
-
#
|
|
644
|
-
# Here's a user-defined class +Foo+:
|
|
645
|
-
# class Foo
|
|
646
|
-
# attr_accessor :bar, :baz
|
|
647
|
-
# def initialize(bar, baz)
|
|
648
|
-
# self.bar = bar
|
|
649
|
-
# self.baz = baz
|
|
650
|
-
# end
|
|
651
|
-
# end
|
|
652
|
-
#
|
|
653
|
-
# Here's the \JSON addition for it:
|
|
654
|
-
# # Extend class Foo with JSON addition.
|
|
655
|
-
# class Foo
|
|
656
|
-
# # Serialize Foo object with its class name and arguments
|
|
657
|
-
# def to_json(*args)
|
|
658
|
-
# {
|
|
659
|
-
# JSON.create_id => self.class.name,
|
|
660
|
-
# 'a' => [ bar, baz ]
|
|
661
|
-
# }.to_json(*args)
|
|
662
|
-
# end
|
|
663
|
-
# # Deserialize JSON string by constructing new Foo object with arguments.
|
|
664
|
-
# def self.json_create(object)
|
|
665
|
-
# new(*object['a'])
|
|
666
|
-
# end
|
|
667
|
-
# end
|
|
668
|
-
#
|
|
669
|
-
# Demonstration:
|
|
670
|
-
# require 'json'
|
|
671
|
-
# # This Foo object has no custom addition.
|
|
672
|
-
# foo0 = Foo.new(0, 1)
|
|
673
|
-
# json0 = JSON.generate(foo0)
|
|
674
|
-
# obj0 = JSON.parse(json0)
|
|
675
|
-
# # Lood the custom addition.
|
|
676
|
-
# require_relative 'foo_addition'
|
|
677
|
-
# # This foo has the custom addition.
|
|
678
|
-
# foo1 = Foo.new(0, 1)
|
|
679
|
-
# json1 = JSON.generate(foo1)
|
|
680
|
-
# obj1 = JSON.parse(json1, create_additions: true)
|
|
681
|
-
# # Make a nice display.
|
|
682
|
-
# display = <<~EOT
|
|
683
|
-
# Generated JSON:
|
|
684
|
-
# Without custom addition: #{json0} (#{json0.class})
|
|
685
|
-
# With custom addition: #{json1} (#{json1.class})
|
|
686
|
-
# Parsed JSON:
|
|
687
|
-
# Without custom addition: #{obj0.inspect} (#{obj0.class})
|
|
688
|
-
# With custom addition: #{obj1.inspect} (#{obj1.class})
|
|
689
|
-
# EOT
|
|
690
|
-
# puts display
|
|
691
|
-
#
|
|
692
|
-
# Output:
|
|
693
|
-
#
|
|
694
|
-
# Generated JSON:
|
|
695
|
-
# Without custom addition: "#<Foo:0x0000000006534e80>" (String)
|
|
696
|
-
# With custom addition: {"json_class":"Foo","a":[0,1]} (String)
|
|
697
|
-
# Parsed JSON:
|
|
698
|
-
# Without custom addition: "#<Foo:0x0000000006534e80>" (String)
|
|
699
|
-
# With custom addition: #<Foo:0x0000000006473bb8 @bar=0, @baz=1> (Foo)
|
|
700
|
-
#
|
|
701
449
|
module JSON
|
|
702
450
|
require 'json/version'
|
|
703
451
|
require 'json/ext'
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: json
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 3.0.0.rc1
|
|
5
5
|
platform: java
|
|
6
6
|
authors:
|
|
7
7
|
- Daniel Luz
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date: 2026-
|
|
10
|
+
date: 2026-08-11 00:00:00.000000000 Z
|
|
11
11
|
dependencies: []
|
|
12
12
|
description: A JSON implementation as a JRuby extension.
|
|
13
13
|
email: dev+ruby@mernen.com
|
|
@@ -23,27 +23,11 @@ files:
|
|
|
23
23
|
- README.md
|
|
24
24
|
- json.gemspec
|
|
25
25
|
- lib/json.rb
|
|
26
|
-
- lib/json/add/bigdecimal.rb
|
|
27
|
-
- lib/json/add/complex.rb
|
|
28
|
-
- lib/json/add/core.rb
|
|
29
|
-
- lib/json/add/date.rb
|
|
30
|
-
- lib/json/add/date_time.rb
|
|
31
|
-
- lib/json/add/exception.rb
|
|
32
|
-
- lib/json/add/ostruct.rb
|
|
33
|
-
- lib/json/add/range.rb
|
|
34
|
-
- lib/json/add/rational.rb
|
|
35
|
-
- lib/json/add/regexp.rb
|
|
36
|
-
- lib/json/add/set.rb
|
|
37
|
-
- lib/json/add/string.rb
|
|
38
|
-
- lib/json/add/struct.rb
|
|
39
|
-
- lib/json/add/symbol.rb
|
|
40
|
-
- lib/json/add/time.rb
|
|
41
26
|
- lib/json/common.rb
|
|
42
27
|
- lib/json/ext.rb
|
|
43
28
|
- lib/json/ext/generator.jar
|
|
44
29
|
- lib/json/ext/generator/state.rb
|
|
45
30
|
- lib/json/ext/parser.jar
|
|
46
|
-
- lib/json/generic_object.rb
|
|
47
31
|
- lib/json/truffle_ruby/generator.rb
|
|
48
32
|
- lib/json/version.rb
|
|
49
33
|
homepage: https://github.com/ruby/json
|
data/lib/json/add/bigdecimal.rb
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
|
3
|
-
require 'json'
|
|
4
|
-
end
|
|
5
|
-
begin
|
|
6
|
-
require 'bigdecimal'
|
|
7
|
-
rescue LoadError
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
class BigDecimal
|
|
11
|
-
|
|
12
|
-
# See #as_json.
|
|
13
|
-
def self.json_create(object)
|
|
14
|
-
BigDecimal._load object['b']
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
# Methods <tt>BigDecimal#as_json</tt> and +BigDecimal.json_create+ may be used
|
|
18
|
-
# to serialize and deserialize a \BigDecimal object;
|
|
19
|
-
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
|
20
|
-
#
|
|
21
|
-
# \Method <tt>BigDecimal#as_json</tt> serializes +self+,
|
|
22
|
-
# returning a 2-element hash representing +self+:
|
|
23
|
-
#
|
|
24
|
-
# require 'json/add/bigdecimal'
|
|
25
|
-
# x = BigDecimal(2).as_json # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"}
|
|
26
|
-
# y = BigDecimal(2.0, 4).as_json # => {"json_class"=>"BigDecimal", "b"=>"36:0.2e1"}
|
|
27
|
-
# z = BigDecimal(Complex(2, 0)).as_json # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"}
|
|
28
|
-
#
|
|
29
|
-
# \Method +JSON.create+ deserializes such a hash, returning a \BigDecimal object:
|
|
30
|
-
#
|
|
31
|
-
# BigDecimal.json_create(x) # => 0.2e1
|
|
32
|
-
# BigDecimal.json_create(y) # => 0.2e1
|
|
33
|
-
# BigDecimal.json_create(z) # => 0.2e1
|
|
34
|
-
#
|
|
35
|
-
def as_json(*)
|
|
36
|
-
{
|
|
37
|
-
JSON.create_id => self.class.name,
|
|
38
|
-
'b' => _dump.force_encoding(Encoding::UTF_8),
|
|
39
|
-
}
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
# Returns a JSON string representing +self+:
|
|
43
|
-
#
|
|
44
|
-
# require 'json/add/bigdecimal'
|
|
45
|
-
# puts BigDecimal(2).to_json
|
|
46
|
-
# puts BigDecimal(2.0, 4).to_json
|
|
47
|
-
# puts BigDecimal(Complex(2, 0)).to_json
|
|
48
|
-
#
|
|
49
|
-
# Output:
|
|
50
|
-
#
|
|
51
|
-
# {"json_class":"BigDecimal","b":"27:0.2e1"}
|
|
52
|
-
# {"json_class":"BigDecimal","b":"36:0.2e1"}
|
|
53
|
-
# {"json_class":"BigDecimal","b":"27:0.2e1"}
|
|
54
|
-
#
|
|
55
|
-
def to_json(*args)
|
|
56
|
-
as_json.to_json(*args)
|
|
57
|
-
end
|
|
58
|
-
end if defined?(::BigDecimal)
|