0

Gson will deserialize non-conforming single quoted JSON perfectly. Unfortunately I need to serialize a random interior object that was passed in with single quotes instead of double quotes. The output should look like the object below - see the key "second":

{
"first":{"normal":1}
"second":{'single_quoted': 'random object'} 
}

Assume I already have the "second" object as a JsonObject. I know there are ways to customize the serializers, but I haven't found what I'm looking for.

John Estess
  • 576
  • 10
  • 22
  • `JsonWriter` only supports `"` when writing property names and strings, whilst `JsonReader` supports both `"` and `'` (the latter is just a quirk that Gson supports for whatever reason). `'` is not a part of the JSON grammar, so Gson does not support it on writing. Even if it would for symmetric support of `'`, I would think that switching the style in serializer would be rather crazy. May I know, why do you even need mixed delimiter strings? – terrorrussia-keeps-killing Sep 15 '21 at 04:36
  • My situation is that I need to provide the "almost JSON except with single quotes" serialized input to an expensive enterprise testing tool that I can't identify. This tool doesn't accept valid JSON that has been serialized as expected with escape-double-quoted keys and values. – John Estess Sep 15 '21 at 05:19
  • 1
    Then Gson is not your friend here. Maybe Jackson can support it. Here is a discussion for deserialization though: https://stackoverflow.com/questions/6591388/configure-jackson-to-deserialize-single-quoted-invalid-json – terrorrussia-keeps-killing Sep 15 '21 at 08:22

0 Answers0