-
Notifications
You must be signed in to change notification settings - Fork 624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optional JsonNull cannot be deserialized (always null, never JsonNull) #2455
Comments
I decompiled the generated serializer and wrote a version of it that works: @Serializable(PropertySerializer::class)
data class Property(val name: String, val defaultValue: JsonElement? = null)
object PropertySerializer : KSerializer<Property> {
private val jsonElementSerializer = JsonElement.serializer()
override val descriptor: SerialDescriptor = buildClassSerialDescriptor(Property::class.qualifiedName!!) {
element("name", String.serializer().descriptor)
element("defaultValue", jsonElementSerializer.descriptor)
}
override fun serialize(encoder: Encoder, value: Property) {
TODO("Not yet implemented")
}
override fun deserialize(decoder: Decoder): Property {
return (decoder as JsonDecoder).decodeStructure(descriptor) {
var parsing = true
var name: String? = null
var defaultValue: JsonElement? = null
while (parsing) {
when (val index = decodeElementIndex(descriptor)) {
CompositeDecoder.DECODE_DONE ->
parsing = false
CompositeDecoder.UNKNOWN_NAME ->
throw SerializationException("unknown field!")
0 ->
name = decodeStringElement(descriptor, index)
1 ->
defaultValue = decodeSerializableElement(descriptor, index, jsonElementSerializer)
}
}
if (name == null) {
throw SerializationException("no name!")
}
Property(name, defaultValue)
}
}
} |
I guess the root cause is, that the JsonElement and JsonPrimitive serializers' descriptors are not nullable. |
pschichtel
added a commit
to pschichtel/idl
that referenced
this issue
Sep 30, 2023
this currently requires the manual implementation of RecordProperty's serializer. This can be simplified once Kotlin/kotlinx.serialization#2455 is resolved.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have a class like this:
The intent is: The default value should be an optional field, however if the field is given with
null
in JSON, then I want it decoded asJsonNull
. That way I could differentiate between "no default value given" and "default value is null".kotlinx.serialization currently always produces
null
in this case if thedefaultValue
field is nullable. If it is not nullable, then I correctly getJsonNull
.An ugly workaround (because it is an incompatible change) would be to have a wrapper object, that has a non-nullable field:
Another ugly workaround, which at least isn't a breaking change, would be to make the defaultValue non-nullable with a dummy default value that signifies that the value wasn't given in JSON:
To Reproduce
I guess the code to reproduce this is fairly clear:
This produces
Expected behavior
Environment
1.9.10
1.6.0
JVM
8.3
The text was updated successfully, but these errors were encountered: