question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a way to automatically escape quotes in Java String using Gson

See original GitHub issue

This is my sample code: ` class SampleObject { String field; public SampleObject() { field = “Great ‘news’”; } public String getField() { return field; } }

private static class EscapeStringSerializer implements JsonSerializer<String> { @Override public JsonElement serialize(String s, Type type, JsonSerializationContext jsonSerializationContext) { return new JsonPrimitive(escapeSpecialChar(s)); }

    public static String escapeSpecialChar(String string) {
        String escapes[][] = new String[][]{
                {"\'", "\\'"}
        };
        for (String[] esc : escapes) {
            string = string.replace(esc[0], esc[1]);
        }
        return string;
    }
}

GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(String.class, new EscapeStringSerializer()); builder.disableHtmlEscaping(); Gson customGson2 = builder.create();

String json = customGson2.toJson(sampleObject); `

I get two escape chars before the quotes instead of one. This is causing issues with serializing and deserializing the data. The Output is: {“field”:“Great \‘news\’”} instead of {“field”:“Great 'news'”} Any pointers will be greatly appreciated. Thanks

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

1reaction
Marcono1234commented, Sep 18, 2021

pass it to couple of child processes

Are you passing the data as command line argument when starting the child process? Maybe you should then rather perform escaping on the JSON result. Also in case you are on Windows, maybe the (unfortunately not well documented) System property jdk.lang.Process.allowAmbiguousCommands can help. It escapes the arguments to prevent Windows from splitting or merging them. Though in general you should avoid passing it as command line argument, and if possible create a temporary file or similar to transfer the data.

0reactions
Marcono1234commented, Jul 29, 2022

Looks like this has been answered, so this issue can probably be closed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Gson - Automatic quote (") escaping? - Stack Overflow
I'm supprised that by default GSON doesn't escape the double quote, so it doesn't generate a valid JSON. Is there a good way...
Read more >
Escape JSON String in Java - Baeldung
Learn ways to escape a JSON String core Java or a library. ... simplest approach is to replace quotes with the appropriate escape...
Read more >
have double quotes in string, gson won't escape it.
JsonObject jsonObject = JsonParser.parse(new Gson().toJson(test)); ... GSON is not escaping the double quote inside string. ... should instead use Gson.
Read more >
How to convert JSON String to Java Object? Gson ... - Java67
Since JSON String are enclosed with double quotes, they need to be escaped in Java code like every double quote i.e. " needs...
Read more >
How to Escape JSON String in Java- Eclipse IDE Tips and ...
You can escape String in Java by putting a backslash in double quotes e.g. " can be escaped as \" if it occurs...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found