diff options
author | Feng Xiao <xfxyjwf@gmail.com> | 2014-11-25 17:09:07 -0800 |
---|---|---|
committer | Feng Xiao <xfxyjwf@gmail.com> | 2014-11-25 17:09:07 -0800 |
commit | 90f2f50233fc42a9e6f551ac8454c46a5df00454 (patch) | |
tree | f3fac7914d42d836b50ee186a08a488b30311ec4 /java | |
parent | 209840eca7eafc42a1c03128aca40471ed45717f (diff) |
Make Java TextFormat.printToUnicodeString() escape newlines.
Fix issue #77
Diffstat (limited to 'java')
-rw-r--r-- | java/src/main/java/com/google/protobuf/TextFormat.java | 3 | ||||
-rw-r--r-- | java/src/test/java/com/google/protobuf/TextFormatTest.java | 18 |
2 files changed, 15 insertions, 6 deletions
diff --git a/java/src/main/java/com/google/protobuf/TextFormat.java b/java/src/main/java/com/google/protobuf/TextFormat.java index 63e62fc6..4f6756ed 100644 --- a/java/src/main/java/com/google/protobuf/TextFormat.java +++ b/java/src/main/java/com/google/protobuf/TextFormat.java @@ -411,7 +411,8 @@ public final class TextFormat { generator.print("\""); generator.print(escapeNonAscii ? escapeText((String) value) : - escapeDoubleQuotesAndBackslashes((String) value)); + escapeDoubleQuotesAndBackslashes((String) value) + .replace("\n", "\\n")); generator.print("\""); break; diff --git a/java/src/test/java/com/google/protobuf/TextFormatTest.java b/java/src/test/java/com/google/protobuf/TextFormatTest.java index 82f9582f..eba06ca0 100644 --- a/java/src/test/java/com/google/protobuf/TextFormatTest.java +++ b/java/src/test/java/com/google/protobuf/TextFormatTest.java @@ -864,15 +864,15 @@ public class TextFormatTest extends TestCase { assertEquals(message.getOptionalString(), builder.getOptionalString()); } - public void testPrintToUnicodeStringWithNewlines() { + public void testPrintToUnicodeStringWithNewlines() throws Exception { // 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() .setOptionalString("test newlines\n\nin\nstring") .build())); // 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() .setOptionalString("\ntest\nnewlines\n\nin\nstring\n") .build())); @@ -882,14 +882,22 @@ public class TextFormatTest extends TestCase { TextFormat.printToUnicodeString(TestAllTypes.newBuilder() .setOptionalString("") .build())); - assertEquals("optional_string: \"\n\"\n", + assertEquals("optional_string: \"\\n\"\n", TextFormat.printToUnicodeString(TestAllTypes.newBuilder() .setOptionalString("\n") .build())); - assertEquals("optional_string: \"\n\n\"\n", + assertEquals("optional_string: \"\\n\\n\"\n", TextFormat.printToUnicodeString(TestAllTypes.newBuilder() .setOptionalString("\n\n") .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() { |