From e89ce83a014e097ab8586df3c90d1c634a1323e7 Mon Sep 17 00:00:00 2001 From: Charann <61188435+charann01@users.noreply.github.com> Date: Sun, 25 Aug 2024 23:41:42 +0530 Subject: [PATCH] As per json spec, there is no need to escape a single quote (#4) * As per json spec, there is no need to escape a single quote * Added testcase for single quote behaviour json stringify --- .../src/korlibs/io/serialization/json/Json.kt | 2 +- .../test/korlibs/io/serialization/json/JsonTest.kt | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/korlibs-serialization/src/korlibs/io/serialization/json/Json.kt b/korlibs-serialization/src/korlibs/io/serialization/json/Json.kt index c88335c..05cb49e 100644 --- a/korlibs-serialization/src/korlibs/io/serialization/json/Json.kt +++ b/korlibs-serialization/src/korlibs/io/serialization/json/Json.kt @@ -245,7 +245,7 @@ open class Json { b.append('"') for (c in str) { when (c) { - '\\' -> b.append("\\\\"); '/' -> b.append("\\/"); '\'' -> b.append("\\'") + '\\' -> b.append("\\\\"); '/' -> b.append("\\/"); '"' -> b.append("\\\""); '\b' -> b.append("\\b"); '\u000c' -> b.append("\\f") '\n' -> b.append("\\n"); '\r' -> b.append("\\r"); '\t' -> b.append("\\t") else -> b.append(c) diff --git a/korlibs-serialization/test/korlibs/io/serialization/json/JsonTest.kt b/korlibs-serialization/test/korlibs/io/serialization/json/JsonTest.kt index 4e8c5b7..b15f47e 100644 --- a/korlibs-serialization/test/korlibs/io/serialization/json/JsonTest.kt +++ b/korlibs-serialization/test/korlibs/io/serialization/json/JsonTest.kt @@ -46,4 +46,12 @@ class JsonTest { fun decodeUnicode() { assertEquals("aeb", Json.parse(""" "a\u0065b" """)) } + + @kotlin.test.Test + fun stringify1(){ + assertEquals( + """{"str":"'","int":1,"bool":true}""", + Json.stringify(Json.parse("""{"str":"'","int":1,"bool":true}""")) + ) + } }