json 2.21.1-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 +45 -0
- 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 +24 -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
|
@@ -121,9 +121,11 @@ require 'json/common'
|
|
|
121
121
|
# ====== Input Options
|
|
122
122
|
#
|
|
123
123
|
# Option +max_nesting+ (\Integer) specifies the maximum nesting depth allowed;
|
|
124
|
-
# defaults to +100+;
|
|
124
|
+
# defaults to +100+;
|
|
125
|
+
# You can set it to +false+ to disable depth checking entirely, but that is dangerous
|
|
126
|
+
# when parsing untrusted input.
|
|
125
127
|
#
|
|
126
|
-
# With the default, +
|
|
128
|
+
# With the default, +100+:
|
|
127
129
|
# source = '[0, [1, [2, [3]]]]'
|
|
128
130
|
# ruby = JSON.parse(source)
|
|
129
131
|
# ruby # => [0, [1, [2, [3]]]]
|
|
@@ -139,18 +141,12 @@ require 'json/common'
|
|
|
139
141
|
# Option +allow_duplicate_key+ specifies whether duplicate keys in objects
|
|
140
142
|
# should be ignored or cause an error to be raised:
|
|
141
143
|
#
|
|
142
|
-
# When
|
|
143
|
-
#
|
|
144
|
-
# JSON.parse('{"a": 1, "a":2}') => {"a" => 2}
|
|
145
|
-
# # warning: detected duplicate keys in JSON object.
|
|
146
|
-
# # 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)
|
|
147
146
|
#
|
|
148
147
|
# When set to +true+:
|
|
149
148
|
# # The last value is used.
|
|
150
|
-
# JSON.parse('{"a": 1, "a":2}') => {"a" => 2}
|
|
151
|
-
#
|
|
152
|
-
# When set to +false+, the future default:
|
|
153
|
-
# 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}
|
|
154
150
|
#
|
|
155
151
|
# ---
|
|
156
152
|
#
|
|
@@ -188,13 +184,11 @@ require 'json/common'
|
|
|
188
184
|
# JavaScript style comments (either <tt>// comment</tt> or <tt>/* comment */</tt>);
|
|
189
185
|
# defaults to +false+.
|
|
190
186
|
#
|
|
191
|
-
# 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)
|
|
192
189
|
#
|
|
193
190
|
# When set to +true+, comments are ignored:
|
|
194
|
-
# JSON.parse('/* comment */ {"a": 1, "a":2}') # => {"a" => 2}
|
|
195
|
-
#
|
|
196
|
-
# When set to +false+, the future default:
|
|
197
|
-
# 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}
|
|
198
192
|
#
|
|
199
193
|
# ---
|
|
200
194
|
#
|
|
@@ -265,11 +259,6 @@ require 'json/common'
|
|
|
265
259
|
# ruby = JSON.parse(source, {array_class: Set})
|
|
266
260
|
# ruby # => #<Set: {"foo", 1.0, true, false, nil}>
|
|
267
261
|
#
|
|
268
|
-
# ---
|
|
269
|
-
#
|
|
270
|
-
# Option +create_additions+ (boolean) specifies whether to use \JSON additions in parsing.
|
|
271
|
-
# See {\JSON Additions}[#module-JSON-label-JSON+Additions].
|
|
272
|
-
#
|
|
273
262
|
# === Generating \JSON
|
|
274
263
|
#
|
|
275
264
|
# To generate a Ruby \String containing \JSON data,
|
|
@@ -358,18 +347,13 @@ require 'json/common'
|
|
|
358
347
|
# hashes with duplicate keys should be allowed or produce an error.
|
|
359
348
|
# defaults to emit a deprecation warning.
|
|
360
349
|
#
|
|
361
|
-
# With the default,
|
|
362
|
-
# Warning[:deprecated] = true
|
|
350
|
+
# With the default, <tt>false</tt>:
|
|
363
351
|
# JSON.generate({ foo: 1, "foo" => 2 })
|
|
364
|
-
# # warning: detected duplicate key "foo" in {foo: 1, "foo" => 2}.
|
|
365
|
-
# # This will raise an error in json 3.0 unless enabled via `allow_duplicate_key: true`
|
|
366
|
-
# # => '{"foo":1,"foo":2}'
|
|
367
|
-
#
|
|
368
|
-
# With <tt>false</tt>
|
|
369
|
-
# JSON.generate({ foo: 1, "foo" => 2 }, allow_duplicate_key: false)
|
|
370
352
|
# # detected duplicate key "foo" in {foo: 1, "foo" => 2} (JSON::GeneratorError)
|
|
371
353
|
#
|
|
372
|
-
#
|
|
354
|
+
# With <tt>true</tt>
|
|
355
|
+
# JSON.generate({ foo: 1, "foo" => 2 }, allow_duplicate_key: true)
|
|
356
|
+
# # => '{"foo":1,"foo":2}'
|
|
373
357
|
#
|
|
374
358
|
# ---
|
|
375
359
|
#
|
|
@@ -384,6 +368,16 @@ require 'json/common'
|
|
|
384
368
|
# # Raises JSON::NestingError (nesting of 2 is too deep):
|
|
385
369
|
# JSON.generate(obj, max_nesting: 2)
|
|
386
370
|
#
|
|
371
|
+
# With +false+:
|
|
372
|
+
# obj = []
|
|
373
|
+
# obj[0] = obj
|
|
374
|
+
# # Raises SystemStackError: stack level too deep
|
|
375
|
+
# JSON.generate(obj, max_nesting: false)
|
|
376
|
+
#
|
|
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.
|
|
380
|
+
#
|
|
387
381
|
# ====== Escaping Options
|
|
388
382
|
#
|
|
389
383
|
# Options +script_safe+ (boolean) specifies wether <tt>'\u2028'</tt>, <tt>'\u2029'</tt>
|
|
@@ -452,241 +446,6 @@ require 'json/common'
|
|
|
452
446
|
# }
|
|
453
447
|
# }
|
|
454
448
|
#
|
|
455
|
-
# == \JSON Additions
|
|
456
|
-
#
|
|
457
|
-
# Note that JSON Additions must only be used with trusted data, and is
|
|
458
|
-
# deprecated.
|
|
459
|
-
#
|
|
460
|
-
# When you "round trip" a non-\String object from Ruby to \JSON and back,
|
|
461
|
-
# you have a new \String, instead of the object you began with:
|
|
462
|
-
# ruby0 = Range.new(0, 2)
|
|
463
|
-
# json = JSON.generate(ruby0)
|
|
464
|
-
# json # => '0..2"'
|
|
465
|
-
# ruby1 = JSON.parse(json)
|
|
466
|
-
# ruby1 # => '0..2'
|
|
467
|
-
# ruby1.class # => String
|
|
468
|
-
#
|
|
469
|
-
# You can use \JSON _additions_ to preserve the original object.
|
|
470
|
-
# The addition is an extension of a ruby class, so that:
|
|
471
|
-
# - \JSON.generate stores more information in the \JSON string.
|
|
472
|
-
# - \JSON.parse, called with option +create_additions+,
|
|
473
|
-
# uses that information to create a proper Ruby object.
|
|
474
|
-
#
|
|
475
|
-
# This example shows a \Range being generated into \JSON
|
|
476
|
-
# and parsed back into Ruby, both without and with
|
|
477
|
-
# the addition for \Range:
|
|
478
|
-
# ruby = Range.new(0, 2)
|
|
479
|
-
# # This passage does not use the addition for Range.
|
|
480
|
-
# json0 = JSON.generate(ruby)
|
|
481
|
-
# ruby0 = JSON.parse(json0)
|
|
482
|
-
# # This passage uses the addition for Range.
|
|
483
|
-
# require 'json/add/range'
|
|
484
|
-
# json1 = JSON.generate(ruby)
|
|
485
|
-
# ruby1 = JSON.parse(json1, create_additions: true)
|
|
486
|
-
# # Make a nice display.
|
|
487
|
-
# display = <<~EOT
|
|
488
|
-
# Generated JSON:
|
|
489
|
-
# Without addition: #{json0} (#{json0.class})
|
|
490
|
-
# With addition: #{json1} (#{json1.class})
|
|
491
|
-
# Parsed JSON:
|
|
492
|
-
# Without addition: #{ruby0.inspect} (#{ruby0.class})
|
|
493
|
-
# With addition: #{ruby1.inspect} (#{ruby1.class})
|
|
494
|
-
# EOT
|
|
495
|
-
# puts display
|
|
496
|
-
#
|
|
497
|
-
# This output shows the different results:
|
|
498
|
-
# Generated JSON:
|
|
499
|
-
# Without addition: "0..2" (String)
|
|
500
|
-
# With addition: {"json_class":"Range","a":[0,2,false]} (String)
|
|
501
|
-
# Parsed JSON:
|
|
502
|
-
# Without addition: "0..2" (String)
|
|
503
|
-
# With addition: 0..2 (Range)
|
|
504
|
-
#
|
|
505
|
-
# The \JSON module includes additions for certain classes.
|
|
506
|
-
# You can also craft custom additions.
|
|
507
|
-
# See {Custom \JSON Additions}[#module-JSON-label-Custom+JSON+Additions].
|
|
508
|
-
#
|
|
509
|
-
# === Built-in Additions
|
|
510
|
-
#
|
|
511
|
-
# The \JSON module includes additions for certain classes.
|
|
512
|
-
# To use an addition, +require+ its source:
|
|
513
|
-
# - BigDecimal: <tt>require 'json/add/bigdecimal'</tt>
|
|
514
|
-
# - Complex: <tt>require 'json/add/complex'</tt>
|
|
515
|
-
# - Date: <tt>require 'json/add/date'</tt>
|
|
516
|
-
# - DateTime: <tt>require 'json/add/date_time'</tt>
|
|
517
|
-
# - Exception: <tt>require 'json/add/exception'</tt>
|
|
518
|
-
# - OpenStruct: <tt>require 'json/add/ostruct'</tt>
|
|
519
|
-
# - Range: <tt>require 'json/add/range'</tt>
|
|
520
|
-
# - Rational: <tt>require 'json/add/rational'</tt>
|
|
521
|
-
# - Regexp: <tt>require 'json/add/regexp'</tt>
|
|
522
|
-
# - Set: <tt>require 'json/add/set'</tt>
|
|
523
|
-
# - Struct: <tt>require 'json/add/struct'</tt>
|
|
524
|
-
# - Symbol: <tt>require 'json/add/symbol'</tt>
|
|
525
|
-
# - Time: <tt>require 'json/add/time'</tt>
|
|
526
|
-
#
|
|
527
|
-
# To reduce punctuation clutter, the examples below
|
|
528
|
-
# show the generated \JSON via +puts+, rather than the usual +inspect+,
|
|
529
|
-
#
|
|
530
|
-
# \BigDecimal:
|
|
531
|
-
# require 'json/add/bigdecimal'
|
|
532
|
-
# ruby0 = BigDecimal(0) # 0.0
|
|
533
|
-
# json = JSON.generate(ruby0) # {"json_class":"BigDecimal","b":"27:0.0"}
|
|
534
|
-
# ruby1 = JSON.parse(json, create_additions: true) # 0.0
|
|
535
|
-
# ruby1.class # => BigDecimal
|
|
536
|
-
#
|
|
537
|
-
# \Complex:
|
|
538
|
-
# require 'json/add/complex'
|
|
539
|
-
# ruby0 = Complex(1+0i) # 1+0i
|
|
540
|
-
# json = JSON.generate(ruby0) # {"json_class":"Complex","r":1,"i":0}
|
|
541
|
-
# ruby1 = JSON.parse(json, create_additions: true) # 1+0i
|
|
542
|
-
# ruby1.class # Complex
|
|
543
|
-
#
|
|
544
|
-
# \Date:
|
|
545
|
-
# require 'json/add/date'
|
|
546
|
-
# ruby0 = Date.today # 2020-05-02
|
|
547
|
-
# json = JSON.generate(ruby0) # {"json_class":"Date","y":2020,"m":5,"d":2,"sg":2299161.0}
|
|
548
|
-
# ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02
|
|
549
|
-
# ruby1.class # Date
|
|
550
|
-
#
|
|
551
|
-
# \DateTime:
|
|
552
|
-
# require 'json/add/date_time'
|
|
553
|
-
# ruby0 = DateTime.now # 2020-05-02T10:38:13-05:00
|
|
554
|
-
# 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}
|
|
555
|
-
# ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02T10:38:13-05:00
|
|
556
|
-
# ruby1.class # DateTime
|
|
557
|
-
#
|
|
558
|
-
# \Exception (and its subclasses including \RuntimeError):
|
|
559
|
-
# require 'json/add/exception'
|
|
560
|
-
# ruby0 = Exception.new('A message') # A message
|
|
561
|
-
# json = JSON.generate(ruby0) # {"json_class":"Exception","m":"A message","b":null}
|
|
562
|
-
# ruby1 = JSON.parse(json, create_additions: true) # A message
|
|
563
|
-
# ruby1.class # Exception
|
|
564
|
-
# ruby0 = RuntimeError.new('Another message') # Another message
|
|
565
|
-
# json = JSON.generate(ruby0) # {"json_class":"RuntimeError","m":"Another message","b":null}
|
|
566
|
-
# ruby1 = JSON.parse(json, create_additions: true) # Another message
|
|
567
|
-
# ruby1.class # RuntimeError
|
|
568
|
-
#
|
|
569
|
-
# \OpenStruct:
|
|
570
|
-
# require 'json/add/ostruct'
|
|
571
|
-
# ruby0 = OpenStruct.new(name: 'Matz', language: 'Ruby') # #<OpenStruct name="Matz", language="Ruby">
|
|
572
|
-
# json = JSON.generate(ruby0) # {"json_class":"OpenStruct","t":{"name":"Matz","language":"Ruby"}}
|
|
573
|
-
# ruby1 = JSON.parse(json, create_additions: true) # #<OpenStruct name="Matz", language="Ruby">
|
|
574
|
-
# ruby1.class # OpenStruct
|
|
575
|
-
#
|
|
576
|
-
# \Range:
|
|
577
|
-
# require 'json/add/range'
|
|
578
|
-
# ruby0 = Range.new(0, 2) # 0..2
|
|
579
|
-
# json = JSON.generate(ruby0) # {"json_class":"Range","a":[0,2,false]}
|
|
580
|
-
# ruby1 = JSON.parse(json, create_additions: true) # 0..2
|
|
581
|
-
# ruby1.class # Range
|
|
582
|
-
#
|
|
583
|
-
# \Rational:
|
|
584
|
-
# require 'json/add/rational'
|
|
585
|
-
# ruby0 = Rational(1, 3) # 1/3
|
|
586
|
-
# json = JSON.generate(ruby0) # {"json_class":"Rational","n":1,"d":3}
|
|
587
|
-
# ruby1 = JSON.parse(json, create_additions: true) # 1/3
|
|
588
|
-
# ruby1.class # Rational
|
|
589
|
-
#
|
|
590
|
-
# \Regexp:
|
|
591
|
-
# require 'json/add/regexp'
|
|
592
|
-
# ruby0 = Regexp.new('foo') # (?-mix:foo)
|
|
593
|
-
# json = JSON.generate(ruby0) # {"json_class":"Regexp","o":0,"s":"foo"}
|
|
594
|
-
# ruby1 = JSON.parse(json, create_additions: true) # (?-mix:foo)
|
|
595
|
-
# ruby1.class # Regexp
|
|
596
|
-
#
|
|
597
|
-
# \Set:
|
|
598
|
-
# require 'json/add/set'
|
|
599
|
-
# ruby0 = Set.new([0, 1, 2]) # #<Set: {0, 1, 2}>
|
|
600
|
-
# json = JSON.generate(ruby0) # {"json_class":"Set","a":[0,1,2]}
|
|
601
|
-
# ruby1 = JSON.parse(json, create_additions: true) # #<Set: {0, 1, 2}>
|
|
602
|
-
# ruby1.class # Set
|
|
603
|
-
#
|
|
604
|
-
# \Struct:
|
|
605
|
-
# require 'json/add/struct'
|
|
606
|
-
# Customer = Struct.new(:name, :address) # Customer
|
|
607
|
-
# ruby0 = Customer.new("Dave", "123 Main") # #<struct Customer name="Dave", address="123 Main">
|
|
608
|
-
# json = JSON.generate(ruby0) # {"json_class":"Customer","v":["Dave","123 Main"]}
|
|
609
|
-
# ruby1 = JSON.parse(json, create_additions: true) # #<struct Customer name="Dave", address="123 Main">
|
|
610
|
-
# ruby1.class # Customer
|
|
611
|
-
#
|
|
612
|
-
# \Symbol:
|
|
613
|
-
# require 'json/add/symbol'
|
|
614
|
-
# ruby0 = :foo # foo
|
|
615
|
-
# json = JSON.generate(ruby0) # {"json_class":"Symbol","s":"foo"}
|
|
616
|
-
# ruby1 = JSON.parse(json, create_additions: true) # foo
|
|
617
|
-
# ruby1.class # Symbol
|
|
618
|
-
#
|
|
619
|
-
# \Time:
|
|
620
|
-
# require 'json/add/time'
|
|
621
|
-
# ruby0 = Time.now # 2020-05-02 11:28:26 -0500
|
|
622
|
-
# json = JSON.generate(ruby0) # {"json_class":"Time","s":1588436906,"n":840560000}
|
|
623
|
-
# ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02 11:28:26 -0500
|
|
624
|
-
# ruby1.class # Time
|
|
625
|
-
#
|
|
626
|
-
#
|
|
627
|
-
# === Custom \JSON Additions
|
|
628
|
-
#
|
|
629
|
-
# In addition to the \JSON additions provided,
|
|
630
|
-
# you can craft \JSON additions of your own,
|
|
631
|
-
# either for Ruby built-in classes or for user-defined classes.
|
|
632
|
-
#
|
|
633
|
-
# Here's a user-defined class +Foo+:
|
|
634
|
-
# class Foo
|
|
635
|
-
# attr_accessor :bar, :baz
|
|
636
|
-
# def initialize(bar, baz)
|
|
637
|
-
# self.bar = bar
|
|
638
|
-
# self.baz = baz
|
|
639
|
-
# end
|
|
640
|
-
# end
|
|
641
|
-
#
|
|
642
|
-
# Here's the \JSON addition for it:
|
|
643
|
-
# # Extend class Foo with JSON addition.
|
|
644
|
-
# class Foo
|
|
645
|
-
# # Serialize Foo object with its class name and arguments
|
|
646
|
-
# def to_json(*args)
|
|
647
|
-
# {
|
|
648
|
-
# JSON.create_id => self.class.name,
|
|
649
|
-
# 'a' => [ bar, baz ]
|
|
650
|
-
# }.to_json(*args)
|
|
651
|
-
# end
|
|
652
|
-
# # Deserialize JSON string by constructing new Foo object with arguments.
|
|
653
|
-
# def self.json_create(object)
|
|
654
|
-
# new(*object['a'])
|
|
655
|
-
# end
|
|
656
|
-
# end
|
|
657
|
-
#
|
|
658
|
-
# Demonstration:
|
|
659
|
-
# require 'json'
|
|
660
|
-
# # This Foo object has no custom addition.
|
|
661
|
-
# foo0 = Foo.new(0, 1)
|
|
662
|
-
# json0 = JSON.generate(foo0)
|
|
663
|
-
# obj0 = JSON.parse(json0)
|
|
664
|
-
# # Lood the custom addition.
|
|
665
|
-
# require_relative 'foo_addition'
|
|
666
|
-
# # This foo has the custom addition.
|
|
667
|
-
# foo1 = Foo.new(0, 1)
|
|
668
|
-
# json1 = JSON.generate(foo1)
|
|
669
|
-
# obj1 = JSON.parse(json1, create_additions: true)
|
|
670
|
-
# # Make a nice display.
|
|
671
|
-
# display = <<~EOT
|
|
672
|
-
# Generated JSON:
|
|
673
|
-
# Without custom addition: #{json0} (#{json0.class})
|
|
674
|
-
# With custom addition: #{json1} (#{json1.class})
|
|
675
|
-
# Parsed JSON:
|
|
676
|
-
# Without custom addition: #{obj0.inspect} (#{obj0.class})
|
|
677
|
-
# With custom addition: #{obj1.inspect} (#{obj1.class})
|
|
678
|
-
# EOT
|
|
679
|
-
# puts display
|
|
680
|
-
#
|
|
681
|
-
# Output:
|
|
682
|
-
#
|
|
683
|
-
# Generated JSON:
|
|
684
|
-
# Without custom addition: "#<Foo:0x0000000006534e80>" (String)
|
|
685
|
-
# With custom addition: {"json_class":"Foo","a":[0,1]} (String)
|
|
686
|
-
# Parsed JSON:
|
|
687
|
-
# Without custom addition: "#<Foo:0x0000000006534e80>" (String)
|
|
688
|
-
# With custom addition: #<Foo:0x0000000006473bb8 @bar=0, @baz=1> (Foo)
|
|
689
|
-
#
|
|
690
449
|
module JSON
|
|
691
450
|
require 'json/version'
|
|
692
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)
|