question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@UseSerializers ignores nullable serializer

See original GitHub issue

When 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
SpaceBisoncommented, Jul 28, 2019

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 return null from deserialize() or handle null values in serialize().

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found