Hi, I updated kotlin version form 1.7.20 to 1.8.21...
# serialization
m
Hi, I updated kotlin version form 1.7.20 to 1.8.21 im my project, and while I try to parse json I get an error.
Expected start of the array '[', but had 'EOF' instead at path: $
This code works properly on kotlin 1.7.20 but throws an exception on 1.8.21 (1.8.20 too)
inline fun <reified T> String.listFromJson() = json.decodeFromString<List<T>>(this)
In both cases I use the latest
1.5.1
library. What different between versions? Where I can find what changes have been made?
c
The String being parsed is empty, resulting in that exception (it’s expecting a JSON array). Issue may not be with serialization - it’s doing what’s expected given that input data, you’ll need to track down why that string is empty.
m
Thank you for your response Chris, meanwhile I found working solution
Copy code
inline fun <reified T> String.listFromJson(): List<T>? =
    json.decodeFromString(json.parseToJsonElement(this).jsonPrimitive.content)
I have no idea why this works
c
Another option to deal with empty input:
Copy code
inline fun <reified T> String.listFromJson() {
    if( isEmpty()) {
        return emptyList<T>()
    }
    return json.decodeFromString<List<T>>(this)
}
though it’s worth understanding why the code is attempting to deserialize an empty string in the first place, likely upstream challenges.