Commit 90f2f502 authored by Feng Xiao's avatar Feng Xiao

Make Java TextFormat.printToUnicodeString() escape newlines.

Fix issue #77
parent 209840ec
...@@ -411,7 +411,8 @@ public final class TextFormat { ...@@ -411,7 +411,8 @@ public final class TextFormat {
generator.print("\""); generator.print("\"");
generator.print(escapeNonAscii ? generator.print(escapeNonAscii ?
escapeText((String) value) : escapeText((String) value) :
escapeDoubleQuotesAndBackslashes((String) value)); escapeDoubleQuotesAndBackslashes((String) value)
.replace("\n", "\\n"));
generator.print("\""); generator.print("\"");
break; break;
......
...@@ -864,15 +864,15 @@ public class TextFormatTest extends TestCase { ...@@ -864,15 +864,15 @@ public class TextFormatTest extends TestCase {
assertEquals(message.getOptionalString(), builder.getOptionalString()); assertEquals(message.getOptionalString(), builder.getOptionalString());
} }
public void testPrintToUnicodeStringWithNewlines() { public void testPrintToUnicodeStringWithNewlines() throws Exception {
// No newlines at start and end // No newlines at start and end
assertEquals("optional_string: \"test newlines\n\nin\nstring\"\n", assertEquals("optional_string: \"test newlines\\n\\nin\\nstring\"\n",
TextFormat.printToUnicodeString(TestAllTypes.newBuilder() TextFormat.printToUnicodeString(TestAllTypes.newBuilder()
.setOptionalString("test newlines\n\nin\nstring") .setOptionalString("test newlines\n\nin\nstring")
.build())); .build()));
// Newlines at start and end // Newlines at start and end
assertEquals("optional_string: \"\ntest\nnewlines\n\nin\nstring\n\"\n", assertEquals("optional_string: \"\\ntest\\nnewlines\\n\\nin\\nstring\\n\"\n",
TextFormat.printToUnicodeString(TestAllTypes.newBuilder() TextFormat.printToUnicodeString(TestAllTypes.newBuilder()
.setOptionalString("\ntest\nnewlines\n\nin\nstring\n") .setOptionalString("\ntest\nnewlines\n\nin\nstring\n")
.build())); .build()));
...@@ -882,14 +882,22 @@ public class TextFormatTest extends TestCase { ...@@ -882,14 +882,22 @@ public class TextFormatTest extends TestCase {
TextFormat.printToUnicodeString(TestAllTypes.newBuilder() TextFormat.printToUnicodeString(TestAllTypes.newBuilder()
.setOptionalString("") .setOptionalString("")
.build())); .build()));
assertEquals("optional_string: \"\n\"\n", assertEquals("optional_string: \"\\n\"\n",
TextFormat.printToUnicodeString(TestAllTypes.newBuilder() TextFormat.printToUnicodeString(TestAllTypes.newBuilder()
.setOptionalString("\n") .setOptionalString("\n")
.build())); .build()));
assertEquals("optional_string: \"\n\n\"\n", assertEquals("optional_string: \"\\n\\n\"\n",
TextFormat.printToUnicodeString(TestAllTypes.newBuilder() TextFormat.printToUnicodeString(TestAllTypes.newBuilder()
.setOptionalString("\n\n") .setOptionalString("\n\n")
.build())); .build()));
// Test escaping roundtrip
TestAllTypes message = TestAllTypes.newBuilder()
.setOptionalString("\ntest\nnewlines\n\nin\nstring\n")
.build();
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
TextFormat.merge(TextFormat.printToUnicodeString(message), builder);
assertEquals(message.getOptionalString(), builder.getOptionalString());
} }
public void testPrintToUnicodeString_unknown() { public void testPrintToUnicodeString_unknown() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment