ruby-on-railsjsonutf-8afnetworkinghttparty

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: ضصيتحضصتحخـ


Solution

  • In the end, I used

    JSON::dump(obj))