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.

How/Can I use a custom KSerializer?

See original GitHub issue

Is it possible to use a custom defined KSerializer to store an object that does not have the @Serializable annotation defined? For example, let’s say I want to store a Date using the kotlinxPref delegate – basing this on the https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#serializing-3rd-party-classes example.

I’ve defined a KSerializer in my Kotlin file.

@file:UseSerializers(DateAsLongSerializer::class)

object DateAsLongSerializer : KSerializer<Date> {
    override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("Date", PrimitiveKind.LONG)
    override fun serialize(encoder: Encoder, value: Date) = encoder.encodeLong(value.time)
    override fun deserialize(decoder: Decoder): Date = Date(decoder.decodeLong())
}

And created a SerializersModule so that I can set that in the json extension property.

val module = SerializersModule {
    contextual(DateAsLongSerializer)
}

And then defined a SimpleKrate as;

class DateKrate(context: Context) : SimpleKrate(context) {
        init {
            json = Json { serializersModule = module }
        }

        var currentDate: Date? by kotlinxPref("current-date")
    }

But when I try to assign a value to that Krate I get the following exception. Serializer for class 'Date' is not found. Mark the class as @Serializable or provide the serializer explicitly.

Any ideas of what I might be doing wrong here? (Thanks for creating this great library btw)

Edit: The alternative is to just store it as a private longPref and define a getter/setter which casts the value to and from a long. Just curious though on the use of the KSerializer.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
zsmb13commented, Sep 24, 2021

Ah, got it. Back in 1.0.0, the publishing script I was using was incorrect, and pulled in too many things as a compile dependency, exposing them transitively despite them being listed as implementation dependencies. I fixed this in later versions.

1reaction
zsmb13commented, Sep 21, 2021

You can follow the upcoming release here https://github.com/AutSoft/Krate/pull/24

Got a bunch of other things to include in it too

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson - Custom Serializer | Baeldung
This quick tutorial will show how to serialize a Java entity with Jackson 2 using a Custom Serializer. If you want to dig...
Read more >
How do I use a custom Serializer with Jackson? - Stack Overflow
You can put @JsonSerialize(using = CustomDateSerializer.class) over any date field of object to be serialized.
Read more >
Creating custom serializer and deserializer classes - IBM
You can create custom Jackson serializer and deserializer classes to use instead of the default classes. Then, create a custom ObjectMapper to use...
Read more >
How To Write a Custom Serializer with Jackson - Atomic Spin
The first step in creating a custom serializer is to extend Jackson's JsonSerializer . This abstract class includes a method called serialize, ...
Read more >
How to implement a custom serializer using the Jackson ...
We can implement a custom serializer using the StdSerializer class and need to override the serialize(T value, JsonGenerator gen, ...
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