I am using kotlinx-serialization.
Let soppose I have this class
@Serializable
class Settings(
val uiScale:Float=1f,
val theme:String="dark",
)
I have serialized this class to a file which the result is
{
"uiScale":1.0,
"theme":"dark"
}
now some folk comes and change the json file content to this
{
"uiScale":2.0, //from 1.0 to 2.0 (accpetable)
"theme":1 //from "dark" to 1 (not acceptable and cause the whole serialization to fail because it must be string)
}
now when I try to deserialize my Settings class
it will throw an exception
Is there any way when a key can’t be deserialized use a default value and make the deserialization success ?
I need this result
{
"uiScale":2.0,
"theme":"dark" //because previous (1) is invalid for string type use default value that defined in data class
}
this is a sample example it could be very nested
Notes
- I know that it could be possible to write a custom serializer for my
Settings
but I want a global solution