Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding 4 byte UTF-8 character 💩 to JSON from Rails produce invalid character

I have a web service in rails (3.2.19) that encode a JSON to be read by some iOS or Android apps. The json might contain any characters, but it seems to fail every time I use a 4 bytes UTF8 character such as 💩: it produce instead \uf4a9 aka .

In rails console the character is correctly displayed but when i retrieve the answer in iOS with AFNetworking or with HTTParty, it fails.

Here is my code sample to retrieve the faulty JSON:

puts HTTParty.post( 'http://0.0.0.0:3000/login',
:body => { :login => 'antoine',
           :password => 'thisisnotmypassword',
         }.to_json,
:headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json' })

And to encode the JSON:

format.json { render json: json_reponse } #json_response is a Hash.

Sending other UTF-8 characters works well, for exemple: ضصيتحضصتحخـ

like image 951
Antzi Avatar asked Aug 07 '14 02:08

Antzi


2 Answers

Your web service is encoding the JSON incorrectly. It looks like you're not using the standard JSON encoder, since as of Rails 3.2.13, the Unicode characters are passed through (that is, you shouldn't be seeing any \u1234-type encoding.)

That said, if you want to maintain the old encoding, try switching from object.to_json to JSON.generate(object, :ascii_only => true). (More details in the link in the 1st paragraph.)

like image 180
Aaron Brager Avatar answered Sep 20 '22 08:09

Aaron Brager


In the end, I used

JSON::dump(obj))

like image 21
Antzi Avatar answered Sep 21 '22 08:09

Antzi