boonproject / boon

Simple opinionated Java for the novice to expert level Java Programmer. Low Ceremony. High Productivity.
http://richardhightower.github.io/site/Boon/Welcome.html
Apache License 2.0
520 stars 102 forks source link

Boon fails to escape double quote #362

Open dustpuppyNGTI opened 7 years ago

dustpuppyNGTI commented 7 years ago

In order to deliver valid JSON double quotes have to be escaped. Boon does not appear to take this into account. There is a setting that does encoding of UTF-8 chars which will also take care of escaping by accident but would push decoding onto the receiving end. There is no way to leave the UTF-8 characters intact but still output escaped double quotes. The Gson parser does the right thing by default:

public class JsonTest {

    public static final Gson GSON_DEFAULT = new Gson();

    public static final ObjectMapper BOON_DEFAULT = JsonFactory.create();
    public static final ObjectMapper BOON_ENCODING = JsonFactory.create(new JsonParserFactory(),
            new JsonSerializerFactory().setEncodeStrings(false));

    private class Wrapper {
        public String test;
    }

    @Test
    public void encodeQuote() {
        Wrapper quote = new Wrapper();
        quote.test = "boe \" aargh";
        Wrapper utf8 = new Wrapper();
        utf8.test = "Τη γλώσσα μου έδωσαν ελληνική";

        System.out.println(GSON_DEFAULT.toJson(quote));
        System.out.println(GSON_DEFAULT.toJson(utf8)+"\n");

        System.out.println(BOON_DEFAULT.toJson(quote));
        System.out.println(BOON_DEFAULT.toJson(utf8)+"\n");

        System.out.println(BOON_ENCODING.toJson(quote));
        System.out.println(BOON_ENCODING.toJson(utf8)+"\n");
    }
}

output:

{"test":"boe \" aargh"}
{"test":"Τη γλώσσα μου έδωσαν ελληνική"}

{"test":"boe \" aargh"}
{"test":"\u03a4\u03b7 \u03b3\u03bb\u03ce\u03c3\u03c3\u03b1 \u03bc\u03bf\u03c5 \u03ad\u03b4\u03c9\u03c3\u03b1\u03bd \u03b5\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ae"}

{"test":"boe " aargh"}
{"test":"Τη γλώσσα μου έδωσαν ελληνική"}

EDIT: Checked Jackson too, it behaves like Gson does