Vel
Vel

Reputation: 787

How to disable character encoding in gson?

Gson directly encodes the characters like double quote(") by default when it converts a java object in json string. I have a array of xml data in a json object, as given below.

<items>
  <item id="100" name="test">
  </item>
</items>

I am using JAXB to create this XML structure using java objects, when I convert the constructed object to JSON using GSON, the xml output I get is given below,

<items><item id=\"100\" name=\"test\"></item></items>

How can I stop GSON escape the double quote? Please help.

Upvotes: 3

Views: 1694

Answers (1)

MinecraftShamrock
MinecraftShamrock

Reputation: 3574

You cannot stop GSON escape double quotes, because the output would not be valid JSON anymore. Each " would stop or start the current JSON string and the parts between would corrupt the JSON string.

However, this should not be a problem since a JSON parser will convert the \" back to " when deserializing anyway, because this is the specification of JSON.

See this very nice JSON specification, especially this part:


(source: json.org)

Upvotes: 2

Related Questions