json 2.11.3 → 2.21.2
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 +194 -8
- data/LEGAL +12 -0
- data/README.md +44 -4
- data/ext/json/ext/fbuffer/fbuffer.h +99 -76
- data/ext/json/ext/generator/extconf.rb +9 -0
- data/ext/json/ext/generator/generator.c +788 -563
- data/ext/json/ext/json.h +183 -0
- data/ext/json/ext/parser/extconf.rb +34 -2
- data/ext/json/ext/parser/parser.c +2254 -695
- data/ext/json/ext/simd/conf.rb +24 -0
- data/ext/json/ext/simd/simd.h +208 -0
- data/ext/json/ext/vendor/fast_float_parser.h +814 -0
- data/ext/json/ext/vendor/fpconv.c +20 -19
- data/json.gemspec +2 -3
- data/lib/json/add/core.rb +1 -0
- data/lib/json/add/string.rb +35 -0
- data/lib/json/common.rb +135 -55
- data/lib/json/ext/generator/state.rb +12 -14
- data/lib/json/ext.rb +28 -2
- data/lib/json/generic_object.rb +0 -8
- data/lib/json/truffle_ruby/generator.rb +172 -72
- data/lib/json/version.rb +1 -1
- data/lib/json.rb +122 -5
- metadata +8 -3
|
@@ -47,6 +47,19 @@ module JSON
|
|
|
47
47
|
|
|
48
48
|
SCRIPT_SAFE_ESCAPE_PATTERN = /[\/"\\\x0-\x1f\u2028-\u2029]/
|
|
49
49
|
|
|
50
|
+
def self.native_type?(value) # :nodoc:
|
|
51
|
+
(false == value || true == value || nil == value || String === value || Symbol === value || Array === value || Hash === value || Integer === value || Float === value || Fragment === value)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def self.native_key?(key) # :nodoc:
|
|
55
|
+
(Symbol === key || String === key)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def self.valid_encoding?(string) # :nodoc:
|
|
59
|
+
return false unless string.encoding == ::Encoding::UTF_8 || string.encoding == ::Encoding::US_ASCII
|
|
60
|
+
string.is_a?(Symbol) || string.valid_encoding?
|
|
61
|
+
end
|
|
62
|
+
|
|
50
63
|
# Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with
|
|
51
64
|
# UTF16 big endian characters as \u????, and return it.
|
|
52
65
|
def self.utf8_to_json(string, script_safe = false) # :nodoc:
|
|
@@ -98,6 +111,8 @@ module JSON
|
|
|
98
111
|
# This class is used to create State instances, that are use to hold data
|
|
99
112
|
# while generating a JSON text from a Ruby data structure.
|
|
100
113
|
class State
|
|
114
|
+
singleton_class.attr_accessor :default_sort_keys_proc # :nodoc:
|
|
115
|
+
|
|
101
116
|
def self.generate(obj, opts = nil, io = nil)
|
|
102
117
|
new(opts).generate(obj, io)
|
|
103
118
|
end
|
|
@@ -151,6 +166,7 @@ module JSON
|
|
|
151
166
|
@script_safe = false
|
|
152
167
|
@strict = false
|
|
153
168
|
@max_nesting = 100
|
|
169
|
+
@sort_keys = false
|
|
154
170
|
configure(opts) if opts
|
|
155
171
|
end
|
|
156
172
|
|
|
@@ -186,6 +202,33 @@ module JSON
|
|
|
186
202
|
# supported by the JSON spec will raise a JSON::GeneratorError
|
|
187
203
|
attr_accessor :strict
|
|
188
204
|
|
|
205
|
+
# Controls key sorting in the generated JSON. If set to +true+, object
|
|
206
|
+
# keys are sorted by key lexicographically. If set to a Proc, it
|
|
207
|
+
# receives the entire Hash and must return a Hash with its pairs in the
|
|
208
|
+
# desired order.
|
|
209
|
+
attr_reader :sort_keys
|
|
210
|
+
|
|
211
|
+
def sort_keys=(value) # :nodoc:
|
|
212
|
+
type_error = false
|
|
213
|
+
@sort_keys = case value
|
|
214
|
+
when Proc
|
|
215
|
+
value
|
|
216
|
+
when true
|
|
217
|
+
State.default_sort_keys_proc
|
|
218
|
+
when nil, false
|
|
219
|
+
false
|
|
220
|
+
else
|
|
221
|
+
type_error = true
|
|
222
|
+
false
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
if type_error
|
|
226
|
+
raise TypeError, "The `sort_keys` argument must be a boolean or a Proc"
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
@sort_keys
|
|
230
|
+
end
|
|
231
|
+
|
|
189
232
|
# :stopdoc:
|
|
190
233
|
attr_reader :buffer_initial_length
|
|
191
234
|
|
|
@@ -198,13 +241,20 @@ module JSON
|
|
|
198
241
|
|
|
199
242
|
# This integer returns the current depth data structure nesting in the
|
|
200
243
|
# generated JSON.
|
|
201
|
-
|
|
244
|
+
attr_reader :depth
|
|
245
|
+
|
|
246
|
+
def depth=(depth)
|
|
247
|
+
if depth.negative?
|
|
248
|
+
raise ArgumentError, "depth must be >= 0 (got #{depth})"
|
|
249
|
+
end
|
|
250
|
+
@depth = depth
|
|
251
|
+
end
|
|
202
252
|
|
|
203
253
|
def check_max_nesting # :nodoc:
|
|
204
254
|
return if @max_nesting.zero?
|
|
205
255
|
current_nesting = depth + 1
|
|
206
256
|
current_nesting > @max_nesting and
|
|
207
|
-
raise NestingError, "nesting of #{current_nesting} is too deep"
|
|
257
|
+
raise NestingError, "nesting of #{current_nesting} is too deep. Did you try to serialize objects with circular references?"
|
|
208
258
|
end
|
|
209
259
|
|
|
210
260
|
# Returns true, if circular data structures are checked,
|
|
@@ -247,6 +297,11 @@ module JSON
|
|
|
247
297
|
else
|
|
248
298
|
raise TypeError, "can't convert #{opts.class} into Hash"
|
|
249
299
|
end
|
|
300
|
+
|
|
301
|
+
if opts[:depth]&.negative?
|
|
302
|
+
raise ArgumentError, "depth must be >= 0 (got #{opts[:depth]})"
|
|
303
|
+
end
|
|
304
|
+
|
|
250
305
|
opts.each do |key, value|
|
|
251
306
|
instance_variable_set "@#{key}", value
|
|
252
307
|
end
|
|
@@ -260,6 +315,7 @@ module JSON
|
|
|
260
315
|
@allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan)
|
|
261
316
|
@as_json = opts[:as_json].to_proc if opts[:as_json]
|
|
262
317
|
@ascii_only = opts[:ascii_only] if opts.key?(:ascii_only)
|
|
318
|
+
self.sort_keys = opts[:sort_keys] if opts.key?(:sort_keys)
|
|
263
319
|
@depth = opts[:depth] || 0
|
|
264
320
|
@buffer_initial_length ||= opts[:buffer_initial_length]
|
|
265
321
|
|
|
@@ -271,11 +327,20 @@ module JSON
|
|
|
271
327
|
false
|
|
272
328
|
end
|
|
273
329
|
|
|
330
|
+
if opts.key?(:allow_duplicate_key)
|
|
331
|
+
@allow_duplicate_key = !!opts[:allow_duplicate_key]
|
|
332
|
+
else
|
|
333
|
+
@allow_duplicate_key = nil # nil is deprecation
|
|
334
|
+
end
|
|
335
|
+
|
|
274
336
|
@strict = !!opts[:strict] if opts.key?(:strict)
|
|
275
337
|
|
|
276
338
|
if !opts.key?(:max_nesting) # defaults to 100
|
|
277
339
|
@max_nesting = 100
|
|
278
340
|
elsif opts[:max_nesting]
|
|
341
|
+
unless opts[:max_nesting].is_a?(Integer)
|
|
342
|
+
raise TypeError, ":max_nesting must be an Integer, got: #{opts[:max_nesting].class}"
|
|
343
|
+
end
|
|
279
344
|
@max_nesting = opts[:max_nesting]
|
|
280
345
|
else
|
|
281
346
|
@max_nesting = 0
|
|
@@ -284,14 +349,23 @@ module JSON
|
|
|
284
349
|
end
|
|
285
350
|
alias merge configure
|
|
286
351
|
|
|
352
|
+
def allow_duplicate_key? # :nodoc:
|
|
353
|
+
@allow_duplicate_key
|
|
354
|
+
end
|
|
355
|
+
|
|
287
356
|
# Returns the configuration instance variables as a hash, that can be
|
|
288
357
|
# passed to the configure method.
|
|
289
358
|
def to_h
|
|
290
359
|
result = {}
|
|
291
360
|
instance_variables.each do |iv|
|
|
292
|
-
|
|
293
|
-
result[
|
|
361
|
+
key = iv.to_s[1..-1]
|
|
362
|
+
result[key.to_sym] = instance_variable_get(iv)
|
|
294
363
|
end
|
|
364
|
+
|
|
365
|
+
if result[:allow_duplicate_key].nil?
|
|
366
|
+
result.delete(:allow_duplicate_key)
|
|
367
|
+
end
|
|
368
|
+
|
|
295
369
|
result
|
|
296
370
|
end
|
|
297
371
|
|
|
@@ -302,10 +376,17 @@ module JSON
|
|
|
302
376
|
# created this method raises a
|
|
303
377
|
# GeneratorError exception.
|
|
304
378
|
def generate(obj, anIO = nil)
|
|
379
|
+
return dup.generate(obj, anIO) if frozen?
|
|
380
|
+
|
|
381
|
+
depth = @depth
|
|
305
382
|
if @indent.empty? and @space.empty? and @space_before.empty? and @object_nl.empty? and @array_nl.empty? and
|
|
306
|
-
!@ascii_only and !@script_safe and @max_nesting == 0 and (!@strict || Symbol === obj)
|
|
383
|
+
!@ascii_only and !@script_safe and @max_nesting == 0 and (!@strict || Symbol === obj) and !@sort_keys
|
|
307
384
|
result = generate_json(obj, ''.dup)
|
|
308
385
|
else
|
|
386
|
+
if @sort_keys
|
|
387
|
+
obj = @sort_keys.call(obj)
|
|
388
|
+
end
|
|
389
|
+
|
|
309
390
|
result = obj.to_json(self)
|
|
310
391
|
end
|
|
311
392
|
JSON::TruffleRuby::Generator.valid_utf8?(result) or raise GeneratorError.new(
|
|
@@ -318,10 +399,8 @@ module JSON
|
|
|
318
399
|
else
|
|
319
400
|
result
|
|
320
401
|
end
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
def generate_new(obj, anIO = nil) # :nodoc:
|
|
324
|
-
dup.generate(obj, anIO)
|
|
402
|
+
ensure
|
|
403
|
+
@depth = depth unless frozen?
|
|
325
404
|
end
|
|
326
405
|
|
|
327
406
|
# Handles @allow_nan, @buffer_initial_length, other ivars must be the default value (see above)
|
|
@@ -330,8 +409,17 @@ module JSON
|
|
|
330
409
|
when Hash
|
|
331
410
|
buf << '{'
|
|
332
411
|
first = true
|
|
412
|
+
key_type = nil
|
|
333
413
|
obj.each_pair do |k,v|
|
|
334
|
-
|
|
414
|
+
if first
|
|
415
|
+
key_type = k.class
|
|
416
|
+
else
|
|
417
|
+
if key_type && !@allow_duplicate_key && key_type != k.class
|
|
418
|
+
key_type = nil # stop checking
|
|
419
|
+
JSON.send(:on_mixed_keys_hash, obj, !@allow_duplicate_key.nil?)
|
|
420
|
+
end
|
|
421
|
+
buf << ','
|
|
422
|
+
end
|
|
335
423
|
|
|
336
424
|
key_str = k.to_s
|
|
337
425
|
if key_str.class == String
|
|
@@ -398,6 +486,8 @@ module JSON
|
|
|
398
486
|
|
|
399
487
|
# Return the value returned by method +name+.
|
|
400
488
|
def [](name)
|
|
489
|
+
::JSON.deprecation_warning("JSON::State#[] is deprecated and will be removed in json 3.0.0")
|
|
490
|
+
|
|
401
491
|
if respond_to?(name)
|
|
402
492
|
__send__(name)
|
|
403
493
|
else
|
|
@@ -407,6 +497,8 @@ module JSON
|
|
|
407
497
|
end
|
|
408
498
|
|
|
409
499
|
def []=(name, value)
|
|
500
|
+
::JSON.deprecation_warning("JSON::State#[]= is deprecated and will be removed in json 3.0.0")
|
|
501
|
+
|
|
410
502
|
if respond_to?(name_writer = "#{name}=")
|
|
411
503
|
__send__ name_writer, value
|
|
412
504
|
else
|
|
@@ -424,10 +516,10 @@ module JSON
|
|
|
424
516
|
state = State.from_state(state) if state
|
|
425
517
|
if state&.strict?
|
|
426
518
|
value = self
|
|
427
|
-
if state.strict? && !(
|
|
519
|
+
if state.strict? && !Generator.native_type?(value)
|
|
428
520
|
if state.as_json
|
|
429
|
-
value = state.as_json.call(value)
|
|
430
|
-
unless
|
|
521
|
+
value = state.as_json.call(value, false)
|
|
522
|
+
unless Generator.native_type?(value)
|
|
431
523
|
raise GeneratorError.new("#{value.class} returned by #{state.as_json} not allowed in JSON", value)
|
|
432
524
|
end
|
|
433
525
|
value.to_json(state)
|
|
@@ -449,33 +541,54 @@ module JSON
|
|
|
449
541
|
# _depth_ is used to find out nesting depth, to indent accordingly.
|
|
450
542
|
def to_json(state = nil, *)
|
|
451
543
|
state = State.from_state(state)
|
|
544
|
+
depth = state.depth
|
|
452
545
|
state.check_max_nesting
|
|
453
546
|
json_transform(state)
|
|
547
|
+
ensure
|
|
548
|
+
state.depth = depth
|
|
454
549
|
end
|
|
455
550
|
|
|
456
551
|
private
|
|
457
552
|
|
|
458
|
-
def json_shift(state)
|
|
459
|
-
state.object_nl.empty? or return ''
|
|
460
|
-
state.indent * state.depth
|
|
461
|
-
end
|
|
462
|
-
|
|
463
553
|
def json_transform(state)
|
|
464
554
|
depth = state.depth += 1
|
|
465
555
|
|
|
466
556
|
if empty?
|
|
467
557
|
state.depth -= 1
|
|
468
|
-
return '{}'
|
|
558
|
+
return +'{}'
|
|
469
559
|
end
|
|
470
560
|
|
|
471
561
|
delim = ",#{state.object_nl}"
|
|
472
|
-
result =
|
|
562
|
+
result = "{#{state.object_nl}"
|
|
473
563
|
first = true
|
|
564
|
+
key_type = nil
|
|
474
565
|
indent = !state.object_nl.empty?
|
|
475
566
|
each { |key, value|
|
|
476
|
-
|
|
567
|
+
if first
|
|
568
|
+
key_type = key.class
|
|
569
|
+
else
|
|
570
|
+
if key_type && !state.allow_duplicate_key? && key_type != key.class
|
|
571
|
+
key_type = nil # stop checking
|
|
572
|
+
JSON.send(:on_mixed_keys_hash, self, state.allow_duplicate_key? == false)
|
|
573
|
+
end
|
|
574
|
+
result << delim
|
|
575
|
+
end
|
|
477
576
|
result << state.indent * depth if indent
|
|
478
577
|
|
|
578
|
+
if state.strict?
|
|
579
|
+
if state.as_json && (!Generator.native_key?(key) || !Generator.valid_encoding?(key))
|
|
580
|
+
key = state.as_json.call(key, true)
|
|
581
|
+
end
|
|
582
|
+
|
|
583
|
+
unless Generator.native_key?(key)
|
|
584
|
+
raise GeneratorError.new("#{key.class} not allowed as object key in JSON", key)
|
|
585
|
+
end
|
|
586
|
+
|
|
587
|
+
unless Generator.valid_encoding?(key)
|
|
588
|
+
raise GeneratorError.new("source sequence is illegal/malformed utf-8", key)
|
|
589
|
+
end
|
|
590
|
+
end
|
|
591
|
+
|
|
479
592
|
key_str = key.to_s
|
|
480
593
|
if key_str.is_a?(String)
|
|
481
594
|
key_json = key_str.to_json(state)
|
|
@@ -483,25 +596,27 @@ module JSON
|
|
|
483
596
|
raise TypeError, "#{key.class}#to_s returns an instance of #{key_str.class}, expected a String"
|
|
484
597
|
end
|
|
485
598
|
|
|
486
|
-
result =
|
|
487
|
-
if state.strict? && !(
|
|
599
|
+
result = "#{result}#{key_json}#{state.space_before}:#{state.space}"
|
|
600
|
+
if state.strict? && !Generator.native_type?(value)
|
|
488
601
|
if state.as_json
|
|
489
|
-
value = state.as_json.call(value)
|
|
490
|
-
unless
|
|
602
|
+
value = state.as_json.call(value, false)
|
|
603
|
+
unless Generator.native_type?(value)
|
|
491
604
|
raise GeneratorError.new("#{value.class} returned by #{state.as_json} not allowed in JSON", value)
|
|
492
605
|
end
|
|
493
606
|
result << value.to_json(state)
|
|
607
|
+
state.depth = depth
|
|
494
608
|
else
|
|
495
609
|
raise GeneratorError.new("#{value.class} not allowed in JSON", value)
|
|
496
610
|
end
|
|
497
611
|
elsif value.respond_to?(:to_json)
|
|
498
612
|
result << value.to_json(state)
|
|
613
|
+
state.depth = depth
|
|
499
614
|
else
|
|
500
615
|
result << %{"#{String(value)}"}
|
|
501
616
|
end
|
|
502
617
|
first = false
|
|
503
618
|
}
|
|
504
|
-
depth
|
|
619
|
+
depth -= 1
|
|
505
620
|
unless first
|
|
506
621
|
result << state.object_nl
|
|
507
622
|
result << state.indent * depth if indent
|
|
@@ -518,8 +633,11 @@ module JSON
|
|
|
518
633
|
# produced JSON string output further.
|
|
519
634
|
def to_json(state = nil, *)
|
|
520
635
|
state = State.from_state(state)
|
|
636
|
+
depth = state.depth
|
|
521
637
|
state.check_max_nesting
|
|
522
638
|
json_transform(state)
|
|
639
|
+
ensure
|
|
640
|
+
state.depth = depth
|
|
523
641
|
end
|
|
524
642
|
|
|
525
643
|
private
|
|
@@ -529,7 +647,7 @@ module JSON
|
|
|
529
647
|
|
|
530
648
|
if empty?
|
|
531
649
|
state.depth -= 1
|
|
532
|
-
return '[]'
|
|
650
|
+
return +'[]'
|
|
533
651
|
end
|
|
534
652
|
|
|
535
653
|
result = '['.dup
|
|
@@ -545,10 +663,10 @@ module JSON
|
|
|
545
663
|
each { |value|
|
|
546
664
|
result << delim unless first
|
|
547
665
|
result << state.indent * depth if indent
|
|
548
|
-
if state.strict? && !(
|
|
666
|
+
if state.strict? && !Generator.native_type?(value)
|
|
549
667
|
if state.as_json
|
|
550
|
-
value = state.as_json.call(value)
|
|
551
|
-
unless
|
|
668
|
+
value = state.as_json.call(value, false)
|
|
669
|
+
unless Generator.native_type?(value)
|
|
552
670
|
raise GeneratorError.new("#{value.class} returned by #{state.as_json} not allowed in JSON", value)
|
|
553
671
|
end
|
|
554
672
|
result << value.to_json(state)
|
|
@@ -557,12 +675,13 @@ module JSON
|
|
|
557
675
|
end
|
|
558
676
|
elsif value.respond_to?(:to_json)
|
|
559
677
|
result << value.to_json(state)
|
|
678
|
+
state.depth = depth
|
|
560
679
|
else
|
|
561
680
|
result << %{"#{String(value)}"}
|
|
562
681
|
end
|
|
563
682
|
first = false
|
|
564
683
|
}
|
|
565
|
-
depth
|
|
684
|
+
depth -= 1
|
|
566
685
|
result << state.array_nl
|
|
567
686
|
result << state.indent * depth if indent
|
|
568
687
|
result << ']'
|
|
@@ -582,11 +701,14 @@ module JSON
|
|
|
582
701
|
if state.allow_nan?
|
|
583
702
|
to_s
|
|
584
703
|
elsif state.strict? && state.as_json
|
|
585
|
-
casted_value = state.as_json.call(self)
|
|
704
|
+
casted_value = state.as_json.call(self, false)
|
|
586
705
|
|
|
587
706
|
if casted_value.equal?(self)
|
|
588
707
|
raise GeneratorError.new("#{self} not allowed in JSON", self)
|
|
589
708
|
end
|
|
709
|
+
unless Generator.native_type?(casted_value)
|
|
710
|
+
raise GeneratorError.new("#{casted_value.class} returned by #{state.as_json} not allowed in JSON", casted_value)
|
|
711
|
+
end
|
|
590
712
|
|
|
591
713
|
state.check_max_nesting
|
|
592
714
|
state.depth += 1
|
|
@@ -619,14 +741,25 @@ module JSON
|
|
|
619
741
|
# \u????.
|
|
620
742
|
def to_json(state = nil, *args)
|
|
621
743
|
state = State.from_state(state)
|
|
622
|
-
|
|
623
|
-
|
|
744
|
+
string = self
|
|
745
|
+
|
|
746
|
+
if state.strict? && state.as_json
|
|
747
|
+
unless Generator.valid_encoding?(string)
|
|
748
|
+
string = state.as_json.call(string, false)
|
|
749
|
+
unless string.is_a?(::String)
|
|
750
|
+
return string.to_json(state, *args)
|
|
751
|
+
end
|
|
752
|
+
end
|
|
753
|
+
end
|
|
754
|
+
|
|
755
|
+
if string.encoding == ::Encoding::UTF_8
|
|
756
|
+
unless string.valid_encoding?
|
|
624
757
|
raise GeneratorError.new("source sequence is illegal/malformed utf-8", self)
|
|
625
758
|
end
|
|
626
|
-
string = self
|
|
627
759
|
else
|
|
628
|
-
string = encode(::Encoding::UTF_8)
|
|
760
|
+
string = string.encode(::Encoding::UTF_8)
|
|
629
761
|
end
|
|
762
|
+
|
|
630
763
|
if state.ascii_only?
|
|
631
764
|
%("#{JSON::TruffleRuby::Generator.utf8_to_json_ascii(string, state.script_safe)}")
|
|
632
765
|
else
|
|
@@ -635,54 +768,21 @@ module JSON
|
|
|
635
768
|
rescue Encoding::UndefinedConversionError => error
|
|
636
769
|
raise ::JSON::GeneratorError.new(error.message, self)
|
|
637
770
|
end
|
|
638
|
-
|
|
639
|
-
# Module that holds the extending methods if, the String module is
|
|
640
|
-
# included.
|
|
641
|
-
module Extend
|
|
642
|
-
# Raw Strings are JSON Objects (the raw bytes are stored in an
|
|
643
|
-
# array for the key "raw"). The Ruby String can be created by this
|
|
644
|
-
# module method.
|
|
645
|
-
def json_create(o)
|
|
646
|
-
o['raw'].pack('C*')
|
|
647
|
-
end
|
|
648
|
-
end
|
|
649
|
-
|
|
650
|
-
# Extends _modul_ with the String::Extend module.
|
|
651
|
-
def self.included(modul)
|
|
652
|
-
modul.extend Extend
|
|
653
|
-
end
|
|
654
|
-
|
|
655
|
-
# This method creates a raw object hash, that can be nested into
|
|
656
|
-
# other data structures and will be unparsed as a raw string. This
|
|
657
|
-
# method should be used, if you want to convert raw strings to JSON
|
|
658
|
-
# instead of UTF-8 strings, e. g. binary data.
|
|
659
|
-
def to_json_raw_object
|
|
660
|
-
{
|
|
661
|
-
JSON.create_id => self.class.name,
|
|
662
|
-
'raw' => self.unpack('C*'),
|
|
663
|
-
}
|
|
664
|
-
end
|
|
665
|
-
|
|
666
|
-
# This method creates a JSON text from the result of
|
|
667
|
-
# a call to to_json_raw_object of this String.
|
|
668
|
-
def to_json_raw(*args)
|
|
669
|
-
to_json_raw_object.to_json(*args)
|
|
670
|
-
end
|
|
671
771
|
end
|
|
672
772
|
|
|
673
773
|
module TrueClass
|
|
674
774
|
# Returns a JSON string for true: 'true'.
|
|
675
|
-
def to_json(*) 'true' end
|
|
775
|
+
def to_json(*) +'true' end
|
|
676
776
|
end
|
|
677
777
|
|
|
678
778
|
module FalseClass
|
|
679
779
|
# Returns a JSON string for false: 'false'.
|
|
680
|
-
def to_json(*) 'false' end
|
|
780
|
+
def to_json(*) +'false' end
|
|
681
781
|
end
|
|
682
782
|
|
|
683
783
|
module NilClass
|
|
684
784
|
# Returns a JSON string for nil: 'null'.
|
|
685
|
-
def to_json(*) 'null' end
|
|
785
|
+
def to_json(*) +'null' end
|
|
686
786
|
end
|
|
687
787
|
end
|
|
688
788
|
end
|
data/lib/json/version.rb
CHANGED
data/lib/json.rb
CHANGED
|
@@ -6,6 +6,15 @@ require 'json/common'
|
|
|
6
6
|
#
|
|
7
7
|
# \JSON is a lightweight data-interchange format.
|
|
8
8
|
#
|
|
9
|
+
# \JSON is easy for us humans to read and write,
|
|
10
|
+
# and equally simple for machines to read (parse) and write (generate).
|
|
11
|
+
#
|
|
12
|
+
# \JSON is language-independent, making it an ideal interchange format
|
|
13
|
+
# for applications in differing programming languages
|
|
14
|
+
# and on differing operating systems.
|
|
15
|
+
#
|
|
16
|
+
# == \JSON Values
|
|
17
|
+
#
|
|
9
18
|
# A \JSON value is one of the following:
|
|
10
19
|
# - Double-quoted text: <tt>"foo"</tt>.
|
|
11
20
|
# - Number: +1+, +1.0+, +2.0e2+.
|
|
@@ -112,9 +121,11 @@ require 'json/common'
|
|
|
112
121
|
# ====== Input Options
|
|
113
122
|
#
|
|
114
123
|
# Option +max_nesting+ (\Integer) specifies the maximum nesting depth allowed;
|
|
115
|
-
# 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.
|
|
116
127
|
#
|
|
117
|
-
# With the default, +
|
|
128
|
+
# With the default, +100+:
|
|
118
129
|
# source = '[0, [1, [2, [3]]]]'
|
|
119
130
|
# ruby = JSON.parse(source)
|
|
120
131
|
# ruby # => [0, [1, [2, [3]]]]
|
|
@@ -127,6 +138,24 @@ require 'json/common'
|
|
|
127
138
|
#
|
|
128
139
|
# ---
|
|
129
140
|
#
|
|
141
|
+
# Option +allow_duplicate_key+ specifies whether duplicate keys in objects
|
|
142
|
+
# should be ignored or cause an error to be raised:
|
|
143
|
+
#
|
|
144
|
+
# When not specified:
|
|
145
|
+
# # The last value is used and a deprecation warning emitted.
|
|
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`
|
|
149
|
+
#
|
|
150
|
+
# When set to +true+:
|
|
151
|
+
# # 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)
|
|
156
|
+
#
|
|
157
|
+
# ---
|
|
158
|
+
#
|
|
130
159
|
# Option +allow_nan+ (boolean) specifies whether to allow
|
|
131
160
|
# NaN, Infinity, and MinusInfinity in +source+;
|
|
132
161
|
# defaults to +false+.
|
|
@@ -143,8 +172,61 @@ require 'json/common'
|
|
|
143
172
|
# ruby = JSON.parse(source, {allow_nan: true})
|
|
144
173
|
# ruby # => [NaN, Infinity, -Infinity]
|
|
145
174
|
#
|
|
175
|
+
# ---
|
|
176
|
+
#
|
|
177
|
+
# Option +allow_trailing_comma+ (boolean) specifies whether to allow
|
|
178
|
+
# trailing commas in objects and arrays;
|
|
179
|
+
# defaults to +false+.
|
|
180
|
+
#
|
|
181
|
+
# With the default, +false+:
|
|
182
|
+
# JSON.parse('[1,]') # unexpected character: ']' at line 1 column 4 (JSON::ParserError)
|
|
183
|
+
#
|
|
184
|
+
# When enabled:
|
|
185
|
+
# JSON.parse('[1,]', allow_trailing_comma: true) # => [1]
|
|
186
|
+
#
|
|
187
|
+
# ---
|
|
188
|
+
#
|
|
189
|
+
# Option +allow_comments+ (boolean) specifies whether to allow
|
|
190
|
+
# JavaScript style comments (either <tt>// comment</tt> or <tt>/* comment */</tt>);
|
|
191
|
+
# defaults to +false+.
|
|
192
|
+
#
|
|
193
|
+
# When not specified, a deprecation warning is emitted if a comment is encountered.
|
|
194
|
+
#
|
|
195
|
+
# 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)
|
|
200
|
+
#
|
|
201
|
+
# ---
|
|
202
|
+
#
|
|
203
|
+
# Option +allow_control_characters+ (boolean) specifies whether to allow
|
|
204
|
+
# unescaped ASCII control characters, such as newlines, in strings;
|
|
205
|
+
# defaults to +false+.
|
|
206
|
+
#
|
|
207
|
+
# With the default, +false+:
|
|
208
|
+
# JSON.parse(%{"Hello\nWorld"}) # invalid ASCII control character in string (JSON::ParserError)
|
|
209
|
+
#
|
|
210
|
+
# When enabled:
|
|
211
|
+
# JSON.parse(%{"Hello\nWorld"}, allow_control_characters: true) # => "Hello\nWorld"
|
|
212
|
+
#
|
|
213
|
+
# ---
|
|
214
|
+
#
|
|
215
|
+
# Option +allow_invalid_escape+ (boolean) specifies whether to ignore backslahes that are followed
|
|
216
|
+
# by an invalid escape character in strings;
|
|
217
|
+
# defaults to +false+.
|
|
218
|
+
#
|
|
219
|
+
# With the default, +false+:
|
|
220
|
+
# JSON.parse('"Hell\o"') # invalid escape character in string (JSON::ParserError)
|
|
221
|
+
#
|
|
222
|
+
# When enabled:
|
|
223
|
+
# JSON.parse('"Hell\o"', allow_invalid_escape: true) # => "Hello"
|
|
224
|
+
#
|
|
146
225
|
# ====== Output Options
|
|
147
226
|
#
|
|
227
|
+
# Option +freeze+ (boolean) specifies whether the returned objects will be frozen;
|
|
228
|
+
# defaults to +false+.
|
|
229
|
+
#
|
|
148
230
|
# Option +symbolize_names+ (boolean) specifies whether returned \Hash keys
|
|
149
231
|
# should be Symbols;
|
|
150
232
|
# defaults to +false+ (use Strings).
|
|
@@ -269,8 +351,27 @@ require 'json/common'
|
|
|
269
351
|
# JSON.generate(JSON::MinusInfinity)
|
|
270
352
|
#
|
|
271
353
|
# Allow:
|
|
272
|
-
# ruby = [Float::
|
|
273
|
-
# JSON.generate(ruby, allow_nan: true) # => '[NaN,Infinity,-Infinity]'
|
|
354
|
+
# ruby = [Float::NAN, Float::INFINITY, JSON::NaN, JSON::Infinity, JSON::MinusInfinity]
|
|
355
|
+
# JSON.generate(ruby, allow_nan: true) # => '[NaN,Infinity,NaN,Infinity,-Infinity]'
|
|
356
|
+
#
|
|
357
|
+
# ---
|
|
358
|
+
#
|
|
359
|
+
# Option +allow_duplicate_key+ (boolean) specifies whether
|
|
360
|
+
# hashes with duplicate keys should be allowed or produce an error.
|
|
361
|
+
# defaults to emit a deprecation warning.
|
|
362
|
+
#
|
|
363
|
+
# With the default, (not set):
|
|
364
|
+
# Warning[:deprecated] = true
|
|
365
|
+
# 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
|
+
# # detected duplicate key "foo" in {foo: 1, "foo" => 2} (JSON::GeneratorError)
|
|
373
|
+
#
|
|
374
|
+
# In version 3.0, <tt>false</tt> will become the default.
|
|
274
375
|
#
|
|
275
376
|
# ---
|
|
276
377
|
#
|
|
@@ -285,6 +386,15 @@ require 'json/common'
|
|
|
285
386
|
# # Raises JSON::NestingError (nesting of 2 is too deep):
|
|
286
387
|
# JSON.generate(obj, max_nesting: 2)
|
|
287
388
|
#
|
|
389
|
+
# With +false+:
|
|
390
|
+
# obj = []
|
|
391
|
+
# obj[0] = obj
|
|
392
|
+
# # Raises SystemStackError: stack level too deep
|
|
393
|
+
# JSON.generate(obj, max_nesting: false)
|
|
394
|
+
#
|
|
395
|
+
# Setting +max_nesting+ to +false+ can lead to a stackoverflow and may leave the program
|
|
396
|
+
# in an unrecoverable state. It is discouraged.
|
|
397
|
+
#
|
|
288
398
|
# ====== Escaping Options
|
|
289
399
|
#
|
|
290
400
|
# Options +script_safe+ (boolean) specifies wether <tt>'\u2028'</tt>, <tt>'\u2029'</tt>
|
|
@@ -309,7 +419,6 @@ require 'json/common'
|
|
|
309
419
|
# to be inserted after each \JSON object; defaults to the empty \String, <tt>''</tt>.
|
|
310
420
|
# - Option +indent+ (\String) specifies the string (usually spaces) to be
|
|
311
421
|
# used for indentation; defaults to the empty \String, <tt>''</tt>;
|
|
312
|
-
# defaults to the empty \String, <tt>''</tt>;
|
|
313
422
|
# has no effect unless options +array_nl+ or +object_nl+ specify newlines.
|
|
314
423
|
# - Option +space+ (\String) specifies a string (usually a space) to be
|
|
315
424
|
# inserted after the colon in each \JSON object's pair;
|
|
@@ -317,6 +426,11 @@ require 'json/common'
|
|
|
317
426
|
# - Option +space_before+ (\String) specifies a string (usually a space) to be
|
|
318
427
|
# inserted before the colon in each \JSON object's pair;
|
|
319
428
|
# defaults to the empty \String, <tt>''</tt>.
|
|
429
|
+
# - Option +sort_keys+ (boolean or \Proc) controls whether and how the keys of a
|
|
430
|
+
# hash are sorted when generating the output; defaults to <tt>false</tt>.
|
|
431
|
+
# When +true+, keys are sorted lexicographically. When a \Proc, it receives
|
|
432
|
+
# the entire \Hash and must return a \Hash with its pairs in the desired
|
|
433
|
+
# order, allowing for arbitrary sort orders.
|
|
320
434
|
#
|
|
321
435
|
# In this example, +obj+ is used first to generate the shortest
|
|
322
436
|
# \JSON data (no whitespace), then again with all formatting options
|
|
@@ -351,6 +465,9 @@ require 'json/common'
|
|
|
351
465
|
#
|
|
352
466
|
# == \JSON Additions
|
|
353
467
|
#
|
|
468
|
+
# Note that JSON Additions must only be used with trusted data, and is
|
|
469
|
+
# deprecated.
|
|
470
|
+
#
|
|
354
471
|
# When you "round trip" a non-\String object from Ruby to \JSON and back,
|
|
355
472
|
# you have a new \String, instead of the object you began with:
|
|
356
473
|
# ruby0 = Range.new(0, 2)
|