Skip to content

Commit c54de70

Browse files
committed
Improve JSON.load and JSON.unsafe_load to allow passing options as second argument
Otherwise it's very error prone.
1 parent 65d62dc commit c54de70

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### Unreleased
44

5+
* Improve `JSON.load` and `JSON.unsafe_load` to allow passing options as second argument.
56
* Fix the parser to no longer ignore invalid escapes in strings.
67
Only `\"`, `\\`, `\b`, `\f`, `\n`, `\r`, `\t` and `\u` are valid JSON escapes.
78
* On TruffleRuby, fix the generator to not call `to_json` on the return value of `as_json` for `Float::NAN`.

lib/json/common.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ def pretty_generate(obj, opts = nil)
550550
:create_additions => nil,
551551
}
552552
# :call-seq:
553+
# JSON.unsafe_load(source, options = {}) -> object
553554
# JSON.unsafe_load(source, proc = nil, options = {}) -> object
554555
#
555556
# Returns the Ruby objects created by parsing the given +source+.
@@ -681,7 +682,12 @@ def pretty_generate(obj, opts = nil)
681682
#
682683
def unsafe_load(source, proc = nil, options = nil)
683684
opts = if options.nil?
684-
_unsafe_load_default_options
685+
if proc && proc.is_a?(Hash)
686+
options, proc = proc, nil
687+
options
688+
else
689+
_unsafe_load_default_options
690+
end
685691
else
686692
_unsafe_load_default_options.merge(options)
687693
end
@@ -709,6 +715,7 @@ def unsafe_load(source, proc = nil, options = nil)
709715
end
710716

711717
# :call-seq:
718+
# JSON.load(source, options = {}) -> object
712719
# JSON.load(source, proc = nil, options = {}) -> object
713720
#
714721
# Returns the Ruby objects created by parsing the given +source+.
@@ -845,8 +852,18 @@ def unsafe_load(source, proc = nil, options = nil)
845852
# @attributes={"type"=>"Admin", "password"=>"0wn3d"}>}
846853
#
847854
def load(source, proc = nil, options = nil)
855+
if proc && options.nil? && proc.is_a?(Hash)
856+
options = proc
857+
proc = nil
858+
end
859+
848860
opts = if options.nil?
849-
_load_default_options
861+
if proc && proc.is_a?(Hash)
862+
options, proc = proc, nil
863+
options
864+
else
865+
_load_default_options
866+
end
850867
else
851868
_load_default_options.merge(options)
852869
end

test/json/json_common_interface_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ def test_load_with_proc
149149
def test_load_with_options
150150
json = '{ "foo": NaN }'
151151
assert JSON.load(json, nil, :allow_nan => true)['foo'].nan?
152+
assert JSON.load(json, :allow_nan => true)['foo'].nan?
152153
end
153154

154155
def test_load_null
@@ -215,6 +216,12 @@ def test_unsafe_load_with_proc
215216
assert_equal expected, visited
216217
end
217218

219+
def test_unsafe_load_with_options
220+
json = '{ "foo": NaN }'
221+
assert JSON.unsafe_load(json, nil, :allow_nan => true)['foo'].nan?
222+
assert JSON.unsafe_load(json, :allow_nan => true)['foo'].nan?
223+
end
224+
218225
def test_unsafe_load_default_options
219226
too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
220227
assert JSON.unsafe_load(too_deep, nil).is_a?(Array)

0 commit comments

Comments
 (0)