From 8b32721e1dde6b99bbe2628f98b564b82d6975e9 Mon Sep 17 00:00:00 2001 From: Philip Hallstrom Date: Thu, 23 Oct 2008 10:30:09 -0700 Subject: [PATCH] update to existing json decoding unicode patch --- activesupport/lib/active_support/json/decoding.rb | 20 ++++++++++++++++++-- activesupport/test/json/decoding_test.rb | 4 +++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/activesupport/lib/active_support/json/decoding.rb b/activesupport/lib/active_support/json/decoding.rb index fdb219d..a5f5e63 100644 --- a/activesupport/lib/active_support/json/decoding.rb +++ b/activesupport/lib/active_support/json/decoding.rb @@ -43,13 +43,29 @@ module ActiveSupport 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 output = [] left_pos.each_with_index do |left, i| - output << json[left.succ..right_pos[i]] + output << json[left.succ..right_pos[i]].gsub(/\\([\\\/]|u[[:xdigit:]]{4})/) do + if $1.starts_with?('u') + [$1[1..-1].to_i(16)].pack("U") + elsif $1 == '\\' + '\\\\' + else + $1 + end + end end output = output * " " 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 @@ 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") => "", + %q("\\\\u0020skip double backslashes") => "\\u0020skip double backslashes" } TESTS.each do |json, expected| -- 1.5.6.2