You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuweni.apache.org by to...@apache.org on 2020/05/13 18:45:56 UTC

[incubator-tuweni] branch master updated: Fix escape on JsonSerializer of toml (#83)

This is an automated email from the ASF dual-hosted git repository.

toulmean pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git


The following commit(s) were added to refs/heads/master by this push:
     new 1de1b65  Fix escape on JsonSerializer of toml (#83)
1de1b65 is described below

commit 1de1b656a09e0772d78b24cc3443e2c2e612edc3
Author: Koudai Aono <ko...@gmail.com>
AuthorDate: Thu May 14 03:45:11 2020 +0900

    Fix escape on JsonSerializer of toml (#83)
    
    * Fix esape on JsonSerializer
    
    * add unittest for toml JsonSerializer
---
 .../org/apache/tuweni/toml/JsonSerializer.java     |  4 ++--
 .../apache/tuweni/toml/MutableTomlTableTest.java   | 22 ++++++++++++++++++++++
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/toml/src/main/java/org/apache/tuweni/toml/JsonSerializer.java b/toml/src/main/java/org/apache/tuweni/toml/JsonSerializer.java
index 8865c2e..39cf50e 100644
--- a/toml/src/main/java/org/apache/tuweni/toml/JsonSerializer.java
+++ b/toml/src/main/java/org/apache/tuweni/toml/JsonSerializer.java
@@ -134,8 +134,8 @@ final class JsonSerializer {
     StringBuilder out = new StringBuilder(text.length());
     for (int i = 0; i < text.length(); i++) {
       char ch = text.charAt(i);
-      if (ch == '\'') {
-        out.append("\\'");
+      if (ch == '\"') {
+        out.append("\\\"");
         continue;
       }
       if (ch >= 0x20) {
diff --git a/toml/src/test/java/org/apache/tuweni/toml/MutableTomlTableTest.java b/toml/src/test/java/org/apache/tuweni/toml/MutableTomlTableTest.java
index 1a9d83c..53afebf 100644
--- a/toml/src/test/java/org/apache/tuweni/toml/MutableTomlTableTest.java
+++ b/toml/src/test/java/org/apache/tuweni/toml/MutableTomlTableTest.java
@@ -192,4 +192,26 @@ class MutableTomlTableTest {
         + "}\n";
     assertEquals(expected.replace("\n", System.lineSeparator()), table.toJson());
   }
+
+  @Test
+  void shouldSerializeToJSONForEscapeCharacter() {
+    MutableTomlTable table = new MutableTomlTable();
+    table.set("foo", "one'two", positionAt(1, 1));
+    table.set("bar", "hello\"there", positionAt(2, 1));
+    table.set("buz", "good\tbye", positionAt(3, 1));
+    table.set("bug", "alpha\bbeta", positionAt(4, 1));
+    table.set("egg", "bread\rham", positionAt(5, 1));
+    table.set("pet", "dog\fcat", positionAt(6, 1));
+    table.set("red", "black\nwhite", positionAt(7, 1));
+    String expected = "{\n"
+        + "  \"bar\" : \"hello\\\"there\",\n"
+        + "  \"bug\" : \"alpha\\bbeta\",\n"
+        + "  \"buz\" : \"good\\tbye\",\n"
+        + "  \"egg\" : \"bread\\rham\",\n"
+        + "  \"foo\" : \"one'two\",\n"
+        + "  \"pet\" : \"dog\\fcat\",\n"
+        + "  \"red\" : \"black\\nwhite\"\n"
+        + "}\n";
+    assertEquals(expected.replace("\n", System.lineSeparator()), table.toJson());
+  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tuweni.apache.org
For additional commands, e-mail: commits-help@tuweni.apache.org