Fix JSON decoding of newline character with Yaml backend [#3479 state:resolved]

Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
This commit is contained in:
Maxime RETY 2010-06-14 16:05:49 +02:00 committed by Santiago Pastorino
parent 8bfa8e7cbe
commit 68e3fb8109
2 changed files with 11 additions and 3 deletions

@ -54,7 +54,9 @@ def convert_json_to_yaml(json) #:nodoc:
json.gsub(/\\([\\\/]|u[[:xdigit:]]{4})/) do
ustr = $1
if ustr.start_with?('u')
[ustr[1..-1].to_i(16)].pack("U")
char = [ustr[1..-1].to_i(16)].pack("U")
# "\n" needs extra escaping due to yaml formatting
char == "\n" ? "\\n" : char
elsif ustr == '\\'
'\\\\'
else
@ -75,7 +77,9 @@ def convert_json_to_yaml(json) #:nodoc:
chunk.gsub!(/\\([\\\/]|u[[:xdigit:]]{4})/) do
ustr = $1
if ustr.start_with?('u')
[ustr[1..-1].to_i(16)].pack("U")
char = [ustr[1..-1].to_i(16)].pack("U")
# "\n" needs extra escaping due to yaml formatting
char == "\n" ? "\\n" : char
elsif ustr == '\\'
'\\\\'
else

@ -41,7 +41,11 @@ class TestJSONDecoding < ActiveSupport::TestCase
[{'d' => Date.new(1970, 1, 1), 's' => ' escape'},{'d' => Date.new(1970, 1, 1), 's' => ' escape'}],
%q([{"d":"1970-01-01","s":"http:\/\/example.com"},{"d":"1970-01-01","s":"http:\/\/example.com"}]) =>
[{'d' => Date.new(1970, 1, 1), 's' => 'http://example.com'},
{'d' => Date.new(1970, 1, 1), 's' => 'http://example.com'}]
{'d' => Date.new(1970, 1, 1), 's' => 'http://example.com'}],
# tests escaping of "\n" char with Yaml backend
%q("\n") => "\n",
%q("\u000a") => "\n",
%q({"a":"Line1\u000aLine2"}) => {"a"=>"Line1\nLine2"}
}
# load the default JSON backend