@UseSerializers ignores nullable serializer
See original GitHub issueWhen using UseSerializers
with a serializer of a nullable type, the build fails.
Cause: Back-end (JVM) Internal error: Serializer for element of type Date has not been found.
The same serializer works when specified explicitly on a property using @Serializable(with=...)
.
To Reproduce
@file:UseSerializers(NullableDateSerializer::class)
/* ... */
@Serializable
class Foo(val nullable: Date?)
/* ... */
@Serializer(forClass = Date::class)
class NullableDateSerializer : KSerializer<Date?> {
/* ... */
}
Expected behavior
The specified serializer should be used for both nullable and non-null values.
Environment
- Kotlin version: 1.1
- Library version: 0.11.0
- Kotlin platforms: JVM
- Gradle version: 5.1.1
- IDE version (if bug is related to the IDE) Android Studio 3.4.1
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Kotlinx Serialization - Custom serializer to ignore null value
Use encodeDefaults = false property in JsonConfiguration and it won't serialize nulls (or other optional values).
Read more >serializerOrNull - Kotlin
Returns null if serializer cannot be created (provided type or its type ... Serializers for classes with generic parameters are ignored by this...
Read more >decodeElementIndex - GitHub Pages
Decodes the index of the next element to be decoded. Index represents a position of the current element in the serial descriptor element...
Read more >Serializer fields - Django REST framework
Any 'read_only' fields that are incorrectly included in the serializer input will be ignored. Set this to True to ensure that the field...
Read more >Custom serializer to ignore null value-kotlin
[Solved]-Kotlinx Serialization - Custom serializer to ignore null value-kotlin · score:2. Accepted answer · score:1. JsonConfiguration is deprecated in favor of ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m working with a bad API that sometimes returns empty strings for some data types (dates, URLs) instead of nulls.
KSerializer<Date>
won’t work for me, because it won’t let me returnnull
fromdeserialize()
or handle null values inserialize()
.Related: https://github.com/Kotlin/kotlinx.serialization/issues/984