I noticed that Gson converts the string "<" into an unicode escape sequence in JSON output. Can you avoid this somehow, or do characters like "<" and ">" always have to be escaped in JSON?
Consider this example which prints {"s":"\u003c"}; I'd want simply {"s":"<"}.
public static void main(String[] args) {
Gson gson = new GsonBuilder().create();
System.out.println(gson.toJson(new Foo()));
}
static class Foo {
String s = "<";
}
Context: the piece of JSON I'm creating has nothing to do with HTML pages or even JavaScript; it's just used to pass certain structured information to another piece of software (embedded in a device, written in C).
You need to disable HTML escaping.
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
the Ampasand symbol was replacing with \u0026 , by using this it got resolved.
Related
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.
I am having user defined object Customer which has multiple attributes ,in one of the attributes we can have single , double quotes and backslash as well. While converting the object to string Gson library is adding backslash in it.
I am using below code to achive this but it is not working.
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
JsonElement jsonString = gson.toJsonTree(triggerModel);
Output is
{
"customerId": "1234",
"customerName": "Loren",
"customerAddress": [
{
"postalcode": "67676",
"lane": "\"LA16767",
"houseNumber": "2025",
"society": "null"
}
]
}
In lane attribute the original value was "LA16767 but it is adding one backslash character. How to write this in such a way string with single ,double quotes and backslash are handled using same line of code.
Output provided by Gson is correct because "lane": ""LA16767" would not have been a valid json.
From json docs
A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.
You had mentioned :
How to write this in such a way string with single ,double quotes and backslash are handled using same line of code.
You need not do anything special to handle single ,double quotes and backslash characters. Gson will automatically escape them for you.
Any app (server, UI, etc,) who is consuming your json, will correctly parse "\"LA16767" as "LA16767 going by the json conventions.
I have Map<String, String> which later I want to serialize to JSON using Gson. Some of keys of this map contain Unicode characters like \uf177 etc. The problem appears when I try to serialize such a map to JSON, let's say I have Map<String, String> containing:
"TEST_KEY" -> "\uf177"
then, when serialized using Gson, I have:
{
"TEST_KEY": "\\uf177"
}
Which is not what I want, I want these Unicode symbols to be as they are when serialized. Is there a way to achieve this? Would appreciate any help,
UPDATE
Code which produces the issue:
projectI18nFileContent = commentsRemover.transform(projectI18nFileContent);
//find json map which represents translations
Matcher fullTranslationsMapMatcher = translationsMapSerializedToJsonPattern.matcher(projectI18nFileContent);
if (!fullTranslationsMapMatcher.find()) {
throw new IllegalArgumentException(format("%s \n does not contain valid translations json map", projectI18nFileContent));
}
String translationsMapSerializedToJson = fullTranslationsMapMatcher.group();
String newTranslationsMapSerializedToJson = gson.toJson(newTranslations);
//replace old json translations map with a new
return projectI18nFileContent.replace(translationsMapSerializedToJson, newTranslationsMapSerializedToJson);
This piece of code is dedicated to change content of i18n file for javascript project, this is the reason why unicode should not be escaped (otherwise it's just resolved not correctly)
Thanks,
Cheers
According to Json spec the backslashes must to be escaped.
So you shouldn't try to prevent this. It's correct behavior.
I am loading a value from the property file and then passing it to gson method for converting it to final json object. However, the value coming from the property file has double quotes for which the gson is adding "\" to the output. I have scanned down the whole web but unable to find a solution
The property file contains
0110= This is a test for the renewal and the "Renewal no:"
Here's my code
public String toJSONString(Object object) {
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
//Note object here is the value from the property file
return gson.toJson(object);
}
This produces
"{ResponseCode:0110,ResponseText:This is a test for the renewal and the \"Renewal no:\"}"
I am not sure in the output, why it is adding or wrapping the \ around the literals or where ever we have the double quotes in the property file value?
Based on comments on your question, the object parameter is actually referencing a Java String with the value
{ResponseCode:0110,ResponseText:This is a test for the renewal and the "Renewal no:"}
I can't say why, but that's what your String contains.
String is a special type which Gson interprets as a JSON string. Since " is a special character that must be escaped in JSON strings, that's what Gson does and produces the JSON string.
"{ResponseCode:0110,ResponseText:This is a test for the renewal and the \"Renewal no:\"}"
The \ character is escaping special characters like " in the string. You can't store a " in a string without a leading . It has to be \".
You remove the slashes when you display any output string.
Apache Commons has a library for handling escaping and unescaping strings: https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html
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);