summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean Boussier <[email protected]>2025-03-26 11:47:51 +0100
committerHiroshi SHIBATA <[email protected]>2025-03-27 11:37:27 +0900
commitf4529ecbe7adf6cc24bf8f8aa92becbc005e897a (patch)
treefeeff4f1bc07b0381e53f233290b1b34321570d0
parent46ee73f4f6a6fceba4ed6dfe6622c78462a2b1ac (diff)
[ruby/json] Deprecate all `*_default_options`
Globally changing the behavior of the library is a bad idea, as many different libraries may rely on `json` and may not expect it and likely never tested that a different default config works for them. If you need to change the behavior of JSON, it's best to do it only locally, and not globally. In addition the new `JSON::Coder` interface is much more suited for that. Another reason for the deprecation is that it's impossible to make `JSON.load` and `JSON.dump` Ractor-safe with such API. https://github.com/ruby/json/commit/172762c6e4
-rw-r--r--ext/json/lib/json/common.rb77
-rw-r--r--test/json/json_common_interface_test.rb8
2 files changed, 54 insertions, 31 deletions
diff --git a/ext/json/lib/json/common.rb b/ext/json/lib/json/common.rb
index 9094df00a5..800056c49c 100644
--- a/ext/json/lib/json/common.rb
+++ b/ext/json/lib/json/common.rb
@@ -101,6 +101,29 @@ module JSON
# Sets or Returns the JSON generator state class that is used by JSON.
attr_accessor :state
+
+ private
+
+ def deprecated_singleton_attr_accessor(*attrs)
+ args = RUBY_VERSION >= "3.0" ? ", category: :deprecated" : ""
+ attrs.each do |attr|
+ singleton_class.class_eval <<~RUBY
+ def #{attr}
+ warn "JSON.#{attr} is deprecated and will be removed in json 3.0.0", uplevel: 1 #{args}
+ @#{attr}
+ end
+
+ def #{attr}=(val)
+ warn "JSON.#{attr}= is deprecated and will be removed in json 3.0.0", uplevel: 1 #{args}
+ @#{attr} = val
+ end
+
+ def _#{attr}
+ @#{attr}
+ end
+ RUBY
+ end
+ end
end
# Sets create identifier, which is used to decide if the _json_create_
@@ -424,28 +447,26 @@ module JSON
module_function :pretty_unparse
# :startdoc:
- class << self
- # Sets or returns default options for the JSON.unsafe_load method.
- # Initially:
- # opts = JSON.load_default_options
- # opts # => {:max_nesting=>false, :allow_nan=>true, :allow_blank=>true, :create_additions=>true}
- attr_accessor :unsafe_load_default_options
- end
- self.unsafe_load_default_options = {
+ # Sets or returns default options for the JSON.unsafe_load method.
+ # Initially:
+ # opts = JSON.load_default_options
+ # opts # => {:max_nesting=>false, :allow_nan=>true, :allow_blank=>true, :create_additions=>true}
+ deprecated_singleton_attr_accessor :unsafe_load_default_options
+
+ @unsafe_load_default_options = {
:max_nesting => false,
:allow_nan => true,
:allow_blank => true,
:create_additions => true,
}
- class << self
- # Sets or returns default options for the JSON.load method.
- # Initially:
- # opts = JSON.load_default_options
- # opts # => {:max_nesting=>false, :allow_nan=>true, :allow_blank=>true, :create_additions=>true}
- attr_accessor :load_default_options
- end
- self.load_default_options = {
+ # Sets or returns default options for the JSON.load method.
+ # Initially:
+ # opts = JSON.load_default_options
+ # opts # => {:max_nesting=>false, :allow_nan=>true, :allow_blank=>true, :create_additions=>true}
+ deprecated_singleton_attr_accessor :load_default_options
+
+ @load_default_options = {
:allow_nan => true,
:allow_blank => true,
:create_additions => nil,
@@ -581,9 +602,9 @@ module JSON
#
def unsafe_load(source, proc = nil, options = nil)
opts = if options.nil?
- unsafe_load_default_options
+ _unsafe_load_default_options
else
- unsafe_load_default_options.merge(options)
+ _unsafe_load_default_options.merge(options)
end
unless source.is_a?(String)
@@ -741,9 +762,9 @@ module JSON
#
def load(source, proc = nil, options = nil)
opts = if options.nil?
- load_default_options
+ _load_default_options
else
- load_default_options.merge(options)
+ _load_default_options.merge(options)
end
unless source.is_a?(String)
@@ -781,14 +802,12 @@ module JSON
alias restore load
module_function :restore
- class << self
- # Sets or returns the default options for the JSON.dump method.
- # Initially:
- # opts = JSON.dump_default_options
- # opts # => {:max_nesting=>false, :allow_nan=>true}
- attr_accessor :dump_default_options
- end
- self.dump_default_options = {
+ # Sets or returns the default options for the JSON.dump method.
+ # Initially:
+ # opts = JSON.dump_default_options
+ # opts # => {:max_nesting=>false, :allow_nan=>true}
+ deprecated_singleton_attr_accessor :dump_default_options
+ @dump_default_options = {
:max_nesting => false,
:allow_nan => true,
}
@@ -841,7 +860,7 @@ module JSON
end
end
- opts = JSON.dump_default_options
+ opts = JSON._dump_default_options
opts = opts.merge(:max_nesting => limit) if limit
opts = opts.merge(kwargs) if kwargs
diff --git a/test/json/json_common_interface_test.rb b/test/json/json_common_interface_test.rb
index 1f157da026..96f7ccdf92 100644
--- a/test/json/json_common_interface_test.rb
+++ b/test/json/json_common_interface_test.rb
@@ -174,9 +174,9 @@ class JSONCommonInterfaceTest < Test::Unit::TestCase
end
def test_dump_should_modify_defaults
- max_nesting = JSON.dump_default_options[:max_nesting]
+ max_nesting = JSON._dump_default_options[:max_nesting]
dump([], StringIO.new, 10)
- assert_equal max_nesting, JSON.dump_default_options[:max_nesting]
+ assert_equal max_nesting, JSON._dump_default_options[:max_nesting]
end
def test_JSON
@@ -211,6 +211,10 @@ class JSONCommonInterfaceTest < Test::Unit::TestCase
end
end
+ def test_deprecated_dump_default_options
+ assert JSON.dump_default_options
+ end
+
private
def with_external_encoding(encoding)