kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

Have a problem when encode unescaped backslash

Open pank-su opened this issue 1 year ago • 2 comments

I have no idea how to implement backslash removal in jsondecoder.

kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 23: Invalid escaped char ' ' at path: $[0].name
 JSON input: [{"id":3321206,"name":"Axwell /\ Ingrosso"}]

One possible solution that doesn't work.

object ArtistNameSerializer : JsonTransformingSerializer<String>(String.serializer()) {
    override fun transformSerialize(element: JsonElement): JsonElement {
        return Json.decodeFromString<JsonObject>(
            element.toString().replace("/", "")
                .replace("\\", "")
                .removePrefix("\"").removeSuffix("\"")
        )
    }

}

pank-su avatar May 07 '24 13:05 pank-su

@pank-su The problem here is that "\ " is not a valid escape sequence. I assume that you have data that contains these sequences. Unfortunately processing escape sequences is not something that can not be done using regular expressions. You'd have to actually write a simple parser with some state that knows about being in text and where in an escape it is. It can then translate invalid escapes to something that isn't invalid (either the followup character, or an escaped ''). You can do this without fully parsing json.

pdvrieze avatar May 07 '24 20:05 pdvrieze

Yes, \ is incorrect input. You shouldn't have it in your Json. Or, if it is a part of the data, the backslash itself should be escaped with another backslash. I.e. valid inputs are Axwell /\\ Ingrosso, or Axwell / Ingrosso, but not Axwell /\ Ingrosso.

sandwwraith avatar Jun 05 '24 15:06 sandwwraith