Skip to content

Deprecate all *_default_options #773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes

* Deprecate `JSON.load_default_options`.
* Deprecate `JSON.unsafe_load_default_options`.
* Deprecate `JSON.dump_default_options`.

### 2025-03-12 (2.10.2)

* Fix a potential crash in the C extension parser.
Expand Down
77 changes: 48 additions & 29 deletions lib/json/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,29 @@ def create_pretty_state

# 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_
Expand Down Expand Up @@ -424,28 +447,26 @@ def pretty_generate(obj, opts = nil)
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,
Expand Down Expand Up @@ -581,9 +602,9 @@ class << self
#
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)
Expand Down Expand Up @@ -741,9 +762,9 @@ def unsafe_load(source, proc = nil, options = nil)
#
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)
Expand Down Expand Up @@ -781,14 +802,12 @@ def recurse_proc(result, &proc) # :nodoc:
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,
}
Expand Down Expand Up @@ -841,7 +860,7 @@ def dump(obj, anIO = nil, limit = nil, kwargs = nil)
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

Expand Down
8 changes: 6 additions & 2 deletions test/json/json_common_interface_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ def test_dump_in_io
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
Expand Down Expand Up @@ -211,6 +211,10 @@ def test_load_file_with_bad_default_external_encoding
end
end

def test_deprecated_dump_default_options
assert JSON.dump_default_options
end

private

def with_external_encoding(encoding)
Expand Down