Permalink
Browse files

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.
  • Loading branch information...
1 parent 4bdf929 commit 52fb1a9565a2c1d163efa95f3d1a42247a4cc074 @chancancode chancancode committed Sep 12, 2013
Showing with 14 additions and 2 deletions.
  1. +1 −1 activesupport/lib/active_support/json/decoding.rb
  2. +13 −1 activesupport/test/json/decoding_test.rb
@@ -14,7 +14,7 @@ class << self
# ActiveSupport::JSON.decode("{\"team\":\"rails\",\"players\":\"36\"}")
# => {"team" => "rails", "players" => "36"}
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
convert_dates_from(data)
else
@@ -59,7 +59,16 @@ def self.json_create(object)
%q({"a":"\n"}) => {"a"=>"\n"},
%q({"a":"\u000a"}) => {"a"=>"\n"},
%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|
@@ -83,7 +92,10 @@ def self.json_create(object)
end
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(%()) }
end
def test_cannot_force_json_unmarshalling

0 comments on commit 52fb1a9

Please sign in to comment.