Thread
#serialization
    m

    Mendess

    9 months ago
    Is it possible to define a deserializer for a class that can deserialize from 2 different unambiguous formats? Example:
    { "createdAt": 11112222333 }
    { "createdAt": { "seconds": 179879, "nanoseconds": 1201800000 } }
    class DateTime(....)
    
    @Serializable
    class Payload(val createdAt: DateTime)
    (serialization is not needed)
    Dominaezzz

    Dominaezzz

    9 months ago
    Yes this is possible. Just deserialise to JsonElement and do the rest yourself.
    m

    Mendess

    9 months ago
    ah, that makes sense
    let me give that a go
    Dominaezzz

    Dominaezzz

    9 months ago
    Depending on what DateTime looks like, you may be able to get kotlinx-serialisation to do more of the work for you.
    m

    Mendess

    9 months ago
    it has a
    val milliseconds: Long
    in it
    I intend to truncate the nanosecond precision I get from the json
    Dominaezzz

    Dominaezzz

    9 months ago
    Ah fairs. You'll have to DIY then. I was going to suggest surrogate types.
    m

    Mendess

    9 months ago
    is there a better way to do this detail?
    Dominaezzz

    Dominaezzz

    9 months ago
    I though you didn't want to serialize?
    m

    Mendess

    9 months ago
    I don't but I have to implement both methods of the
    KSerializer
    interface, no?
    Dominaezzz

    Dominaezzz

    9 months ago
    Nope, just the one you need.
    You can leave that one as is,.
    m

    Mendess

    9 months ago
    🤔
    Dominaezzz

    Dominaezzz

    9 months ago
    Don't remove the method lol, just leave it with the exception,.
    m

    Mendess

    9 months ago
    ah okay, that's what I meant if there was a better way of doing it besides the exception 😄
    Dominaezzz

    Dominaezzz

    9 months ago
    Ahhh I see. Maybe `TODO()`` but 🤷🏼 .
    m

    Mendess

    9 months ago
    yeah, TODO implies it will be implemented, I guess this conveys more of a "I won't do it"
    Dominaezzz

    Dominaezzz

    9 months ago
    Did you succeed?
    m

    Mendess

    9 months ago
    I did, tho I'm a bit sad that I can't at compile time guarantee that there are no attempts to serialize
    DateTime
    Dominaezzz

    Dominaezzz

    9 months ago
    Ah yeah, can understand that.
    a

    archecraft

    9 months ago
    Isn't there also a DeserializationStrategy interface? I think KSerializer just implements DeserializationStrategy and SerializationStrategy
    Dominaezzz

    Dominaezzz

    9 months ago
    Yes but it has limited use cases, this is not one of them. The
    @Serializable
    needs the full
    KSerializer
    interface.