'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 1:[1]
In the end, I used
JSON::dump(obj))
Solution 2:[2]
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.)
Solution 3:[3]
Oj can also do the job:
Oj.dump(obj, mode: :compat)
You will have to add oj
it to your gem list
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Antzi |
Solution 2 | Aaron Brager |
Solution 3 | Ariel Cabib |