Unconditionally escape unicode newline characters.
Fixes issue 341.
This commit is contained in:
parent
7dca724292
commit
d3a4b48ad9
@ -431,6 +431,10 @@ public final class JsonWriter implements Closeable {
|
|||||||
* quotation marks except for the characters that must be escaped:
|
* quotation marks except for the characters that must be escaped:
|
||||||
* quotation mark, reverse solidus, and the control characters
|
* quotation mark, reverse solidus, and the control characters
|
||||||
* (U+0000 through U+001F)."
|
* (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) {
|
switch (c) {
|
||||||
case '"':
|
case '"':
|
||||||
@ -471,6 +475,11 @@ public final class JsonWriter implements Closeable {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case '\u2028':
|
||||||
|
case '\u2029':
|
||||||
|
out.write(String.format("\\u%04x", (int) c));
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if (c <= 0x1F) {
|
if (c <= 0x1F) {
|
||||||
out.write(String.format("\\u%04x", (int) c));
|
out.write(String.format("\\u%04x", (int) c));
|
||||||
|
@ -290,6 +290,15 @@ public final class JsonWriterTest extends TestCase {
|
|||||||
+ "\"\\u0019\"]", stringWriter.toString());
|
+ "\"\\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 {
|
public void testEmptyArray() throws IOException {
|
||||||
StringWriter stringWriter = new StringWriter();
|
StringWriter stringWriter = new StringWriter();
|
||||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||||
|
Loading…
Reference in New Issue
Block a user