json 2.15.1 → 2.17.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/common.rb CHANGED
@@ -71,8 +71,13 @@ module JSON
71
71
  end
72
72
  when object_class
73
73
  if opts[:create_additions] != false
74
- if class_name = object[JSON.create_id]
75
- klass = JSON.deep_const_get(class_name)
74
+ if class_path = object[JSON.create_id]
75
+ klass = begin
76
+ Object.const_get(class_path)
77
+ rescue NameError => e
78
+ raise ArgumentError, "can't get const #{class_path}: #{e}"
79
+ end
80
+
76
81
  if klass.respond_to?(:json_creatable?) ? klass.json_creatable? : klass.respond_to?(:json_create)
77
82
  create_additions_warning if create_additions.nil?
78
83
  object = klass.json_create(object)
@@ -147,16 +152,6 @@ module JSON
147
152
  const_set :Parser, parser
148
153
  end
149
154
 
150
- # Return the constant located at _path_. The format of _path_ has to be
151
- # either ::A::B::C or A::B::C. In any case, A has to be located at the top
152
- # level (absolute namespace path?). If there doesn't exist a constant at
153
- # the given path, an ArgumentError is raised.
154
- def deep_const_get(path) # :nodoc:
155
- Object.const_get(path)
156
- rescue NameError => e
157
- raise ArgumentError, "can't get const #{path}: #{e}"
158
- end
159
-
160
155
  # Set the module _generator_ to be used by JSON.
161
156
  def generator=(generator) # :nodoc:
162
157
  old, $VERBOSE = $VERBOSE, nil
@@ -555,6 +550,7 @@ module JSON
555
550
  :create_additions => nil,
556
551
  }
557
552
  # :call-seq:
553
+ # JSON.unsafe_load(source, options = {}) -> object
558
554
  # JSON.unsafe_load(source, proc = nil, options = {}) -> object
559
555
  #
560
556
  # Returns the Ruby objects created by parsing the given +source+.
@@ -686,7 +682,12 @@ module JSON
686
682
  #
687
683
  def unsafe_load(source, proc = nil, options = nil)
688
684
  opts = if options.nil?
689
- _unsafe_load_default_options
685
+ if proc && proc.is_a?(Hash)
686
+ options, proc = proc, nil
687
+ options
688
+ else
689
+ _unsafe_load_default_options
690
+ end
690
691
  else
691
692
  _unsafe_load_default_options.merge(options)
692
693
  end
@@ -714,6 +715,7 @@ module JSON
714
715
  end
715
716
 
716
717
  # :call-seq:
718
+ # JSON.load(source, options = {}) -> object
717
719
  # JSON.load(source, proc = nil, options = {}) -> object
718
720
  #
719
721
  # Returns the Ruby objects created by parsing the given +source+.
@@ -850,8 +852,18 @@ module JSON
850
852
  # @attributes={"type"=>"Admin", "password"=>"0wn3d"}>}
851
853
  #
852
854
  def load(source, proc = nil, options = nil)
855
+ if proc && options.nil? && proc.is_a?(Hash)
856
+ options = proc
857
+ proc = nil
858
+ end
859
+
853
860
  opts = if options.nil?
854
- _load_default_options
861
+ if proc && proc.is_a?(Hash)
862
+ options, proc = proc, nil
863
+ options
864
+ else
865
+ _load_default_options
866
+ end
855
867
  else
856
868
  _load_default_options.merge(options)
857
869
  end
@@ -1053,7 +1065,7 @@ module JSON
1053
1065
  options[:as_json] = as_json if as_json
1054
1066
 
1055
1067
  @state = State.new(options).freeze
1056
- @parser_config = Ext::Parser::Config.new(ParserOptions.prepare(options))
1068
+ @parser_config = Ext::Parser::Config.new(ParserOptions.prepare(options)).freeze
1057
1069
  end
1058
1070
 
1059
1071
  # call-seq:
@@ -1062,7 +1074,7 @@ module JSON
1062
1074
  #
1063
1075
  # Serialize the given object into a \JSON document.
1064
1076
  def dump(object, io = nil)
1065
- @state.generate_new(object, io)
1077
+ @state.generate(object, io)
1066
1078
  end
1067
1079
  alias_method :generate, :dump
1068
1080
 
@@ -75,6 +75,8 @@ module JSON
75
75
  #
76
76
  # Returns the value returned by method +name+.
77
77
  def [](name)
78
+ ::JSON.deprecation_warning("JSON::State#[] is deprecated and will be removed in json 3.0.0")
79
+
78
80
  if respond_to?(name)
