Char array replace characters - java

I'm creating json response in quite sophisticated framework and have problems with json escaping.
I'm able to grab char[] with text I'd like to escape. What's the proper (and the best in case of performance) way to do escaping. Keep in mind that it's not replacing character with character - it's replacing (mostly) one character with two characters, so the array has to be rearranged.
Using common (Apache, Google, ...) libraries would be appreciated.
edit:
Gson library looks fine for my purposes, however there's a problem with snippet:
Gson gson2 = new Gson();
String json = gson2.toJson(new String(buf));
cause it encodes html as well. My task is to do just json encoding for given HTML markup, so I don't want to have tags encoded like \u003e.

I alway use Gson from Google. It work fine for me and no escaping problems I meat ever. Try it.
Gson gson = new Gson();
// To json:
String result = gson.toJson(yourObject);
// From json:
YourObject object= gson.fromJson(result, YourObject.class);

You need
GsonBuilder builder = new GsonBuilder();
builder.disableHtmlEscaping();
String result = builder.create().toJson(yourObject);

Related

How to convert utf8 string to escape string in JSON Java?

I want to convert a UTF-8 string to escape \uXXX format in value of JSON Object.
I used both JSON Object and Gson, but did not work for me in this case:
JSONObject js = new JSONObject();
js.put("lastReason","nguyễn");
System.out.println(js.toString());
and
Gson gson = new Gson();
String new_js = gson.toJson(js.toString());
System.out.println(new_js);
Output: {"test":"nguyễn"}
But i am expect that my result is:
Expected Output: {"test":"nguy\u1EC5n"}
Any solutions for this case, please help me to resolve it.
You can use apache commons-text library to change a string to use Unicode escape sequences. Use org.apache.commons.text.StringEscapeUtils to translate the text before adding it to JSONObject.
StringEscapeUtils.escapeJava("nguyễn")
will produce
nguy\u1EC5n
One possible problem with using StringEscapeUtils might be that it will escape control characters as well. If there is a tab character at the end of the string it will be translated to \t. I.e.:
StringEscapeUtils.escapeJava("nguyễn\t")
will produce an incorrect string:
nguy\u1EC5n\t
You can use org.apache.commons.text.translate.UnicodeEscaper to get around this but it will translate every character in the string to a Unicode escape sequence. I.e.:
UnicodeEscaper ue = new UnicodeEscaper();
ue.translate(rawString);
will produce
\u006E\u0067\u0075\u0079\u1EC5\u006E
or
\u006E\u0067\u0075\u0079\u1EC5\u006E\u0009
Whether it is a problem or not is up to you to decide.

Does the StringBuffer change the order of JSON?

I'm using StringBuffer to get JSON from a URL.
This is the original JSON
[{"name":"Italy","topLevelDomain":[".it"],"alpha2Code":"IT","alpha3Code":"ITA","callingCodes":["39"],"capital":"Rome","altSpellings":["IT","Italian Republic","Repubblica italiana"],"region":"Europe","subregion":"Southern Europe","population":60665551,"latlng":[42.83333333,12.83333333],"demonym":"Italian","area":301336.0,"gini":36.0,"timezones":["UTC+01:00"],"borders":["AUT","FRA","SMR","SVN","CHE","VAT"],"nativeName":"Italia","numericCode":"380","currencies":[{"code":"EUR","name":"Euro","symbol":"€"}],"languages":[{"iso639_1":"it","iso639_2":"ita","name":"Italian","nativeName":"Italiano"}],"translations":{"de":"Italien","es":"Italia","fr":"Italie","ja":"イタリア","it":"Italia","br":"Itália","pt":"Itália","nl":"Italië","hr":"Italija","fa":"ایتالیا"},"flag":"https://restcountries.eu/data/ita.svg","regionalBlocs":[{"acronym":"EU","name":"European Union","otherAcronyms":[],"otherNames":[]}],"cioc":"ITA"}]
This is the JSON That I end up with once I convert it to a string from the response
[{"area":301336,"nativeName":"Italia","capital":"Rome","demonym":"Italian","flag":"https://restcountries.eu/data/ita.svg","alpha2Code":"IT","languages":[{"nativeName":"Italiano","iso639_2":"ita","name":"Italian","iso639_1":"it"}],"borders":["AUT","FRA","SMR","SVN","CHE","VAT"],"subregion":"Southern Europe","callingCodes":["39"],"regionalBlocs":[{"otherNames":[],"acronym":"EU","name":"European Union","otherAcronyms":[]}],"gini":36,"population":60665551,"numericCode":"380","alpha3Code":"ITA","topLevelDomain":[".it"],"timezones":["UTC+01:00"],"cioc":"ITA","translations":{"br":"Itália","de":"Italien","pt":"Itália","ja":"イタリア","hr":"Italija","it":"Italia","fa":"ایتالیا","fr":"Italie","es":"Italia","nl":"Italië"},"name":"Italy","altSpellings":["IT","Italian Republic","Repubblica italiana"],"region":"Europe","latlng":[42.83333333,12.83333333],"currencies":[{"symbol":"\u20ac","code":"EUR","name":"Euro"}]}]
This is my code for getting the JSON + Converting it.
JSONArray JSON = null;
//Reading Variables
BufferedReader r = new BufferedReader(new InputStreamReader(con.getInputStream()));
String input;
StringBuffer response = new StringBuffer();
//Adding response to StringBuffer
while((input = r.readLine()) != null) {
response.append(input);
}
//Stopping the reader
r.close();
System.out.println(response);
//Convert StringBuffer to JSON
JSON = new JSONArray(response.toString());
System.out.println(JSON);
return JSON;
Is there a way of preventing it from doing this?
It's not the StringBuffer but the JSONArray.
The order of elements in an array [] is maintained like the list ["AUT","FRA","SMR","SVN","CHE","VAT"] in both examples.
Anything as a name value pair surrounded by {} can be reordered like {"code":"EUR","name":"Euro","symbol":"€"} and {"symbol":"\u20ac","code":"EUR","name":"Euro"}.
To prevent this, you can keep it as a String or create your own object and define the toString method.
Your question is similar to Keep the order of the JSON keys during JSON conversion to CSV.
It is not StringBuffer doing this. It is the JSON implementation itself.
For a start, according to all of the JSON specifications that I have seen, the order of the attributes of a JSON object are not significant. A JSON parser is not expected to preserve the attribute order, and neither is the in memory representation of a JSON object. So, for example, a typical in-memory representation of a JSON object uses a HashMap to hold the attribute names and values.
So my first piece of advice to you would be to change your application so that the order of the JSON attributes doesn't matter. If you design a JSON API where attribute order matters, then your API will be problematic.
(If this is in a testcase, it is not difficult to compare JSON properly. For example, parse the JSON and compare objects attribute by attribute.)
If you are lumbered with a (so-called) JSON API where the order of attributes has some meaning, my advice is:
Complain. Submit a bug report. This is not a proper JSON API.
Look for a JSON library that provides a way to work around the bad design. For example, some libraries allow you to provide a Map class to be used when constructing a JSONObject. The default is usually HashMap, but you could use LinkedHashMap instead.

