1

I upgraded downgraded to rails 2.3.17 due to the security bugs, but now I can't decode json strings that I have saved down to a DB if they have unicode in them :(. Is there a way to process the string such that it decodes properly?

e = ActiveSupport::JSON.encode({'a' => "Hello Unicode \u2019"})
ActiveSupport::JSON.decode(e)

gives me

RangeError: 8217 out of char range
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-2.3.17/lib/active_support/json/backends/okjson.rb:314:in `unquote'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-2.3.17/lib/active_support/json/backends/okjson.rb:251:in `strtok'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-2.3.17/lib/active_support/json/backends/okjson.rb:215:in `tok'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-2.3.17/lib/active_support/json/backends/okjson.rb:178:in `lex'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-2.3.17/lib/active_support/json/backends/okjson.rb:46:in `decode'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-2.3.17/lib/active_support/json/backends/okjson.rb:612:in `decode'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-2.3.17/lib/active_support/json/decoding.rb:14:in `decode'
from (irb):30
from /usr/local/bin/irb:12:in `<main>'

I can't change the first line since it's coming from the DB like that.

This used to work.

1
  • Try not using ActiveSupport instead of the basic Jason library. Commented Feb 12, 2013 at 1:29

3 Answers 3

3

You can change the backend JSON provider in ActiveSupport.

Add ActiveSupport::JSON.backend = "JSONGem" into an application initialiser (I added it to application.rb). This fixed the unicode parsing issues I had after I upgraded activesupport to 3.0.20.

See the vulnerability notice which caused this update - It mentions that this workaround should apply to 2.3.16 as well.

From rails console:

> ActiveSupport::VERSION::STRING
 => "3.0.20" 
> ActiveSupport::JSON.decode('{"test":"string\u2019"}')
RangeError: 8217 out of char range
> ActiveSupport::JSON.backend = "JSONGem" 
> ActiveSupport::JSON.decode('{"test":"string\u2019"}')
 => {"test"=>"string’"}
Sign up to request clarification or add additional context in comments.

Comments

0

The JSON gem will handle this correctly.

As a note, the gem is much more strict than the other JSON parsers out there. For example:

{ 'test' : 'value' }

This is not valid JSON even though it looks okay.

For whatever reason the non-UTF-8 savvy JSON parser shipped as part of the 2.3.16 patch which is really sloppy on the part of the maintainer.

2 Comments

Doesn't work because the values in the DB were encoded with ActiveSupport::JSON.encode, and I doubt the gem will be able to decode all values. I temporarily fixed it by upgrading from 2.3.17 to 2.3.15. I think it's just an active support bug.
Yikes. You could always spin through all your records, decode with the broken parser, encode with the working one, and save them in-place.
-1
  • Switch to 2.3.15 which should be fine because that's when the fixes landed.
  • Curse the developer who started this project in rails
  • Begin work on porting to python post haste

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.