From 154a6c143723fa4c3031e58626f80c673a867731 Mon Sep 17 00:00:00 2001 From: Tim Pope Date: Tue, 23 Sep 2008 19:46:40 -0400 Subject: [PATCH] Properly decode \u escape sequences in JSON --- activesupport/lib/active_support/json/decoding.rb | 10 +++++++++- activesupport/test/json/decoding_test.rb | 4 +++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/json/decoding.rb b/activesupport/lib/active_support/json/decoding.rb index fdb219d..514ac9b 100644 --- a/activesupport/lib/active_support/json/decoding.rb +++ b/activesupport/lib/active_support/json/decoding.rb @@ -43,7 +43,15 @@ def convert_json_to_yaml(json) #:nodoc: end if marks.empty? - json.gsub(/\\\//, '/') + json.gsub(/\\([\\\/]|u[[:xdigit:]]{4})/) do + if $1.starts_with?('u') + [$1[1..-1].to_i(16)].pack("U") + elsif $1 == '\\' + '\\\\' + else + $1 + end + end else left_pos = [-1].push(*marks) right_pos = marks << json.length diff --git a/activesupport/test/json/decoding_test.rb b/activesupport/test/json/decoding_test.rb index 19ae3a0..17212f4 100644 --- a/activesupport/test/json/decoding_test.rb +++ b/activesupport/test/json/decoding_test.rb @@ -24,7 +24,9 @@ %(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") => "", + %q("\\\\u0020skip double backslashes") => "\\u0020skip double backslashes" } TESTS.each do |json, expected| -- 1.5.6.5