79
81
  __send__(name)
80
82
  else
@@ -87,6 +89,8 @@ module JSON
87
89
  #
88
90
  # Sets the attribute name to value.
89
91
  def []=(name, value)
92
+ ::JSON.deprecation_warning("JSON::State#[]= is deprecated and will be removed in json 3.0.0")
93
+
90
94
  if respond_to?(name_writer = "#{name}=")
91
95
  __send__ name_writer, value
92
96
  else
@@ -55,6 +55,11 @@ module JSON
55
55
  (Symbol === key || String === key)
56
56
  end
57
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
+
58
63
  # Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with
59
64
  # UTF16 big endian characters as \u????, and return it.
60
65
  def self.utf8_to_json(string, script_safe = false) # :nodoc:
@@ -212,7 +217,7 @@ module JSON
212
217
  return if @max_nesting.zero?
213
218
  current_nesting = depth + 1
214
219
  current_nesting > @max_nesting and
215
- raise NestingError, "nesting of #{current_nesting} is too deep"
220
+ raise NestingError, "nesting of #{current_nesting} is too deep. Did you try to serialize objects with circular references?"
216
221
  end
217
222
 
218
223
  # Returns true, if circular data structures are checked,
@@ -307,8 +312,8 @@ module JSON
307
312
  def to_h
308
313
  result = {}
309
314
  instance_variables.each do |iv|
310
- iv = iv.to_s[1..-1]
311
- result[iv.to_sym] = self[iv]
315
+ key = iv.to_s[1..-1]
316
+ result[key.to_sym] = instance_variable_get(iv)
312
317
  end
313
318
 
314
319
  if result[:allow_duplicate_key].nil?
@@ -325,6 +330,9 @@ module JSON
325
330
  # created this method raises a
326
331
  # GeneratorError exception.
327
332
  def generate(obj, anIO = nil)
333
+ return dup.generate(obj, anIO) if frozen?
334
+
335
+ depth = @depth
328
336
  if @indent.empty? and @space.empty? and @space_before.empty? and @object_nl.empty? and @array_nl.empty? and
329
337
  !@ascii_only and !@script_safe and @max_nesting == 0 and (!@strict || Symbol === obj)
330
338
  result = generate_json(obj, ''.dup)
@@ -341,10 +349,8 @@ module JSON
341
349
  else
342
350
  result
343
351
  end
344
- end
345
-
346
- def generate_new(obj, anIO = nil) # :nodoc:
347
- dup.generate(obj, anIO)
352
+ ensure
353
+ @depth = depth unless frozen?
348
354
  end
349
355
 
350
356
  # Handles @allow_nan, @buffer_initial_length, other ivars must be the default value (see above)
@@ -430,6 +436,8 @@ module JSON
430
436
 
431
437
  # Return the value returned by method +name+.
432
438
  def [](name)
439
+ ::JSON.deprecation_warning("JSON::State#[] is deprecated and will be removed in json 3.0.0")
440
+
433
441
  if respond_to?(name)
434
442
  __send__(name)
435
443
  else
@@ -439,6 +447,8 @@ module JSON
439
447
  end
440
448
 
441
449
  def []=(name, value)
450
+ ::JSON.deprecation_warning("JSON::State#[]= is deprecated and will be removed in json 3.0.0")
451
+
442
452
  if respond_to?(name_writer = "#{name}=")
443
453
  __send__ name_writer, value
444
454
  else
@@ -481,8 +491,11 @@ module JSON
481
491
  # _depth_ is used to find out nesting depth, to indent accordingly.
482
492
  def to_json(state = nil, *)
483
493
  state = State.from_state(state)
494
+ depth = state.depth
484
495
  state.check_max_nesting
485
496
  json_transform(state)
497
+ ensure
498
+ state.depth = depth
486
499
  end
487
500
 
488
501
  private
@@ -517,13 +530,17 @@ module JSON
517
530
  end
518
531
  result << state.indent * depth if indent
519
532
 
520
- if state.strict? && !Generator.native_key?(key)
521
- if state.as_json
533
+ if state.strict?
534
+ if state.as_json && (!Generator.native_key?(key) || !Generator.valid_encoding?(key))
522
535
  key = state.as_json.call(key, true)
523
536
  end
524
537
 
525
538
  unless Generator.native_key?(key)
