json 2.15.2 → 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:
@@ -434,6 +439,8 @@ module JSON
434
439
 
435
440
  # Return the value returned by method +name+.
436
441
  def [](name)
442
+ ::JSON.deprecation_warning("JSON::State#[] is deprecated and will be removed in json 3.0.0")
443
+
437
444
  if respond_to?(name)
438
445
  __send__(name)
439
446
  else
@@ -443,6 +450,8 @@ module JSON
443
450
  end
444
451
 
445
452
  def []=(name, value)
453
+ ::JSON.deprecation_warning("JSON::State#[]= is deprecated and will be removed in json 3.0.0")
454
+
446
455
  if respond_to?(name_writer = "#{name}=")
447
456
  __send__ name_writer, value
448
457
  else
@@ -521,13 +530,17 @@ module JSON
521
530
  end
522
531
  result << state.indent * depth if indent
523
532
 
524
- if state.strict? && !Generator.native_key?(key)
525
- if state.as_json
533
+ if state.strict?
534
+ if state.as_json && (!Generator.native_key?(key) || !Generator.valid_encoding?(key))
526
535
  key = state.as_json.call(key, true)
527
536
  end
528
537
 
529
538
  unless Generator.native_key?(key)
530
- 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)
531
544
  end
532
545
  end
533
546
 
@@ -674,14 +687,25 @@ module JSON
674
687
  # \u????.
675
688
  def to_json(state = nil, *args)
676
689
  state = State.from_state(state)
677
- if encoding == ::Encoding::UTF_8
678
- 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?
679
703
  raise GeneratorError.new("source sequence is illegal/malformed utf-8", self)
680
704
  end
681
- string = self
682
705
  else
683
- string = encode(::Encoding::UTF_8)
706
+ string = string.encode(::Encoding::UTF_8)
684
707
  end
708
+
685
709
  if state.ascii_only?
686
710
  %("#{JSON::TruffleRuby::Generator.utf8_to_json_ascii(string, state.script_safe)}")
687
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.2'
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.2
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