Skip to content
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Unreleased

* Improve `JSON.load` and `JSON.unsafe_load` to allow passing options as second argument.
* Fix the parser to no longer ignore invalid escapes in strings.
Only `\"`, `\\`, `\b`, `\f`, `\n`, `\r`, `\t` and `\u` are valid JSON escapes.
* On TruffleRuby, fix the generator to not call `to_json` on the return value of `as_json` for `Float::NAN`.
Expand Down
21 changes: 19 additions & 2 deletions lib/json/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ def pretty_generate(obj, opts = nil)
:create_additions => nil,
}
# :call-seq:
# JSON.unsafe_load(source, options = {}) -> object
# JSON.unsafe_load(source, proc = nil, options = {}) -> object
#
# Returns the Ruby objects created by parsing the given +source+.
Expand Down Expand Up @@ -681,7 +682,12 @@ def pretty_generate(obj, opts = nil)
#
def unsafe_load(source, proc = nil, options = nil)
opts = if options.nil?
_unsafe_load_default_options
if proc && proc.is_a?(Hash)
options, proc = proc, nil
options
else
_unsafe_load_default_options
end
else
_unsafe_load_default_options.merge(options)
end
Expand Down Expand Up @@ -709,6 +715,7 @@ def unsafe_load(source, proc = nil, options = nil)
end

# :call-seq:
# JSON.load(source, options = {}) -> object
# JSON.load(source, proc = nil, options = {}) -> object
#
# Returns the Ruby objects created by parsing the given +source+.
Expand Down Expand Up @@ -845,8 +852,18 @@ def unsafe_load(source, proc = nil, options = nil)
# @attributes={"type"=>"Admin", "password"=>"0wn3d"}>}
#
def load(source, proc = nil, options = nil)
if proc && options.nil? && proc.is_a?(Hash)
options = proc
proc = nil
end

opts = if options.nil?
_load_default_options
if proc && proc.is_a?(Hash)
options, proc = proc, nil
options
else
_load_default_options
end
else
_load_default_options.merge(options)
end
Expand Down
7 changes: 7 additions & 0 deletions test/json/json_common_interface_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def test_load_with_proc
def test_load_with_options
json = '{ "foo": NaN }'
assert JSON.load(json, nil, :allow_nan => true)['foo'].nan?
assert JSON.load(json, :allow_nan => true)['foo'].nan?
end

def test_load_null
Expand Down Expand Up @@ -215,6 +216,12 @@ def test_unsafe_load_with_proc
assert_equal expected, visited
end

def test_unsafe_load_with_options
json = '{ "foo": NaN }'
assert JSON.unsafe_load(json, nil, :allow_nan => true)['foo'].nan?
assert JSON.unsafe_load(json, :allow_nan => true)['foo'].nan?
end

def test_unsafe_load_default_options
too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
assert JSON.unsafe_load(too_deep, nil).is_a?(Array)
Expand Down