From 5964014d7c9aed998dddba54124e557262df0afc 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 From 8bd4072e86bfe59a0c1672bca3b545fb94413cb2 Mon Sep 17 00:00:00 2001 From: Philip Hallstrom Date: Thu, 23 Oct 2008 16:19:45 -0700 Subject: [PATCH] a fix for situations in which would get reset --- activesupport/lib/active_support/json/decoding.rb | 18 ++++++++++-------- 1 files changed, 10 insertions(+), 8 deletions(-) diff --git a/activesupport/lib/active_support/json/decoding.rb b/activesupport/lib/active_support/json/decoding.rb index a5f5e63..a92ed57 100644 --- a/activesupport/lib/active_support/json/decoding.rb +++ b/activesupport/lib/active_support/json/decoding.rb @@ -44,12 +44,13 @@ module ActiveSupport if marks.empty? json.gsub(/\\([\\\/]|u[[:xdigit:]]{4})/) do - if $1.starts_with?('u') - [$1[1..-1].to_i(16)].pack("U") - elsif $1 == '\\' + ustr = $1 + if ustr.starts_with?('u') + [ustr[1..-1].to_i(16)].pack("U") + elsif ustr == '\\' '\\\\' else - $1 + ustr end end else @@ -58,12 +59,13 @@ module ActiveSupport output = [] left_pos.each_with_index do |left, 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 == '\\' + ustr = $1 + if ustr.starts_with?('u') + [ustr[1..-1].to_i(16)].pack("U") + elsif ustr == '\\' '\\\\' else - $1 + ustr end end end -- 1.5.6.2