Jakub Troszok
Jakub Troszok

Reputation: 103733

toJSON and UTF8 string encoding error in Ruby on Rails 3.1

I've got problem with UTF8 string exported using to_json.

The problem is that when I do this:

ruby-1.9.2-p290 :005 > "anche il più remoto".encoding
 => #<Encoding:UTF-8> 
ruby-1.9.2-p290 :006 > {:text => "anche il più remoto"}.to_json
 => "{\"text\":\"anche il pi\\u00f9 remoto\"}" 
{:text => "anche il più remoto"}.to_json.encoding
 => #<Encoding:US-ASCII> 
ruby-1.9.2-p290 :009 > 

How can i skip changing the encoding? I want to return UTF8 string to the clients so they don't have decode it.

Upvotes: 1

Views: 1132

Answers (1)

Larry K
Larry K

Reputation: 49104

Are you using json or ruby-json gem? From here, it's looking like ruby-json is having problems.

Try

s.add_dependency('json', '>= 1.5.3')  # this
s.add_dependency('ruby-json', '>= 1.1.2')   # not this

# and

require 'json' #this
require 'json/objects' # not this

Upvotes: 1

Related Questions