1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Enabled quirks mode on JSON.parse, fixes broken test in af9caae

It turns out that ActionPack depends on the decoder to parse JSON
"fragments" (e.g. '"a string"', '1', 'null', etc), so we need to
enable quirks mode on JSON.parse. Also added coverage on the decoder
side to prevent regression.
This commit is contained in:
Godfrey Chan 2013-09-12 09:58:18 -07:00
parent 4bdf929579
commit 52fb1a9565
2 changed files with 14 additions and 2 deletions

View file

@ -14,7 +14,7 @@ module ActiveSupport
# ActiveSupport::JSON.decode("{\"team\":\"rails\",\"players\":\"36\"}") # ActiveSupport::JSON.decode("{\"team\":\"rails\",\"players\":\"36\"}")
# => {"team" => "rails", "players" => "36"} # => {"team" => "rails", "players" => "36"}
def decode(json, options = {}) def decode(json, options = {})
data = ::JSON.parse(json, options.merge(create_additions: false)) data = ::JSON.parse(json, options.merge(create_additions: false, quirks_mode: true))
if ActiveSupport.parse_json_times if ActiveSupport.parse_json_times
convert_dates_from(data) convert_dates_from(data)
else else

View file

@ -59,7 +59,16 @@ class TestJSONDecoding < ActiveSupport::TestCase
%q({"a":"\n"}) => {"a"=>"\n"}, %q({"a":"\n"}) => {"a"=>"\n"},
%q({"a":"\u000a"}) => {"a"=>"\n"}, %q({"a":"\u000a"}) => {"a"=>"\n"},
%q({"a":"Line1\u000aLine2"}) => {"a"=>"Line1\nLine2"}, %q({"a":"Line1\u000aLine2"}) => {"a"=>"Line1\nLine2"},
%q({"json_class":"TestJSONDecoding::Foo"}) => {"json_class"=>"TestJSONDecoding::Foo"} # prevent json unmarshalling
%q({"json_class":"TestJSONDecoding::Foo"}) => {"json_class"=>"TestJSONDecoding::Foo"},
# json "fragments" - these are invalid JSON, but ActionPack relies on this
%q("a string") => "a string",
%q(1.1) => 1.1,
%q(1) => 1,
%q(-1) => -1,
%q(true) => true,
%q(false) => false,
%q(null) => nil
} }
TESTS.each_with_index do |(json, expected), index| TESTS.each_with_index do |(json, expected), index|
@ -83,7 +92,10 @@ class TestJSONDecoding < ActiveSupport::TestCase
end end
def test_failed_json_decoding def test_failed_json_decoding
assert_raise(ActiveSupport::JSON.parse_error) { ActiveSupport::JSON.decode(%(undefined)) }
assert_raise(ActiveSupport::JSON.parse_error) { ActiveSupport::JSON.decode(%({a: 1})) }
assert_raise(ActiveSupport::JSON.parse_error) { ActiveSupport::JSON.decode(%({: 1})) } assert_raise(ActiveSupport::JSON.parse_error) { ActiveSupport::JSON.decode(%({: 1})) }
assert_raise(ActiveSupport::JSON.parse_error) { ActiveSupport::JSON.decode(%()) }
end end
def test_cannot_force_json_unmarshalling def test_cannot_force_json_unmarshalling