ed-precompiled_json 2.15.1

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.
data/lib/json.rb ADDED
@@ -0,0 +1,642 @@
1
+ # frozen_string_literal: true
2
+ require 'json/common'
3
+
4
+ ##
5
+ # = JavaScript \Object Notation (\JSON)
6
+ #
7
+ # \JSON is a lightweight data-interchange format.
8
+ #
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]
16
+ #
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}
21
+ #
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]]
26
+ #
27
+ # == Using \Module \JSON
28
+ #
29
+ # To make module \JSON available in your code, begin with:
30
+ # require 'json'
31
+ #
32
+ # All examples here assume that this has been done.
33
+ #
34
+ # === Parsing \JSON
35
+ #
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>
40
+ #
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.
45
+ #
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.
51
+ #
52
+ # ==== Parsing \JSON Arrays
53
+ #
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
59
+ #
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]]
64
+ #
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
+ # ==== Parsing Options
111
+ #
112
+ # ====== Input Options
113
+ #
114
+ # Option +max_nesting+ (\Integer) specifies the maximum nesting depth allowed;
115
+ # defaults to +100+; specify +false+ to disable depth checking.
116
+ #
117
+ # With the default, +false+:
118
+ # source = '[0, [1, [2, [3]]]]'
119
+ # ruby = JSON.parse(source)
120
+ # ruby # => [0, [1, [2, [3]]]]
121
+ # Too deep:
122
+ # # Raises JSON::NestingError (nesting of 2 is too deep):
123
+ # JSON.parse(source, {max_nesting: 1})
124
+ # Bad value:
125
+ # # Raises TypeError (wrong argument type Symbol (expected Fixnum)):
126
+ # JSON.parse(source, {max_nesting: :foo})
127
+ #
128
+ # ---
129
+ #
130
+ # Option +allow_duplicate_key+ specifies whether duplicate keys in objects
131
+ # should be ignored or cause an error to be raised:
132
+ #
133
+ # When not specified:
134
+ # # The last value is used and a deprecation warning emitted.
135
+ # JSON.parse('{"a": 1, "a":2}') => {"a" => 2}
136
+ # # warning: detected duplicate keys in JSON object.
137
+ # # This will raise an error in json 3.0 unless enabled via `allow_duplicate_key: true`
138
+ #
139
+ # When set to `+true+`
140
+ # # The last value is used.
141
+ # JSON.parse('{"a": 1, "a":2}') => {"a" => 2}
142
+ #
143
+ # When set to `+false+`, the future default:
144
+ # JSON.parse('{"a": 1, "a":2}') => duplicate key at line 1 column 1 (JSON::ParserError)
145
+ #
146
+ # ---
147
+ #
148
+ # Option +allow_nan+ (boolean) specifies whether to allow
149
+ # NaN, Infinity, and MinusInfinity in +source+;
150
+ # defaults to +false+.
151
+ #
152
+ # With the default, +false+:
153
+ # # Raises JSON::ParserError (225: unexpected token at '[NaN]'):
154
+ # JSON.parse('[NaN]')
155
+ # # Raises JSON::ParserError (232: unexpected token at '[Infinity]'):
156
+ # JSON.parse('[Infinity]')
157
+ # # Raises JSON::ParserError (248: unexpected token at '[-Infinity]'):
158
+ # JSON.parse('[-Infinity]')
159
+ # Allow:
160
+ # source = '[NaN, Infinity, -Infinity]'
161
+ # ruby = JSON.parse(source, {allow_nan: true})
162
+ # ruby # => [NaN, Infinity, -Infinity]
163
+ #
164
+ # ---
165
+ #
166
+ # Option +allow_trailing_comma+ (boolean) specifies whether to allow
167
+ # trailing commas in objects and arrays;
168
+ # defaults to +false+.
169
+ #
170
+ # With the default, +false+:
171
+ # JSON.parse('[1,]') # unexpected character: ']' at line 1 column 4 (JSON::ParserError)
172
+ #
173
+ # When enabled:
174
+ # JSON.parse('[1,]', allow_trailing_comma: true) # => [1]
175
+ #
176
+ # ====== Output Options
177
+ #
178
+ # Option +freeze+ (boolean) specifies whether the returned objects will be frozen;
179
+ # defaults to +false+.
180
+ #
181
+ # Option +symbolize_names+ (boolean) specifies whether returned \Hash keys
182
+ # should be Symbols;
183
+ # defaults to +false+ (use Strings).
184
+ #
185
+ # With the default, +false+:
186
+ # source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}'
187
+ # ruby = JSON.parse(source)
188
+ # ruby # => {"a"=>"foo", "b"=>1.0, "c"=>true, "d"=>false, "e"=>nil}
189
+ # Use Symbols:
190
+ # ruby = JSON.parse(source, {symbolize_names: true})
191
+ # ruby # => {:a=>"foo", :b=>1.0, :c=>true, :d=>false, :e=>nil}
192
+ #
193
+ # ---
194
+ #
195
+ # Option +object_class+ (\Class) specifies the Ruby class to be used
196
+ # for each \JSON object;
197
+ # defaults to \Hash.
198
+ #
199
+ # With the default, \Hash:
200
+ # source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}'
201
+ # ruby = JSON.parse(source)
202
+ # ruby.class # => Hash
203
+ # Use class \OpenStruct:
204
+ # ruby = JSON.parse(source, {object_class: OpenStruct})
205
+ # ruby # => #<OpenStruct a="foo", b=1.0, c=true, d=false, e=nil>
206
+ #
207
+ # ---
208
+ #
209
+ # Option +array_class+ (\Class) specifies the Ruby class to be used
210
+ # for each \JSON array;
211
+ # defaults to \Array.
212
+ #
213
+ # With the default, \Array:
214
+ # source = '["foo", 1.0, true, false, null]'
215
+ # ruby = JSON.parse(source)
216
+ # ruby.class # => Array
217
+ # Use class \Set:
218
+ # ruby = JSON.parse(source, {array_class: Set})
219
+ # ruby # => #<Set: {"foo", 1.0, true, false, nil}>
220
+ #
221
+ # ---
222
+ #
223
+ # Option +create_additions+ (boolean) specifies whether to use \JSON additions in parsing.
224
+ # See {\JSON Additions}[#module-JSON-label-JSON+Additions].
225
+ #
226
+ # === Generating \JSON
227
+ #
228
+ # To generate a Ruby \String containing \JSON data,
229
+ # use method <tt>JSON.generate(source, opts)</tt>, where
230
+ # - +source+ is a Ruby object.
231
+ # - +opts+ is a \Hash object containing options
232
+ # that control both input allowed and output formatting.
233
+ #
234
+ # ==== Generating \JSON from Arrays
235
+ #
236
+ # When the source is a Ruby \Array, JSON.generate returns
237
+ # a \String containing a \JSON array:
238
+ # ruby = [0, 's', :foo]
239
+ # json = JSON.generate(ruby)
240
+ # json # => '[0,"s","foo"]'
241
+ #
242
+ # The Ruby \Array array may contain nested arrays, hashes, and scalars
243
+ # to any depth:
244
+ # ruby = [0, [1, 2], {foo: 3, bar: 4}]
245
+ # json = JSON.generate(ruby)
246
+ # json # => '[0,[1,2],{"foo":3,"bar":4}]'
247
+ #
248
+ # ==== Generating \JSON from Hashes
249
+ #
250
+ # When the source is a Ruby \Hash, JSON.generate returns
251
+ # a \String containing a \JSON object:
252
+ # ruby = {foo: 0, bar: 's', baz: :bat}
253
+ # json = JSON.generate(ruby)
254
+ # json # => '{"foo":0,"bar":"s","baz":"bat"}'
255
+ #
256
+ # The Ruby \Hash array may contain nested arrays, hashes, and scalars
257
+ # to any depth:
258
+ # ruby = {foo: [0, 1], bar: {baz: 2, bat: 3}, bam: :bad}
259
+ # json = JSON.generate(ruby)
260
+ # json # => '{"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"}'
261
+ #
262
+ # ==== Generating \JSON from Other Objects
263
+ #
264
+ # When the source is neither an \Array nor a \Hash,
265
+ # the generated \JSON data depends on the class of the source.
266
+ #
267
+ # When the source is a Ruby \Integer or \Float, JSON.generate returns
268
+ # a \String containing a \JSON number:
269
+ # JSON.generate(42) # => '42'
270
+ # JSON.generate(0.42) # => '0.42'
271
+ #
272
+ # When the source is a Ruby \String, JSON.generate returns
273
+ # a \String containing a \JSON string (with double-quotes):
274
+ # JSON.generate('A string') # => '"A string"'
275
+ #
276
+ # When the source is +true+, +false+ or +nil+, JSON.generate returns
277
+ # a \String containing the corresponding \JSON token:
278
+ # JSON.generate(true) # => 'true'
279
+ # JSON.generate(false) # => 'false'
280
+ # JSON.generate(nil) # => 'null'
281
+ #
282
+ # When the source is none of the above, JSON.generate returns
283
+ # a \String containing a \JSON string representation of the source:
284
+ # JSON.generate(:foo) # => '"foo"'
285
+ # JSON.generate(Complex(0, 0)) # => '"0+0i"'
286
+ # JSON.generate(Dir.new('.')) # => '"#<Dir>"'
287
+ #
288
+ # ==== Generating Options
289
+ #
290
+ # ====== Input Options
291
+ #
292
+ # Option +allow_nan+ (boolean) specifies whether
293
+ # +NaN+, +Infinity+, and <tt>-Infinity</tt> may be generated;
294
+ # defaults to +false+.
295
+ #
296
+ # With the default, +false+:
297
+ # # Raises JSON::GeneratorError (920: NaN not allowed in JSON):
298
+ # JSON.generate(JSON::NaN)
299
+ # # Raises JSON::GeneratorError (917: Infinity not allowed in JSON):
300
+ # JSON.generate(JSON::Infinity)
301
+ # # Raises JSON::GeneratorError (917: -Infinity not allowed in JSON):
302
+ # JSON.generate(JSON::MinusInfinity)
303
+ #
304
+ # Allow:
305
+ # ruby = [Float::NaN, Float::Infinity, Float::MinusInfinity]
306
+ # JSON.generate(ruby, allow_nan: true) # => '[NaN,Infinity,-Infinity]'
307
+ #
308
+ # ---
309
+ #
310
+ # Option +allow_duplicate_key+ (boolean) specifies whether
311
+ # hashes with duplicate keys should be allowed or produce an error.
312
+ # defaults to emit a deprecation warning.
313
+ #
314
+ # With the default, (not set):
315
+ # Warning[:deprecated] = true
316
+ # JSON.generate({ foo: 1, "foo" => 2 })
317
+ # # warning: detected duplicate key "foo" in {foo: 1, "foo" => 2}.
318
+ # # This will raise an error in json 3.0 unless enabled via `allow_duplicate_key: true`
319
+ # # => '{"foo":1,"foo":2}'
320
+ #
321
+ # With <tt>false</tt>
322
+ # JSON.generate({ foo: 1, "foo" => 2 }, allow_duplicate_key: false)
323
+ # # detected duplicate key "foo" in {foo: 1, "foo" => 2} (JSON::GeneratorError)
324
+ #
325
+ # In version 3.0, <tt>false</tt> will become the default.
326
+ #
327
+ # ---
328
+ #
329
+ # Option +max_nesting+ (\Integer) specifies the maximum nesting depth
330
+ # in +obj+; defaults to +100+.
331
+ #
332
+ # With the default, +100+:
333
+ # obj = [[[[[[0]]]]]]
334
+ # JSON.generate(obj) # => '[[[[[[0]]]]]]'
335
+ #
336
+ # Too deep:
337
+ # # Raises JSON::NestingError (nesting of 2 is too deep):
338
+ # JSON.generate(obj, max_nesting: 2)
339
+ #
340
+ # ====== Escaping Options
341
+ #
342
+ # Options +script_safe+ (boolean) specifies wether <tt>'\u2028'</tt>, <tt>'\u2029'</tt>
343
+ # and <tt>'/'</tt> should be escaped as to make the JSON object safe to interpolate in script
344
+ # tags.
345
+ #
346
+ # Options +ascii_only+ (boolean) specifies wether all characters outside the ASCII range
347
+ # should be escaped.
348
+ #
349
+ # ====== Output Options
350
+ #
351
+ # The default formatting options generate the most compact
352
+ # \JSON data, all on one line and with no whitespace.
353
+ #
354
+ # You can use these formatting options to generate
355
+ # \JSON data in a more open format, using whitespace.
356
+ # See also JSON.pretty_generate.
357
+ #
358
+ # - Option +array_nl+ (\String) specifies a string (usually a newline)
359
+ # to be inserted after each \JSON array; defaults to the empty \String, <tt>''</tt>.
360
+ # - Option +object_nl+ (\String) specifies a string (usually a newline)
361
+ # to be inserted after each \JSON object; defaults to the empty \String, <tt>''</tt>.
362
+ # - Option +indent+ (\String) specifies the string (usually spaces) to be
363
+ # used for indentation; defaults to the empty \String, <tt>''</tt>;
364
+ # defaults to the empty \String, <tt>''</tt>;
365
+ # has no effect unless options +array_nl+ or +object_nl+ specify newlines.
366
+ # - Option +space+ (\String) specifies a string (usually a space) to be
367
+ # inserted after the colon in each \JSON object's pair;
368
+ # defaults to the empty \String, <tt>''</tt>.
369
+ # - Option +space_before+ (\String) specifies a string (usually a space) to be
370
+ # inserted before the colon in each \JSON object's pair;
371
+ # defaults to the empty \String, <tt>''</tt>.
372
+ #
373
+ # In this example, +obj+ is used first to generate the shortest
374
+ # \JSON data (no whitespace), then again with all formatting options
375
+ # specified:
376
+ #
377
+ # obj = {foo: [:bar, :baz], bat: {bam: 0, bad: 1}}
378
+ # json = JSON.generate(obj)
379
+ # puts 'Compact:', json
380
+ # opts = {
381
+ # array_nl: "\n",
382
+ # object_nl: "\n",
383
+ # indent: ' ',
384
+ # space_before: ' ',
385
+ # space: ' '
386
+ # }
387
+ # puts 'Open:', JSON.generate(obj, opts)
388
+ #
389
+ # Output:
390
+ # Compact:
391
+ # {"foo":["bar","baz"],"bat":{"bam":0,"bad":1}}
392
+ # Open:
393
+ # {
394
+ # "foo" : [
395
+ # "bar",
396
+ # "baz"
397
+ # ],
398
+ # "bat" : {
399
+ # "bam" : 0,
400
+ # "bad" : 1
401
+ # }
402
+ # }
403
+ #
404
+ # == \JSON Additions
405
+ #
406
+ # Note that JSON Additions must only be used with trusted data, and is
407
+ # deprecated.
408
+ #
409
+ # When you "round trip" a non-\String object from Ruby to \JSON and back,
410
+ # you have a new \String, instead of the object you began with:
411
+ # ruby0 = Range.new(0, 2)
412
+ # json = JSON.generate(ruby0)
413
+ # json # => '0..2"'
414
+ # ruby1 = JSON.parse(json)
415
+ # ruby1 # => '0..2'
416
+ # ruby1.class # => String
417
+ #
418
+ # You can use \JSON _additions_ to preserve the original object.
419
+ # The addition is an extension of a ruby class, so that:
420
+ # - \JSON.generate stores more information in the \JSON string.
421
+ # - \JSON.parse, called with option +create_additions+,
422
+ # uses that information to create a proper Ruby object.
423
+ #
424
+ # This example shows a \Range being generated into \JSON
425
+ # and parsed back into Ruby, both without and with
426
+ # the addition for \Range:
427
+ # ruby = Range.new(0, 2)
428
+ # # This passage does not use the addition for Range.
429
+ # json0 = JSON.generate(ruby)
430
+ # ruby0 = JSON.parse(json0)
431
+ # # This passage uses the addition for Range.
432
+ # require 'json/add/range'
433
+ # json1 = JSON.generate(ruby)
434
+ # ruby1 = JSON.parse(json1, create_additions: true)
435
+ # # Make a nice display.
436
+ # display = <<~EOT
437
+ # Generated JSON:
438
+ # Without addition: #{json0} (#{json0.class})
439
+ # With addition: #{json1} (#{json1.class})
440
+ # Parsed JSON:
441
+ # Without addition: #{ruby0.inspect} (#{ruby0.class})
442
+ # With addition: #{ruby1.inspect} (#{ruby1.class})
443
+ # EOT
444
+ # puts display
445
+ #
446
+ # This output shows the different results:
447
+ # Generated JSON:
448
+ # Without addition: "0..2" (String)
449
+ # With addition: {"json_class":"Range","a":[0,2,false]} (String)
450
+ # Parsed JSON:
451
+ # Without addition: "0..2" (String)
452
+ # With addition: 0..2 (Range)
453
+ #
454
+ # The \JSON module includes additions for certain classes.
455
+ # You can also craft custom additions.
456
+ # See {Custom \JSON Additions}[#module-JSON-label-Custom+JSON+Additions].
457
+ #
458
+ # === Built-in Additions
459
+ #
460
+ # The \JSON module includes additions for certain classes.
461
+ # To use an addition, +require+ its source:
462
+ # - BigDecimal: <tt>require 'json/add/bigdecimal'</tt>
463
+ # - Complex: <tt>require 'json/add/complex'</tt>
464
+ # - Date: <tt>require 'json/add/date'</tt>
465
+ # - DateTime: <tt>require 'json/add/date_time'</tt>
466
+ # - Exception: <tt>require 'json/add/exception'</tt>
467
+ # - OpenStruct: <tt>require 'json/add/ostruct'</tt>
468
+ # - Range: <tt>require 'json/add/range'</tt>
469
+ # - Rational: <tt>require 'json/add/rational'</tt>
470
+ # - Regexp: <tt>require 'json/add/regexp'</tt>
471
+ # - Set: <tt>require 'json/add/set'</tt>
472
+ # - Struct: <tt>require 'json/add/struct'</tt>
473
+ # - Symbol: <tt>require 'json/add/symbol'</tt>
474
+ # - Time: <tt>require 'json/add/time'</tt>
475
+ #
476
+ # To reduce punctuation clutter, the examples below
477
+ # show the generated \JSON via +puts+, rather than the usual +inspect+,
478
+ #
479
+ # \BigDecimal:
480
+ # require 'json/add/bigdecimal'
481
+ # ruby0 = BigDecimal(0) # 0.0
482
+ # json = JSON.generate(ruby0) # {"json_class":"BigDecimal","b":"27:0.0"}
483
+ # ruby1 = JSON.parse(json, create_additions: true) # 0.0
484
+ # ruby1.class # => BigDecimal
485
+ #
486
+ # \Complex:
487
+ # require 'json/add/complex'
488
+ # ruby0 = Complex(1+0i) # 1+0i
489
+ # json = JSON.generate(ruby0) # {"json_class":"Complex","r":1,"i":0}
490
+ # ruby1 = JSON.parse(json, create_additions: true) # 1+0i
491
+ # ruby1.class # Complex
492
+ #
493
+ # \Date:
494
+ # require 'json/add/date'
495
+ # ruby0 = Date.today # 2020-05-02
496
+ # json = JSON.generate(ruby0) # {"json_class":"Date","y":2020,"m":5,"d":2,"sg":2299161.0}
497
+ # ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02
498
+ # ruby1.class # Date
499
+ #
500
+ # \DateTime:
501
+ # require 'json/add/date_time'
502
+ # ruby0 = DateTime.now # 2020-05-02T10:38:13-05:00
503
+ # 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}
504
+ # ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02T10:38:13-05:00
505
+ # ruby1.class # DateTime
506
+ #
507
+ # \Exception (and its subclasses including \RuntimeError):
508
+ # require 'json/add/exception'
509
+ # ruby0 = Exception.new('A message') # A message
510
+ # json = JSON.generate(ruby0) # {"json_class":"Exception","m":"A message","b":null}
511
+ # ruby1 = JSON.parse(json, create_additions: true) # A message
512
+ # ruby1.class # Exception
513
+ # ruby0 = RuntimeError.new('Another message') # Another message
514
+ # json = JSON.generate(ruby0) # {"json_class":"RuntimeError","m":"Another message","b":null}
515
+ # ruby1 = JSON.parse(json, create_additions: true) # Another message
516
+ # ruby1.class # RuntimeError
517
+ #
518
+ # \OpenStruct:
519
+ # require 'json/add/ostruct'
520
+ # ruby0 = OpenStruct.new(name: 'Matz', language: 'Ruby') # #<OpenStruct name="Matz", language="Ruby">
521
+ # json = JSON.generate(ruby0) # {"json_class":"OpenStruct","t":{"name":"Matz","language":"Ruby"}}
522
+ # ruby1 = JSON.parse(json, create_additions: true) # #<OpenStruct name="Matz", language="Ruby">
523
+ # ruby1.class # OpenStruct
524
+ #
525
+ # \Range:
526
+ # require 'json/add/range'
527
+ # ruby0 = Range.new(0, 2) # 0..2
528
+ # json = JSON.generate(ruby0) # {"json_class":"Range","a":[0,2,false]}
529
+ # ruby1 = JSON.parse(json, create_additions: true) # 0..2
530
+ # ruby1.class # Range
531
+ #
532
+ # \Rational:
533
+ # require 'json/add/rational'
534
+ # ruby0 = Rational(1, 3) # 1/3
535
+ # json = JSON.generate(ruby0) # {"json_class":"Rational","n":1,"d":3}
536
+ # ruby1 = JSON.parse(json, create_additions: true) # 1/3
537
+ # ruby1.class # Rational
538
+ #
539
+ # \Regexp:
540
+ # require 'json/add/regexp'
541
+ # ruby0 = Regexp.new('foo') # (?-mix:foo)
542
+ # json = JSON.generate(ruby0) # {"json_class":"Regexp","o":0,"s":"foo"}
543
+ # ruby1 = JSON.parse(json, create_additions: true) # (?-mix:foo)
544
+ # ruby1.class # Regexp
545
+ #
546
+ # \Set:
547
+ # require 'json/add/set'
548
+ # ruby0 = Set.new([0, 1, 2]) # #<Set: {0, 1, 2}>
549
+ # json = JSON.generate(ruby0) # {"json_class":"Set","a":[0,1,2]}
550
+ # ruby1 = JSON.parse(json, create_additions: true) # #<Set: {0, 1, 2}>
551
+ # ruby1.class # Set
552
+ #
553
+ # \Struct:
554
+ # require 'json/add/struct'
555
+ # Customer = Struct.new(:name, :address) # Customer
556
+ # ruby0 = Customer.new("Dave", "123 Main") # #<struct Customer name="Dave", address="123 Main">
557
+ # json = JSON.generate(ruby0) # {"json_class":"Customer","v":["Dave","123 Main"]}
558
+ # ruby1 = JSON.parse(json, create_additions: true) # #<struct Customer name="Dave", address="123 Main">
559
+ # ruby1.class # Customer
560
+ #
561
+ # \Symbol:
562
+ # require 'json/add/symbol'
563
+ # ruby0 = :foo # foo
564
+ # json = JSON.generate(ruby0) # {"json_class":"Symbol","s":"foo"}
565
+ # ruby1 = JSON.parse(json, create_additions: true) # foo
566
+ # ruby1.class # Symbol
567
+ #
568
+ # \Time:
569
+ # require 'json/add/time'
570
+ # ruby0 = Time.now # 2020-05-02 11:28:26 -0500
571
+ # json = JSON.generate(ruby0) # {"json_class":"Time","s":1588436906,"n":840560000}
572
+ # ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02 11:28:26 -0500
573
+ # ruby1.class # Time
574
+ #
575
+ #
576
+ # === Custom \JSON Additions
577
+ #
578
+ # In addition to the \JSON additions provided,
579
+ # you can craft \JSON additions of your own,
580
+ # either for Ruby built-in classes or for user-defined classes.
581
+ #
582
+ # Here's a user-defined class +Foo+:
583
+ # class Foo
584
+ # attr_accessor :bar, :baz
585
+ # def initialize(bar, baz)
586
+ # self.bar = bar
587
+ # self.baz = baz
588
+ # end
589
+ # end
590
+ #
591
+ # Here's the \JSON addition for it:
592
+ # # Extend class Foo with JSON addition.
593
+ # class Foo
594
+ # # Serialize Foo object with its class name and arguments
595
+ # def to_json(*args)
596
+ # {
597
+ # JSON.create_id => self.class.name,
598
+ # 'a' => [ bar, baz ]
599
+ # }.to_json(*args)
600
+ # end
601
+ # # Deserialize JSON string by constructing new Foo object with arguments.
602
+ # def self.json_create(object)
603
+ # new(*object['a'])
604
+ # end
605
+ # end
606
+ #
607
+ # Demonstration:
608
+ # require 'json'
609
+ # # This Foo object has no custom addition.
610
+ # foo0 = Foo.new(0, 1)
611
+ # json0 = JSON.generate(foo0)
612
+ # obj0 = JSON.parse(json0)
613
+ # # Lood the custom addition.
614
+ # require_relative 'foo_addition'
615
+ # # This foo has the custom addition.
616
+ # foo1 = Foo.new(0, 1)
617
+ # json1 = JSON.generate(foo1)
618
+ # obj1 = JSON.parse(json1, create_additions: true)
619
+ # # Make a nice display.
620
+ # display = <<~EOT
621
+ # Generated JSON:
622
+ # Without custom addition: #{json0} (#{json0.class})
623
+ # With custom addition: #{json1} (#{json1.class})
624
+ # Parsed JSON:
625
+ # Without custom addition: #{obj0.inspect} (#{obj0.class})
626
+ # With custom addition: #{obj1.inspect} (#{obj1.class})
627
+ # EOT
628
+ # puts display
629
+ #
630
+ # Output:
631
+ #
632
+ # Generated JSON:
633
+ # Without custom addition: "#<Foo:0x0000000006534e80>" (String)
634
+ # With custom addition: {"json_class":"Foo","a":[0,1]} (String)
635
+ # Parsed JSON:
636
+ # Without custom addition: "#<Foo:0x0000000006534e80>" (String)
637
+ # With custom addition: #<Foo:0x0000000006473bb8 @bar=0, @baz=1> (Foo)
638
+ #
639
+ module JSON
640
+ require 'json/version'
641
+ require 'json/ext'
642
+ end