Can you avoid Gson converting "<" and ">" into unicode escape sequences?

JavaJsonGson

Java Problem Overview


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).

Java Solutions


Solution 1 - Java

You need to disable HTML escaping.

Gson gson = new GsonBuilder().disableHtmlEscaping().create();

Solution 2 - Java

the Ampasand symbol was replacing with \u0026 , by using this it got resolved.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonikView Question on Stackoverflow
Solution 1 - JavaBalusCView Answer on Stackoverflow
Solution 2 - JavaROOPA P DView Answer on Stackoverflow