From 1dc5773928e5103eaa2939ea2fe5b9ce2c2087ed Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Sat, 14 Feb 2009 16:47:06 +0900 Subject: [PATCH] Ruby 1.9 compat: fix JSON decoding to work properly with multibyte values --- activesupport/lib/active_support/json/decoding.rb | 5 +++-- activesupport/test/json/decoding_test.rb | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/json/decoding.rb b/activesupport/lib/active_support/json/decoding.rb index 9da4048..0d056af 100644 --- a/activesupport/lib/active_support/json/decoding.rb +++ b/activesupport/lib/active_support/json/decoding.rb @@ -46,10 +46,11 @@ module ActiveSupport json.gsub(/\\\//, '/') else left_pos = [-1].push(*marks) - right_pos = marks << json.length + right_pos = marks << json.bytesize output = [] left_pos.each_with_index do |left, i| - output << json[left.succ..right_pos[i]] + scanner.pos = left.succ + output << scanner.peek(right_pos[i] - scanner.pos + 1) end output = output * " " diff --git a/activesupport/test/json/decoding_test.rb b/activesupport/test/json/decoding_test.rb index 558b03b..ebd46a8 100644 --- a/activesupport/test/json/decoding_test.rb +++ b/activesupport/test/json/decoding_test.rb @@ -1,3 +1,4 @@ +# encoding: UTF-8 require 'abstract_unit' class TestJSONDecoding < Test::Unit::TestCase @@ -10,6 +11,8 @@ class TestJSONDecoding < Test::Unit::TestCase %({"returnTo":[1,"\\"a\\",", "b"]}) => {"returnTo" => [1, "\"a\",", "b"]}, %({a: "'", "b": "5,000"}) => {"a" => "'", "b" => "5,000"}, %({a: "a's, b's and c's", "b": "5,000"}) => {"a" => "a's, b's and c's", "b" => "5,000"}, + # multibyte + %({"matzue": "松江", "asakusa": "浅草"}) => {"matzue" => "松江", "asakusa" => "浅草"}, %({a: "2007-01-01"}) => {'a' => Date.new(2007, 1, 1)}, %({a: "2007-01-01 01:12:34 Z"}) => {'a' => Time.utc(2007, 1, 1, 1, 12, 34)}, # no time zone -- 1.6.1.2