Hi, I wrote the following line: ``` val command ...
# getting-started
m
Hi, I wrote the following line:
Copy code
val command = """{"type":"Modify", "content":{"levels": { "$identifier": {"$propertyName":"$value"}}}}"""
This is obviously very bad code as any of the variables can easily break JSON. What would be "kotlin-way"? I'm looking for some concise solution. 🙂 Thanks PS: I use Kotlin 1.1.4
k
Copy code
fun String.escape() = this.replace('\t', "\\t").replace('\n',"\\n")...
, see this for everything you need to replace: https://www.freeformatter.com/json-escape.html
and then
Copy code
val command = {"type":"Modify", "content":{"levels": { "${identifier.escape()}": {"${propertyName.escape()}":"${value.escape()}"}}}}
Altrough a better solution would be to use an actual Json library.
👍 2