w9jds
w9jds

Reputation: 897

App Engine Breaks Encoding

When I deploy the backend locally and make the call to the apimethod (through url or explorer) I receive a response with this in the JSON:

"pronunciations": [  {   
    "type": "ahd-legacy",   
    "pronunciation": "(rēˈstrə)"  
} ]

However, the second I deploy it to the app engine and call this method (which stores the object in objectify) and then sends it back in that object format I am receiving this as the JSON:

"pronunciations": [  {
    "type": "ahd-legacy",
    "pronunciation": "(r����str��)"
} ]

I have also tried storing the string as utf-8 bytes (which objectify automatically converts to base64 which then still converts to the above)

Should I be tagging something specifically so it is stored correctly?

Upvotes: 0

Views: 178

Answers (2)

w9jds
w9jds

Reputation: 897

I figured out what it is. The reason why this works fine when run from local and doesn't work when you deploy is because when I made a call to receieve something from an endpoint, it automatically uses the encoding for the server which is ASCII. If you want this to work you need to make your request like this:

URL url = new URL(uri);
String response = new BufferedReader(
    new InputStreamReader(url.openStream(), "UTF-8")).readLine();

JSONParser jsonParser = new JSONParser();
return jsonParser.parse(response);

Upvotes: 1

Peter Knego
Peter Knego

Reputation: 80340

This is probably not a storage problem, but more likely an encoding problem at the point where you receive/send data from/to the network and encode/decode it.

JVM on GAE production servers is set to have US-ASCII as default encoding. On your local machine it's probably set to UTF-8.

Whenever you convert between String and byte arrays, you should always explicitly use UTF-8.

Upvotes: 0

Related Questions