526
- raise GeneratorError.new("#{key.class} not allowed as object key in JSON", value)
539
+ raise GeneratorError.new("#{key.class} not allowed as object key in JSON", key)
540
+ end
541
+
542
+ unless Generator.valid_encoding?(key)
543
+ raise GeneratorError.new("source sequence is illegal/malformed utf-8", key)
527
544
  end
528
545
  end
529
546
 
@@ -542,17 +559,19 @@ module JSON
542
559
  raise GeneratorError.new("#{value.class} returned by #{state.as_json} not allowed in JSON", value)
543
560
  end
544
561
  result << value.to_json(state)
562
+ state.depth = depth
545
563
  else
546
564
  raise GeneratorError.new("#{value.class} not allowed in JSON", value)
547
565
  end
548
566
  elsif value.respond_to?(:to_json)
549
567
  result << value.to_json(state)
568
+ state.depth = depth
550
569
  else
551
570
  result << %{"#{String(value)}"}
552
571
  end
553
572
  first = false
554
573
  }
555
- depth = state.depth -= 1
574
+ depth -= 1
556
575
  unless first
557
576
  result << state.object_nl
558
577
  result << state.indent * depth if indent
@@ -569,8 +588,11 @@ module JSON
569
588
  # produced JSON string output further.
570
589
  def to_json(state = nil, *)
571
590
  state = State.from_state(state)
591
+ depth = state.depth
572
592
  state.check_max_nesting
573
593
  json_transform(state)
594
+ ensure
595
+ state.depth = depth
574
596
  end
575
597
 
576
598
  private
@@ -608,12 +630,13 @@ module JSON
608
630
  end
609
631
  elsif value.respond_to?(:to_json)
610
632
  result << value.to_json(state)
633
+ state.depth = depth
611
634
  else
612
635
  result << %{"#{String(value)}"}
613
636
  end
614
637
  first = false
615
638
  }
616
- depth = state.depth -= 1
639
+ depth -= 1
617
640
  result << state.array_nl
618
641
  result << state.indent * depth if indent
619
642
  result << ']'
@@ -638,6 +661,9 @@ module JSON
638
661
  if casted_value.equal?(self)
639
662
  raise GeneratorError.new("#{self} not allowed in JSON", self)
640
663
  end
664
+ unless Generator.native_type?(casted_value)
665
+ raise GeneratorError.new("#{casted_value.class} returned by #{state.as_json} not allowed in JSON", casted_value)
666
+ end
641
667
 
642
668
  state.check_max_nesting
643
669
  state.depth += 1
@@ -670,14 +696,25 @@ module JSON
670
696
  # \u????.
671
697
  def to_json(state = nil, *args)
672
698
  state = State.from_state(state)
673
- if encoding == ::Encoding::UTF_8
674
- unless valid_encoding?
699
+ string = self
700
+
701
+ if state.strict? && state.as_json
702
+ unless Generator.valid_encoding?(string)
703
+ string = state.as_json.call(string, false)
704
+ unless string.is_a?(::String)
705
+ return string.to_json(state, *args)
706
+ end
707
+ end
708
+ end
709
+
710
+ if string.encoding == ::Encoding::UTF_8
711
+ unless string.valid_encoding?
675
712
  raise GeneratorError.new("source sequence is illegal/malformed utf-8", self)
676
713
  end
677
- string = self
678
714
  else
679
- string = encode(::Encoding::UTF_8)
715
+ string = string.encode(::Encoding::UTF_8)
680
716
  end
717
+
681
718
  if state.ascii_only?
682
719
  %("#{JSON::TruffleRuby::Generator.utf8_to_json_ascii(string, state.script_safe)}")
683
720
  else
data/lib/json/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JSON
4
- VERSION = '2.15.1'
4
+ VERSION = '2.17.1'
5
5
  end
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: 2.15.1
4
+ version: 2.17.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank
@@ -26,12 +26,14 @@ files:
26
26
  - ext/json/ext/fbuffer/fbuffer.h
27
27
  - ext/json/ext/generator/extconf.rb
28
28
  - ext/json/ext/generator/generator.c
29
+ - ext/json/ext/json.h
29
30
  - ext/json/ext/parser/extconf.rb
30
31
  - ext/json/ext/parser/parser.c
31
32
  - ext/json/ext/simd/conf.rb
32
33
  - ext/json/ext/simd/simd.h
33
34
  - ext/json/ext/vendor/fpconv.c
34
35
  - ext/json/ext/vendor/jeaiii-ltoa.h
36
+ - ext/json/ext/vendor/ryu.h
35
37
  - json.gemspec
36
38
  - lib/json.rb
37
39
  - lib/json/add/bigdecimal.rb