json 2.18.1 → 3.0.0
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 +127 -0
- data/LEGAL +3 -3
- data/README.md +19 -90
- data/ext/json/ext/fbuffer/fbuffer.h +46 -36
- data/ext/json/ext/generator/extconf.rb +3 -0
- data/ext/json/ext/generator/generator.c +200 -364
- data/ext/json/ext/json.h +95 -0
- data/ext/json/ext/parser/extconf.rb +27 -0
- data/ext/json/ext/parser/parser.c +1654 -406
- data/ext/json/ext/simd/simd.h +0 -10
- data/ext/json/ext/vendor/fast_float_parser.h +814 -0
- data/lib/json/common.rb +184 -411
- data/lib/json/ext/generator/state.rb +2 -28
- data/lib/json/ext.rb +25 -1
- data/lib/json/truffle_ruby/generator.rb +89 -81
- data/lib/json/version.rb +1 -1
- data/lib/json.rb +53 -264
- metadata +3 -19
- data/ext/json/ext/vendor/ryu.h +0 -819
- 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
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
|
-
# When set to
|
|
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
|
#
|
|
@@ -184,6 +180,18 @@ require 'json/common'
|
|
|
184
180
|
#
|
|
185
181
|
# ---
|
|
186
182
|
#
|
|
183
|
+
# Option +allow_comments+ (boolean) specifies whether to allow
|
|
184
|
+
# JavaScript style comments (either <tt>// comment</tt> or <tt>/* comment */</tt>);
|
|
185
|
+
# defaults to +false+.
|
|
186
|
+
#
|
|
187
|
+
# When set to +false+, the default:
|
|
188
|
+
# JSON.parse('/* comment */ {"a": 1, "a":2}') # unexpected character: '/' at line 1 column 1 (JSON::ParserError)
|
|
189
|
+
#
|
|
190
|
+
# When set to +true+, comments are ignored:
|
|
191
|
+
# JSON.parse('/* comment */ {"a": 1, "a":2} // more comment') # => {"a" => 2}
|
|
192
|
+
#
|
|
193
|
+
# ---
|
|
194
|
+
#
|
|
187
195
|
# Option +allow_control_characters+ (boolean) specifies whether to allow
|
|
188
196
|
# unescaped ASCII control characters, such as newlines, in strings;
|
|
189
197
|
# defaults to +false+.
|
|
@@ -194,6 +202,18 @@ require 'json/common'
|
|
|
194
202
|
# When enabled:
|
|
195
203
|
# JSON.parse(%{"Hello\nWorld"}, allow_control_characters: true) # => "Hello\nWorld"
|
|
196
204
|
#
|
|
205
|
+
# ---
|
|
206
|
+
#
|
|
207
|
+
# Option +allow_invalid_escape+ (boolean) specifies whether to ignore backslahes that are followed
|
|
208
|
+
# by an invalid escape character in strings;
|
|
209
|
+
# defaults to +false+.
|
|
210
|
+
#
|
|
211
|
+
# With the default, +false+:
|
|
212
|
+
# JSON.parse('"Hell\o"') # invalid escape character in string (JSON::ParserError)
|
|
213
|
+
#
|
|
214
|
+
# When enabled:
|
|
215
|
+
# JSON.parse('"Hell\o"', allow_invalid_escape: true) # => "Hello"
|
|
216
|
+
#
|
|
197
217
|
# ====== Output Options
|
|
198
218
|
#
|
|
199
219
|
# Option +freeze+ (boolean) specifies whether the returned objects will be frozen;
|
|
@@ -239,11 +259,6 @@ require 'json/common'
|
|
|
239
259
|
# ruby = JSON.parse(source, {array_class: Set})
|
|
240
260
|
# ruby # => #<Set: {"foo", 1.0, true, false, nil}>
|
|
241
261
|
#
|
|
242
|
-
# ---
|
|
243
|
-
#
|
|
244
|
-
# Option +create_additions+ (boolean) specifies whether to use \JSON additions in parsing.
|
|
245
|
-
# See {\JSON Additions}[#module-JSON-label-JSON+Additions].
|
|
246
|
-
#
|
|
247
262
|
# === Generating \JSON
|
|
248
263
|
#
|
|
249
264
|
# To generate a Ruby \String containing \JSON data,
|
|
@@ -323,8 +338,8 @@ require 'json/common'
|
|
|
323
338
|
# JSON.generate(JSON::MinusInfinity)
|
|
324
339
|
#
|
|
325
340
|
# Allow:
|
|
326
|
-
# ruby = [Float::
|
|
327
|
-
# JSON.generate(ruby, allow_nan: true) # => '[NaN,Infinity,-Infinity]'
|
|
341
|
+
# ruby = [Float::NAN, Float::INFINITY, JSON::NaN, JSON::Infinity, JSON::MinusInfinity]
|
|
342
|
+
# JSON.generate(ruby, allow_nan: true) # => '[NaN,Infinity,NaN,Infinity,-Infinity]'
|
|
328
343
|
#
|
|
329
344
|
# ---
|
|
330
345
|
#
|
|
@@ -332,18 +347,13 @@ require 'json/common'
|
|
|
332
347
|
# hashes with duplicate keys should be allowed or produce an error.
|
|
333
348
|
# defaults to emit a deprecation warning.
|
|
334
349
|
#
|
|
335
|
-
# With the default,
|
|
336
|
-
# Warning[:deprecated] = true
|
|
350
|
+
# With the default, <tt>false</tt>:
|
|
337
351
|
# JSON.generate({ foo: 1, "foo" => 2 })
|
|
338
|
-
# # warning: detected duplicate key "foo" in {foo: 1, "foo" => 2}.
|
|
339
|
-
# # This will raise an error in json 3.0 unless enabled via `allow_duplicate_key: true`
|
|
340
|
-
# # => '{"foo":1,"foo":2}'
|
|
341
|
-
#
|
|
342
|
-
# With <tt>false</tt>
|
|
343
|
-
# JSON.generate({ foo: 1, "foo" => 2 }, allow_duplicate_key: false)
|
|
344
352
|
# # detected duplicate key "foo" in {foo: 1, "foo" => 2} (JSON::GeneratorError)
|
|
345
353
|
#
|
|
346
|
-
#
|
|
354
|
+
# With <tt>true</tt>
|
|
355
|
+
# JSON.generate({ foo: 1, "foo" => 2 }, allow_duplicate_key: true)
|
|
356
|
+
# # => '{"foo":1,"foo":2}'
|
|
347
357
|
#
|
|
348
358
|
# ---
|
|
349
359
|
#
|
|
@@ -358,6 +368,16 @@ require 'json/common'
|
|
|
358
368
|
# # Raises JSON::NestingError (nesting of 2 is too deep):
|
|
359
369
|
# JSON.generate(obj, max_nesting: 2)
|
|
360
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
|
+
#
|
|
361
381
|
# ====== Escaping Options
|
|
362
382
|
#
|
|
363
383
|
# Options +script_safe+ (boolean) specifies wether <tt>'\u2028'</tt>, <tt>'\u2029'</tt>
|
|
@@ -382,7 +402,6 @@ require 'json/common'
|
|
|
382
402
|
# to be inserted after each \JSON object; defaults to the empty \String, <tt>''</tt>.
|
|
383
403
|
# - Option +indent+ (\String) specifies the string (usually spaces) to be
|
|
384
404
|
# used for indentation; defaults to the empty \String, <tt>''</tt>;
|
|
385
|
-
# defaults to the empty \String, <tt>''</tt>;
|
|
386
405
|
# has no effect unless options +array_nl+ or +object_nl+ specify newlines.
|
|
387
406
|
# - Option +space+ (\String) specifies a string (usually a space) to be
|
|
388
407
|
# inserted after the colon in each \JSON object's pair;
|
|
@@ -390,6 +409,11 @@ require 'json/common'
|
|
|
390
409
|
# - Option +space_before+ (\String) specifies a string (usually a space) to be
|
|
391
410
|
# inserted before the colon in each \JSON object's pair;
|
|
392
411
|
# defaults to the empty \String, <tt>''</tt>.
|
|
412
|
+
# - Option +sort_keys+ (boolean or \Proc) controls whether and how the keys of a
|
|
413
|
+
# hash are sorted when generating the output; defaults to <tt>false</tt>.
|
|
414
|
+
# When +true+, keys are sorted lexicographically. When a \Proc, it receives
|
|
415
|
+
# the entire \Hash and must return a \Hash with its pairs in the desired
|
|
416
|
+
# order, allowing for arbitrary sort orders.
|
|
393
417
|
#
|
|
394
418
|
# In this example, +obj+ is used first to generate the shortest
|
|
395
419
|
# \JSON data (no whitespace), then again with all formatting options
|
|
@@ -422,241 +446,6 @@ require 'json/common'
|
|
|
422
446
|
# }
|
|
423
447
|
# }
|
|
424
448
|
#
|
|
425
|
-
# == \JSON Additions
|
|
426
|
-
#
|
|
427
|
-
# Note that JSON Additions must only be used with trusted data, and is
|
|
428
|
-
# deprecated.
|
|
429
|
-
#
|
|
430
|
-
# When you "round trip" a non-\String object from Ruby to \JSON and back,
|
|
431
|
-
# you have a new \String, instead of the object you began with:
|
|
432
|
-
# ruby0 = Range.new(0, 2)
|
|
433
|
-
# json = JSON.generate(ruby0)
|
|
434
|
-
# json # => '0..2"'
|
|
435
|
-
# ruby1 = JSON.parse(json)
|
|
436
|
-
# ruby1 # => '0..2'
|
|
437
|
-
# ruby1.class # => String
|
|
438
|
-
#
|
|
439
|
-
# You can use \JSON _additions_ to preserve the original object.
|
|
440
|
-
# The addition is an extension of a ruby class, so that:
|
|
441
|
-
# - \JSON.generate stores more information in the \JSON string.
|
|
442
|
-
# - \JSON.parse, called with option +create_additions+,
|
|
443
|
-
# uses that information to create a proper Ruby object.
|
|
444
|
-
#
|
|
445
|
-
# This example shows a \Range being generated into \JSON
|
|
446
|
-
# and parsed back into Ruby, both without and with
|
|
447
|
-
# the addition for \Range:
|
|
448
|
-
# ruby = Range.new(0, 2)
|
|
449
|
-
# # This passage does not use the addition for Range.
|
|
450
|
-
# json0 = JSON.generate(ruby)
|
|
451
|
-
# ruby0 = JSON.parse(json0)
|
|
452
|
-
# # This passage uses the addition for Range.
|
|
453
|
-
# require 'json/add/range'
|
|
454
|
-
# json1 = JSON.generate(ruby)
|
|
455
|
-
# ruby1 = JSON.parse(json1, create_additions: true)
|
|
456
|
-
# # Make a nice display.
|
|
457
|
-
# display = <<~EOT
|
|
458
|
-
# Generated JSON:
|
|
459
|
-
# Without addition: #{json0} (#{json0.class})
|
|
460
|
-
# With addition: #{json1} (#{json1.class})
|
|
461
|
-
# Parsed JSON:
|
|
462
|
-
# Without addition: #{ruby0.inspect} (#{ruby0.class})
|
|
463
|
-
# With addition: #{ruby1.inspect} (#{ruby1.class})
|
|
464
|
-
# EOT
|
|
465
|
-
# puts display
|
|
466
|
-
#
|
|
467
|
-
# This output shows the different results:
|
|
468
|
-
# Generated JSON:
|
|
469
|
-
# Without addition: "0..2" (String)
|
|
470
|
-
# With addition: {"json_class":"Range","a":[0,2,false]} (String)
|
|
471
|
-
# Parsed JSON:
|
|
472
|
-
# Without addition: "0..2" (String)
|
|
473
|
-
# With addition: 0..2 (Range)
|
|
474
|
-
#
|
|
475
|
-
# The \JSON module includes additions for certain classes.
|
|
476
|
-
# You can also craft custom additions.
|
|
477
|
-
# See {Custom \JSON Additions}[#module-JSON-label-Custom+JSON+Additions].
|
|
478
|
-
#
|
|
479
|
-
# === Built-in Additions
|
|
480
|
-
#
|
|
481
|
-
# The \JSON module includes additions for certain classes.
|
|
482
|
-
# To use an addition, +require+ its source:
|
|
483
|
-
# - BigDecimal: <tt>require 'json/add/bigdecimal'</tt>
|
|
484
|
-
# - Complex: <tt>require 'json/add/complex'</tt>
|
|
485
|
-
# - Date: <tt>require 'json/add/date'</tt>
|
|
486
|
-
# - DateTime: <tt>require 'json/add/date_time'</tt>
|
|
487
|
-
# - Exception: <tt>require 'json/add/exception'</tt>
|
|
488
|
-
# - OpenStruct: <tt>require 'json/add/ostruct'</tt>
|
|
489
|
-
# - Range: <tt>require 'json/add/range'</tt>
|
|
490
|
-
# - Rational: <tt>require 'json/add/rational'</tt>
|
|
491
|
-
# - Regexp: <tt>require 'json/add/regexp'</tt>
|
|
492
|
-
# - Set: <tt>require 'json/add/set'</tt>
|
|
493
|
-
# - Struct: <tt>require 'json/add/struct'</tt>
|
|
494
|
-
# - Symbol: <tt>require 'json/add/symbol'</tt>
|
|
495
|
-
# - Time: <tt>require 'json/add/time'</tt>
|
|
496
|
-
#
|
|
497
|
-
# To reduce punctuation clutter, the examples below
|
|
498
|
-
# show the generated \JSON via +puts+, rather than the usual +inspect+,
|
|
499
|
-
#
|
|
500
|
-
# \BigDecimal:
|
|
501
|
-
# require 'json/add/bigdecimal'
|
|
502
|
-
# ruby0 = BigDecimal(0) # 0.0
|
|
503
|
-
# json = JSON.generate(ruby0) # {"json_class":"BigDecimal","b":"27:0.0"}
|
|
504
|
-
# ruby1 = JSON.parse(json, create_additions: true) # 0.0
|
|
505
|
-
# ruby1.class # => BigDecimal
|
|
506
|
-
#
|
|
507
|
-
# \Complex:
|
|
508
|
-
# require 'json/add/complex'
|
|
509
|
-
# ruby0 = Complex(1+0i) # 1+0i
|
|
510
|
-
# json = JSON.generate(ruby0) # {"json_class":"Complex","r":1,"i":0}
|
|
511
|
-
# ruby1 = JSON.parse(json, create_additions: true) # 1+0i
|
|
512
|
-
# ruby1.class # Complex
|
|
513
|
-
#
|
|
514
|
-
# \Date:
|
|
515
|
-
# require 'json/add/date'
|
|
516
|
-
# ruby0 = Date.today # 2020-05-02
|
|
517
|
-
# json = JSON.generate(ruby0) # {"json_class":"Date","y":2020,"m":5,"d":2,"sg":2299161.0}
|
|
518
|
-
# ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02
|
|
519
|
-
# ruby1.class # Date
|
|
520
|
-
#
|
|
521
|
-
# \DateTime:
|
|
522
|
-
# require 'json/add/date_time'
|
|
523
|
-
# ruby0 = DateTime.now # 2020-05-02T10:38:13-05:00
|
|
524
|
-
# 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}
|
|
525
|
-
# ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02T10:38:13-05:00
|
|
526
|
-
# ruby1.class # DateTime
|
|
527
|
-
#
|
|
528
|
-
# \Exception (and its subclasses including \RuntimeError):
|
|
529
|
-
# require 'json/add/exception'
|
|
530
|
-
# ruby0 = Exception.new('A message') # A message
|
|
531
|
-
# json = JSON.generate(ruby0) # {"json_class":"Exception","m":"A message","b":null}
|
|
532
|
-
# ruby1 = JSON.parse(json, create_additions: true) # A message
|
|
533
|
-
# ruby1.class # Exception
|
|
534
|
-
# ruby0 = RuntimeError.new('Another message') # Another message
|
|
535
|
-
# json = JSON.generate(ruby0) # {"json_class":"RuntimeError","m":"Another message","b":null}
|
|
536
|
-
# ruby1 = JSON.parse(json, create_additions: true) # Another message
|
|
537
|
-
# ruby1.class # RuntimeError
|
|
538
|
-
#
|
|
539
|
-
# \OpenStruct:
|
|
540
|
-
# require 'json/add/ostruct'
|
|
541
|
-
# ruby0 = OpenStruct.new(name: 'Matz', language: 'Ruby') # #<OpenStruct name="Matz", language="Ruby">
|
|
542
|
-
# json = JSON.generate(ruby0) # {"json_class":"OpenStruct","t":{"name":"Matz","language":"Ruby"}}
|
|
543
|
-
# ruby1 = JSON.parse(json, create_additions: true) # #<OpenStruct name="Matz", language="Ruby">
|
|
544
|
-
# ruby1.class # OpenStruct
|
|
545
|
-
#
|
|
546
|
-
# \Range:
|
|
547
|
-
# require 'json/add/range'
|
|
548
|
-
# ruby0 = Range.new(0, 2) # 0..2
|
|
549
|
-
# json = JSON.generate(ruby0) # {"json_class":"Range","a":[0,2,false]}
|
|
550
|
-
# ruby1 = JSON.parse(json, create_additions: true) # 0..2
|
|
551
|
-
# ruby1.class # Range
|
|
552
|
-
#
|
|
553
|
-
# \Rational:
|
|
554
|
-
# require 'json/add/rational'
|
|
555
|
-
# ruby0 = Rational(1, 3) # 1/3
|
|
556
|
-
# json = JSON.generate(ruby0) # {"json_class":"Rational","n":1,"d":3}
|
|
557
|
-
# ruby1 = JSON.parse(json, create_additions: true) # 1/3
|
|
558
|
-
# ruby1.class # Rational
|
|
559
|
-
#
|
|
560
|
-
# \Regexp:
|
|
561
|
-
# require 'json/add/regexp'
|
|
562
|
-
# ruby0 = Regexp.new('foo') # (?-mix:foo)
|
|
563
|
-
# json = JSON.generate(ruby0) # {"json_class":"Regexp","o":0,"s":"foo"}
|
|
564
|
-
# ruby1 = JSON.parse(json, create_additions: true) # (?-mix:foo)
|
|
565
|
-
# ruby1.class # Regexp
|
|
566
|
-
#
|
|
567
|
-
# \Set:
|
|
568
|
-
# require 'json/add/set'
|
|
569
|
-
# ruby0 = Set.new([0, 1, 2]) # #<Set: {0, 1, 2}>
|
|
570
|
-
# json = JSON.generate(ruby0) # {"json_class":"Set","a":[0,1,2]}
|
|
571
|
-
# ruby1 = JSON.parse(json, create_additions: true) # #<Set: {0, 1, 2}>
|
|
572
|
-
# ruby1.class # Set
|
|
573
|
-
#
|
|
574
|
-
# \Struct:
|
|
575
|
-
# require 'json/add/struct'
|
|
576
|
-
# Customer = Struct.new(:name, :address) # Customer
|
|
577
|
-
# ruby0 = Customer.new("Dave", "123 Main") # #<struct Customer name="Dave", address="123 Main">
|
|
578
|
-
# json = JSON.generate(ruby0) # {"json_class":"Customer","v":["Dave","123 Main"]}
|
|
579
|
-
# ruby1 = JSON.parse(json, create_additions: true) # #<struct Customer name="Dave", address="123 Main">
|
|
580
|
-
# ruby1.class # Customer
|
|
581
|
-
#
|
|
582
|
-
# \Symbol:
|
|
583
|
-
# require 'json/add/symbol'
|
|
584
|
-
# ruby0 = :foo # foo
|
|
585
|
-
# json = JSON.generate(ruby0) # {"json_class":"Symbol","s":"foo"}
|
|
586
|
-
# ruby1 = JSON.parse(json, create_additions: true) # foo
|
|
587
|
-
# ruby1.class # Symbol
|
|
588
|
-
#
|
|
589
|
-
# \Time:
|
|
590
|
-
# require 'json/add/time'
|
|
591
|
-
# ruby0 = Time.now # 2020-05-02 11:28:26 -0500
|
|
592
|
-
# json = JSON.generate(ruby0) # {"json_class":"Time","s":1588436906,"n":840560000}
|
|
593
|
-
# ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02 11:28:26 -0500
|
|
594
|
-
# ruby1.class # Time
|
|
595
|
-
#
|
|
596
|
-
#
|
|
597
|
-
# === Custom \JSON Additions
|
|
598
|
-
#
|
|
599
|
-
# In addition to the \JSON additions provided,
|
|
600
|
-
# you can craft \JSON additions of your own,
|
|
601
|
-
# either for Ruby built-in classes or for user-defined classes.
|
|
602
|
-
#
|
|
603
|
-
# Here's a user-defined class +Foo+:
|
|
604
|
-
# class Foo
|
|
605
|
-
# attr_accessor :bar, :baz
|
|
606
|
-
# def initialize(bar, baz)
|
|
607
|
-
# self.bar = bar
|
|
608
|
-
# self.baz = baz
|
|
609
|
-
# end
|
|
610
|
-
# end
|
|
611
|
-
#
|
|
612
|
-
# Here's the \JSON addition for it:
|
|
613
|
-
# # Extend class Foo with JSON addition.
|
|
614
|
-
# class Foo
|
|
615
|
-
# # Serialize Foo object with its class name and arguments
|
|
616
|
-
# def to_json(*args)
|
|
617
|
-
# {
|
|
618
|
-
# JSON.create_id => self.class.name,
|
|
619
|
-
# 'a' => [ bar, baz ]
|
|
620
|
-
# }.to_json(*args)
|
|
621
|
-
# end
|
|
622
|
-
# # Deserialize JSON string by constructing new Foo object with arguments.
|
|
623
|
-
# def self.json_create(object)
|
|
624
|
-
# new(*object['a'])
|
|
625
|
-
# end
|
|
626
|
-
# end
|
|
627
|
-
#
|
|
628
|
-
# Demonstration:
|
|
629
|
-
# require 'json'
|
|
630
|
-
# # This Foo object has no custom addition.
|
|
631
|
-
# foo0 = Foo.new(0, 1)
|
|
632
|
-
# json0 = JSON.generate(foo0)
|
|
633
|
-
# obj0 = JSON.parse(json0)
|
|
634
|
-
# # Lood the custom addition.
|
|
635
|
-
# require_relative 'foo_addition'
|
|
636
|
-
# # This foo has the custom addition.
|
|
637
|
-
# foo1 = Foo.new(0, 1)
|
|
638
|
-
# json1 = JSON.generate(foo1)
|
|
639
|
-
# obj1 = JSON.parse(json1, create_additions: true)
|
|
640
|
-
# # Make a nice display.
|
|
641
|
-
# display = <<~EOT
|
|
642
|
-
# Generated JSON:
|
|
643
|
-
# Without custom addition: #{json0} (#{json0.class})
|
|
644
|
-
# With custom addition: #{json1} (#{json1.class})
|
|
645
|
-
# Parsed JSON:
|
|
646
|
-
# Without custom addition: #{obj0.inspect} (#{obj0.class})
|
|
647
|
-
# With custom addition: #{obj1.inspect} (#{obj1.class})
|
|
648
|
-
# EOT
|
|
649
|
-
# puts display
|
|
650
|
-
#
|
|
651
|
-
# Output:
|
|
652
|
-
#
|
|
653
|
-
# Generated JSON:
|
|
654
|
-
# Without custom addition: "#<Foo:0x0000000006534e80>" (String)
|
|
655
|
-
# With custom addition: {"json_class":"Foo","a":[0,1]} (String)
|
|
656
|
-
# Parsed JSON:
|
|
657
|
-
# Without custom addition: "#<Foo:0x0000000006534e80>" (String)
|
|
658
|
-
# With custom addition: #<Foo:0x0000000006473bb8 @bar=0, @baz=1> (Foo)
|
|
659
|
-
#
|
|
660
449
|
module JSON
|
|
661
450
|
require 'json/version'
|
|
662
451
|
require 'json/ext'
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: json
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 3.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Florian Frank
|
|
@@ -31,30 +31,14 @@ files:
|
|
|
31
31
|
- ext/json/ext/parser/parser.c
|
|
32
32
|
- ext/json/ext/simd/conf.rb
|
|
33
33
|
- ext/json/ext/simd/simd.h
|
|
34
|
+
- ext/json/ext/vendor/fast_float_parser.h
|
|
34
35
|
- ext/json/ext/vendor/fpconv.c
|
|
35
36
|
- ext/json/ext/vendor/jeaiii-ltoa.h
|
|
36
|
-
- ext/json/ext/vendor/ryu.h
|
|
37
37
|
- json.gemspec
|
|
38
38
|
- lib/json.rb
|
|
39
|
-
- lib/json/add/bigdecimal.rb
|
|
40
|
-
- lib/json/add/complex.rb
|
|
41
|
-
- lib/json/add/core.rb
|
|
42
|
-
- lib/json/add/date.rb
|
|
43
|
-
- lib/json/add/date_time.rb
|
|
44
|
-
- lib/json/add/exception.rb
|
|
45
|
-
- lib/json/add/ostruct.rb
|
|
46
|
-
- lib/json/add/range.rb
|
|
47
|
-
- lib/json/add/rational.rb
|
|
48
|
-
- lib/json/add/regexp.rb
|
|
49
|
-
- lib/json/add/set.rb
|
|
50
|
-
- lib/json/add/string.rb
|
|
51
|
-
- lib/json/add/struct.rb
|
|
52
|
-
- lib/json/add/symbol.rb
|
|
53
|
-
- lib/json/add/time.rb
|
|
54
39
|
- lib/json/common.rb
|
|
55
40
|
- lib/json/ext.rb
|
|
56
41
|
- lib/json/ext/generator/state.rb
|
|
57
|
-
- lib/json/generic_object.rb
|
|
58
42
|
- lib/json/truffle_ruby/generator.rb
|
|
59
43
|
- lib/json/version.rb
|
|
60
44
|
homepage: https://github.com/ruby/json
|
|
@@ -84,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
84
68
|
- !ruby/object:Gem::Version
|
|
85
69
|
version: '0'
|
|
86
70
|
requirements: []
|
|
87
|
-
rubygems_version: 4.
|
|
71
|
+
rubygems_version: 4.0.16
|
|
88
72
|
specification_version: 4
|
|
89
73
|
summary: JSON Implementation for Ruby
|
|
90
74
|
test_files: []
|