cbeust/klaxon

Not escaping quotes or orther special characters when calling parseArray

Closed this issue · 1 comments

Can you add character escaping to parseArray? (same same as in Issue #91 for toJsonString)

Kotlin Klaxon Version 5.6

Notice "lolipop"" in generated JSON (output of FreeMarker template):

[
    {
	"type": "CANDY_PACK",
	"items": [
	    {
		"type": "CANDY",
		"text": "lolipop""
	    }
        ]
    }
]

Usage: klaxon.parseArray<MyObject>(generatedString) ?: emptyList()

Context:
I got a FreeMarker template which generates a JSON-string. The applied variables may contain quotes. After generating, I parse the JSON-string with klaxon.parseArray<MyObject>(generatedJsonString). I solved the issue by implementing a CustomOutputFormat and call com.beust.klaxon.Render.escapeString manually.

class EscapedJsonOutputFormat : CommonMarkupOutputFormat<EscapedJsonOutputModel>() {
    @Throws(IOException::class, TemplateModelException::class)
    override fun output(textToEsc: String, out: Writer) {
        out.write(Render.escapeString(textToEsc))
    }

    override fun escapePlainText(plainTextContent: String): String {
        return Render.escapeString(plainTextContent)
    }
}

...

Template(
    "Template",
    jsonTemplateString,
    defaultConfig.apply {
        outputFormat = EscapedJsonOutputFormat.INSTANCE
    }
).process(context, out)

Impossible to solve by klaxxon. I must have had a brain fart or something.