Skip to content

Commit

Permalink
Fix that JSON parser fails to read escaped backslashes.
Browse files Browse the repository at this point in the history
[#973 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information
Daniel Sheppard authored and jeremy committed Aug 10, 2009
1 parent 5c74cff commit 9341655
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
7 changes: 5 additions & 2 deletions activesupport/lib/active_support/json/backends/yaml.rb
Expand Up @@ -17,7 +17,7 @@ def decode(json)
rescue ArgumentError => e
raise ParseError, "Invalid JSON string"
end

protected
# Ensure that ":" and "," are always followed by a space
def convert_json_to_yaml(json) #:nodoc:
Expand All @@ -41,6 +41,8 @@ def convert_json_to_yaml(json) #:nodoc:
end
when ":",","
marks << scanner.pos - 1 unless quoting
when "\\"
scanner.skip(/\\/)
end
end

Expand Down Expand Up @@ -82,4 +84,5 @@ def convert_json_to_yaml(json) #:nodoc:
end
end
end
end
end

10 changes: 6 additions & 4 deletions activesupport/test/json/decoding_test.rb
Expand Up @@ -12,10 +12,10 @@ class TestJSONDecoding < ActiveSupport::TestCase
%({"a": "a's, b's and c's", "b": "5,000"}) => {"a" => "a's, b's and c's", "b" => "5,000"},
# multibyte
%({"matzue": "松江", "asakusa": "浅草"}) => {"matzue" => "松江", "asakusa" => "浅草"},
%({"a": "2007-01-01"}) => {'a' => Date.new(2007, 1, 1)},
%({"a": "2007-01-01 01:12:34 Z"}) => {'a' => Time.utc(2007, 1, 1, 1, 12, 34)},
%({"a": "2007-01-01"}) => {'a' => Date.new(2007, 1, 1)},
%({"a": "2007-01-01 01:12:34 Z"}) => {'a' => Time.utc(2007, 1, 1, 1, 12, 34)},
# no time zone
%({"a": "2007-01-01 01:12:34"}) => {'a' => "2007-01-01 01:12:34"},
%({"a": "2007-01-01 01:12:34"}) => {'a' => "2007-01-01 01:12:34"},
# needs to be *exact*
%({"a": " 2007-01-01 01:12:34 Z "}) => {'a' => " 2007-01-01 01:12:34 Z "},
%({"a": "2007-01-01 : it's your birthday"}) => {'a' => "2007-01-01 : it's your birthday"},
Expand All @@ -27,6 +27,7 @@ class TestJSONDecoding < ActiveSupport::TestCase
%({"a": null}) => {"a" => nil},
%({"a": true}) => {"a" => true},
%({"a": false}) => {"a" => false},
%q({"bad":"\\\\","trailing":""}) => {"bad" => "\\", "trailing" => ""},
%q({"a": "http:\/\/test.host\/posts\/1"}) => {"a" => "http://test.host/posts/1"},
%q({"a": "\u003cunicode\u0020escape\u003e"}) => {"a" => "<unicode escape>"},
%q({"a": "\\\\u0020skip double backslashes"}) => {"a" => "\\u0020skip double backslashes"},
Expand Down Expand Up @@ -74,4 +75,5 @@ class TestJSONDecoding < ActiveSupport::TestCase
def test_failed_json_decoding
assert_raise(ActiveSupport::JSON::ParseError) { ActiveSupport::JSON.decode(%({: 1})) }
end
end
end

0 comments on commit 9341655

Please sign in to comment.