Java - JSONObject charset

I'm using JSONObject from org.json.*
I need to construct JSONObject with string fields like this
field:"englishletters123\u1234\u3456"//UTF-8 encoding
so, I'm doing this
myJSONObject.put("field", myString);
But instead of this I'm getting object with fluent (non-english) letters instead of their UTF-8 representation.
String newString = new String(oldString.getBytes(...), ...);
myJSONObject.put("field", newString);
doesn't work as well
Is there any way to make such operation? Maybe I should use some other library?
I'm not overly familiar with that JSON serialization library, but since you asked, the GSON library from google is amazing. It handles nearly everything through reflection, it's as simple as creating an object that fit the description of the JSON text you are attempting to create.
for example:
public class Thing{
public String field = "whatever you want";
}
Gson gson = new Gson();
String jsonString = gson.toJson(new Thing());
de-serializing is simple too:
Thing t = gson.fromJson(jsonString, Thing.class);
Of course, there's much more to the library, but that's the basics of it.

Java Json / Gson put json encoded string to jsonObject without string escapes

i first create a Json String with
String myJsonString = new Gson().toJson(myElement);
this works fine.
After that, i want to add this String to anothe big jsonObject to send it to backend with other vars.
jsonObject.put("Tests",myJsonString);
but with this line of code the special character will be escaped and the parser on the backend didnt get it.
How can I avoid it, that myJsonString will be serialized again?
jsonObject.put("Tests",myElement);
doesnt work, because after that there are only references in the jsonObject but no values.
jsonObject.put("Tests", new JSONObject(myJsonString));
(assuming jsonObject is of type org.json.JSONObject)

Android gson.toJson() gives a result with escaped quotes (\")

I use Gson to serialize my object, but the result contains many '\"' which should be '"', like:
{"data":"{\"calldate\":\"2012-05-03 00:12:00\",\"id\":0,\"uid\":0,\"popdbid\":0
,\"mobilesqlid\":1336025277424,\"callstatus\":2,\"checkstatus\":0,\"resultstatus
\":0,\"sequence\":0,\"subbrandstatus\":0,\"subcategorystatus\":0,\"synstatus\":1
,\"targetstatus\":0,\"trackstatus\":0,\"isfrommobile\":0}","user":{"id":11,"ente
rprise_id":1}}
This is the code I use to serialize:
GsonBuilder builder=new GsonBuilder();
builder.setDateFormat("yyyy-MM-dd mm:hh:ss");
builder.excludeFieldsWithoutExposeAnnotation();
Gson gson=builder.create();
String gsonString = gson.toJson(callDayPlanning);
Any suggestion?
As #PhilippReichart stated, your callDayPlanning contains (probably) a String field named data that contains a Json string. This is confirmed by json.parser.online.fr:
.
There's nothing wrong to put a Json string as payload of another object but it will be escaped. But, if you want that data would be the serialization of another object (whose class is named DayPlanning maybe?) you have to change data type from String to your custom class.

Categories

Resources