Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle escape string for strings containing quotes #146

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package dev.yorkie.document.json
internal fun escapeString(str: String): String {
return str.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("\'", "\\'")
.replace("\n", "\\n")
.replace("\r", "\\r")
.replace("\b", "\\b")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal object JsonStringifier {
is CrdtObject -> {
buffer.append("{")
forEach { (key, value) ->
buffer.append(""""$key":""")
buffer.append(""""${escapeString(key)}":""")
value.toJsonStringInternal(buffer)
if (key != last().first) {
buffer.append(",")
Expand Down
38 changes: 38 additions & 0 deletions yorkie/src/test/kotlin/dev/yorkie/document/DocumentTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.yorkie.document

import com.google.gson.JsonParser
import dev.yorkie.assertJsonContentEquals
import dev.yorkie.document.json.JsonArray
import dev.yorkie.document.json.JsonText
Expand Down Expand Up @@ -265,4 +266,41 @@ class DocumentTest {
assertEquals(0, target.garbageLength)
}
}

@Test
fun `should handle escape string for strings containing single quotes`() {
runTest {
target.updateAsync { root, _ ->
root["str"] = "I'm Yorkie"
}.await()
assertEquals("""{"str":"I'm Yorkie"}""", target.toJson())
assertEquals(
"I'm Yorkie",
JsonParser.parseString(target.toJson()).asJsonObject.get("str").asString,
)

target.updateAsync { root, _ ->
root["str"] = """I\\'m Yorkie"""
}.await()
assertEquals("""{"str":"I\\\\'m Yorkie"}""", target.toJson())
assertEquals(
"""I\\'m Yorkie""",
JsonParser.parseString(target.toJson()).asJsonObject.get("str").asString,
)
}
}

@Test
fun `should handle escape string for object keys`() {
runTest {
target.updateAsync { root, _ ->
root["""it"s"""] = "Yorkie"
}.await()
assertEquals("""{"it\"s":"Yorkie"}""", target.toJson())
assertEquals(
"Yorkie",
JsonParser.parseString(target.toJson()).asJsonObject.get("""it"s""").asString,
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class StringEscapingTest {
@Test
fun `should escape a string with case1`() {
val target = "1\\2\"3'4\n5\r6\t7\b8\u000C9\u20280\u2029"
val escaped = "1\\\\2\\\"3\\'4\\n5\\r6\\t7\\b8\\f9\\u20280\\u2029"
val escaped = "1\\\\2\\\"3'4\\n5\\r6\\t7\\b8\\f9\\u20280\\u2029"
assertEquals(escaped, escapeString(target))
}

Expand Down
Loading