json 2.15.1 → 2.16.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.
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
@@ -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,
@@ -347,6 +352,10 @@ module JSON
347
352
  dup.generate(obj, anIO)
348
353
  end
349
354
 
355
+ private def initialize_copy(_orig)
356
+ @depth = 0
357
+ end
358
+
350
359
  # Handles @allow_nan, @buffer_initial_length, other ivars must be the default value (see above)
351
360
  private def generate_json(obj, buf)
352
361
  case obj
@@ -430,6 +439,8 @@ module JSON
430
439
 
431
440
  # Return the value returned by method +name+.
432
441
  def [](name)
442
+ ::JSON.deprecation_warning("JSON::State#[] is deprecated and will be removed in json 3.0.0")
443
+
433
444
  if respond_to?(name)
434
445
  __send__(name)
435
446
  else
@@ -439,6 +450,8 @@ module JSON
439
450
  end
440
451
 
441
452
  def []=(name, value)
453
+ ::JSON.deprecation_warning("JSON::State#[]= is deprecated and will be removed in json 3.0.0")
454
+
442
455
  if respond_to?(name_writer = "#{name}=")
443
456
  __send__ name_writer, value
444
457
  else
@@ -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
 
@@ -670,14 +687,25 @@ module JSON
670
687
  # \u????.
671
688
  def to_json(state = nil, *args)
672
689
  state = State.from_state(state)
673
- if encoding == ::Encoding::UTF_8
674
- unless valid_encoding?
690
+ string = self
691
+
692
+ if state.strict? && state.as_json
693
+ unless Generator.valid_encoding?(string)
694
+ string = state.as_json.call(string, false)
695
+ unless string.is_a?(::String)
696
+ return string.to_json(state, *args)
697
+ end
698
+ end
699
+ end
700
+
701
+ if string.encoding == ::Encoding::UTF_8
702
+ unless string.valid_encoding?
675
703
  raise GeneratorError.new("source sequence is illegal/malformed utf-8", self)
676
704
  end
677
- string = self
678
705
  else
679
- string = encode(::Encoding::UTF_8)
706
+ string = string.encode(::Encoding::UTF_8)
680
707
  end
708
+
681
709
  if state.ascii_only?
682
710
  %("#{JSON::TruffleRuby::Generator.utf8_to_json_ascii(string, state.script_safe)}")
683
711
  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.16.0'
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.16.0
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