Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0fc0edfd authored by Jesse Wilson's avatar Jesse Wilson
Browse files

Always escape chars that JavaScript treats as newlines.

Related GSON Bug: http://code.google.com/p/google-gson/issues/detail?id=341
Change-Id: I41b1615c4b3b4526f2223a75191f9b0699cdda12
parent 2ad31117
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -407,6 +407,11 @@ public final class JsonWriter implements Closeable {
             * quotation marks except for the characters that must be escaped:
             * quotation mark, reverse solidus, and the control characters
             * (U+0000 through U+001F)."
             *
             * We also escape '\u2028' and '\u2029', which JavaScript interprets
             * as newline characters. This prevents eval() from failing with a
             * syntax error.
             * http://code.google.com/p/google-gson/issues/detail?id=341
             */
            switch (c) {
                case '"':
@@ -435,6 +440,11 @@ public final class JsonWriter implements Closeable {
                    out.write("\\f");
                    break;

                case '\u2028':
                case '\u2029':
                    out.write(String.format("\\u%04x", (int) c));
                    break;

                default:
                    if (c <= 0x1F) {
                        out.write(String.format("\\u%04x", (int) c));
+9 −0
Original line number Diff line number Diff line
@@ -289,6 +289,15 @@ public final class JsonWriterTest extends TestCase {
                + "\"\\u0019\"]", stringWriter.toString());
    }

    public void testUnicodeLineBreaksEscaped() throws IOException {
        StringWriter stringWriter = new StringWriter();
        JsonWriter jsonWriter = new JsonWriter(stringWriter);
        jsonWriter.beginArray();
        jsonWriter.value("\u2028 \u2029");
        jsonWriter.endArray();
        assertEquals("[\"\\u2028 \\u2029\"]", stringWriter.toString());
    }

    public void testEmptyArray() throws IOException {
        StringWriter stringWriter = new StringWriter();
        JsonWriter jsonWriter = new JsonWriter(stringWriter);