mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Properly decode \u escape sequences in JSON [#1100 state:resolved] [Tim Pope, Philip Hallstrom]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
This commit is contained in:
parent
0464254430
commit
9b9b2937ce
2 changed files with 23 additions and 4 deletions
|
@ -43,14 +43,31 @@ module ActiveSupport
|
|||
end
|
||||
|
||||
if marks.empty?
|
||||
json.gsub(/\\\//, '/')
|
||||
json.gsub(/\\([\\\/]|u[[:xdigit:]]{4})/) do
|
||||
ustr = $1
|
||||
if ustr.starts_with?('u')
|
||||
[ustr[1..-1].to_i(16)].pack("U")
|
||||
elsif ustr == '\\'
|
||||
'\\\\'
|
||||
else
|
||||
ustr
|
||||
end
|
||||
end
|
||||
else
|
||||
left_pos = [-1].push(*marks)
|
||||
right_pos = marks << scanner.pos + scanner.rest_size
|
||||
output = []
|
||||
left_pos.each_with_index do |left, i|
|
||||
scanner.pos = left.succ
|
||||
output << scanner.peek(right_pos[i] - scanner.pos + 1)
|
||||
output << json[left.succ..right_pos[i]].gsub(/\\([\\\/]|u[[:xdigit:]]{4})/) do
|
||||
ustr = $1
|
||||
if ustr.starts_with?('u')
|
||||
[ustr[1..-1].to_i(16)].pack("U")
|
||||
elsif ustr == '\\'
|
||||
'\\\\'
|
||||
else
|
||||
ustr
|
||||
end
|
||||
end
|
||||
end
|
||||
output = output * " "
|
||||
|
||||
|
|
|
@ -28,7 +28,9 @@ class TestJSONDecoding < Test::Unit::TestCase
|
|||
%(null) => nil,
|
||||
%(true) => true,
|
||||
%(false) => false,
|
||||
%q("http:\/\/test.host\/posts\/1") => "http://test.host/posts/1"
|
||||
%q("http:\/\/test.host\/posts\/1") => "http://test.host/posts/1",
|
||||
%q("\u003cunicode\u0020escape\u003e") => "<unicode escape>",
|
||||
%q("\\\\u0020skip double backslashes") => "\\u0020skip double backslashes"
|
||||
}
|
||||
|
||||
TESTS.each do |json, expected|
|
||||
|
|
Loading…
Reference in a new issue