Skip to content

Commit ffa9019

Browse files
authored
Merge pull request #773 from casperisfine/deprecate-global-configs
Deprecate all `*_default_options`
2 parents 2614b12 + 172762c commit ffa9019

File tree

3 files changed

+58
-31
lines changed

3 files changed

+58
-31
lines changed

CHANGES.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes
22

3+
* Deprecate `JSON.load_default_options`.
4+
* Deprecate `JSON.unsafe_load_default_options`.
5+
* Deprecate `JSON.dump_default_options`.
6+
37
### 2025-03-12 (2.10.2)
48

59
* Fix a potential crash in the C extension parser.

lib/json/common.rb

+48-29
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,29 @@ def create_pretty_state
101101

102102
# Sets or Returns the JSON generator state class that is used by JSON.
103103
attr_accessor :state
104+
105+
private
106+
107+
def deprecated_singleton_attr_accessor(*attrs)
108+
args = RUBY_VERSION >= "3.0" ? ", category: :deprecated" : ""
109+
attrs.each do |attr|
110+
singleton_class.class_eval <<~RUBY
111+
def #{attr}
112+
warn "JSON.#{attr} is deprecated and will be removed in json 3.0.0", uplevel: 1 #{args}
113+
@#{attr}
114+
end
115+
116+
def #{attr}=(val)
117+
warn "JSON.#{attr}= is deprecated and will be removed in json 3.0.0", uplevel: 1 #{args}
118+
@#{attr} = val
119+
end
120+
121+
def _#{attr}
122+
@#{attr}
123+
end
124+
RUBY
125+
end
126+
end
104127
end
105128

106129
# Sets create identifier, which is used to decide if the _json_create_
@@ -424,28 +447,26 @@ def pretty_generate(obj, opts = nil)
424447
module_function :pretty_unparse
425448
# :startdoc:
426449

427-
class << self
428-
# Sets or returns default options for the JSON.unsafe_load method.
429-
# Initially:
430-
# opts = JSON.load_default_options
431-
# opts # => {:max_nesting=>false, :allow_nan=>true, :allow_blank=>true, :create_additions=>true}
432-
attr_accessor :unsafe_load_default_options
433-
end
434-
self.unsafe_load_default_options = {
450+
# Sets or returns default options for the JSON.unsafe_load method.
451+
# Initially:
452+
# opts = JSON.load_default_options
453+
# opts # => {:max_nesting=>false, :allow_nan=>true, :allow_blank=>true, :create_additions=>true}
454+
deprecated_singleton_attr_accessor :unsafe_load_default_options
455+
456+
@unsafe_load_default_options = {
435457
:max_nesting => false,
436458
:allow_nan => true,
437459
:allow_blank => true,
438460
:create_additions => true,
439461
}
440462

441-
class << self
442-
# Sets or returns default options for the JSON.load method.
443-
# Initially:
444-
# opts = JSON.load_default_options
445-
# opts # => {:max_nesting=>false, :allow_nan=>true, :allow_blank=>true, :create_additions=>true}
446-
attr_accessor :load_default_options
447-
end
448-
self.load_default_options = {
463+
# Sets or returns default options for the JSON.load method.
464+
# Initially:
465+
# opts = JSON.load_default_options
466+
# opts # => {:max_nesting=>false, :allow_nan=>true, :allow_blank=>true, :create_additions=>true}
467+
deprecated_singleton_attr_accessor :load_default_options
468+
469+
@load_default_options = {
449470
:allow_nan => true,
450471
:allow_blank => true,
451472
:create_additions => nil,
@@ -581,9 +602,9 @@ class << self
581602
#
582603
def unsafe_load(source, proc = nil, options = nil)
583604
opts = if options.nil?
584-
unsafe_load_default_options
605+
_unsafe_load_default_options
585606
else
586-
unsafe_load_default_options.merge(options)
607+
_unsafe_load_default_options.merge(options)
587608
end
588609

589610
unless source.is_a?(String)
@@ -741,9 +762,9 @@ def unsafe_load(source, proc = nil, options = nil)
741762
#
742763
def load(source, proc = nil, options = nil)
743764
opts = if options.nil?
744-
load_default_options
765+
_load_default_options
745766
else
746-
load_default_options.merge(options)
767+
_load_default_options.merge(options)
747768
end
748769

749770
unless source.is_a?(String)
@@ -781,14 +802,12 @@ def recurse_proc(result, &proc) # :nodoc:
781802
alias restore load
782803
module_function :restore
783804

784-
class << self
785-
# Sets or returns the default options for the JSON.dump method.
786-
# Initially:
787-
# opts = JSON.dump_default_options
788-
# opts # => {:max_nesting=>false, :allow_nan=>true}
789-
attr_accessor :dump_default_options
790-
end
791-
self.dump_default_options = {
805+
# Sets or returns the default options for the JSON.dump method.
806+
# Initially:
807+
# opts = JSON.dump_default_options
808+
# opts # => {:max_nesting=>false, :allow_nan=>true}
809+
deprecated_singleton_attr_accessor :dump_default_options
810+
@dump_default_options = {
792811
:max_nesting => false,
793812
:allow_nan => true,
794813
}
@@ -841,7 +860,7 @@ def dump(obj, anIO = nil, limit = nil, kwargs = nil)
841860
end
842861
end
843862

844-
opts = JSON.dump_default_options
863+
opts = JSON._dump_default_options
845864
opts = opts.merge(:max_nesting => limit) if limit
846865
opts = opts.merge(kwargs) if kwargs
847866

test/json/json_common_interface_test.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ def test_dump_in_io
174174
end
175175

176176
def test_dump_should_modify_defaults
177-
max_nesting = JSON.dump_default_options[:max_nesting]
177+
max_nesting = JSON._dump_default_options[:max_nesting]
178178
dump([], StringIO.new, 10)
179-
assert_equal max_nesting, JSON.dump_default_options[:max_nesting]
179+
assert_equal max_nesting, JSON._dump_default_options[:max_nesting]
180180
end
181181

182182
def test_JSON
@@ -211,6 +211,10 @@ def test_load_file_with_bad_default_external_encoding
211211
end
212212
end
213213

214+
def test_deprecated_dump_default_options
215+
assert JSON.dump_default_options
216+
end
217+
214218
private
215219

216220
def with_external_encoding(encoding)

0 commit comments

Comments
 (0)