Skip to content

Commit

Permalink
Fixed bug with treating special symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
Iurii Zaitsev committed Oct 9, 2024
1 parent 1aef6ec commit 432a0f3
Showing 1 changed file with 4 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,9 @@ class JsonEncoding {
/**
* Encode a list of strings into a string
*/
fun encode(values: MutableList<String>): String {
var jsonString = Json.encodeToString(
ListSerializer(String.serializer()),
values,
)
// These characters are incorrectly stored in json, so the following substitutions are required
val replacements = mapOf(
"\\n" to "\n",
"\\t" to "\t",
"\\r" to "\r",
"\\\\" to "\\",
"\\\"" to "\"",
"\\'" to "\'",
"\\b" to "\b",
"\\f" to "\u000c",
)
replacements.forEach { (key, value) ->
jsonString = jsonString.replace(key, value)
}
return jsonString
}
fun encode(values: MutableList<String>): String = Json.encodeToString(
ListSerializer(String.serializer()),
values,
)
}
}

0 comments on commit 432a0f3

Please sign in to comment.