Thread
#serialization
    k

    kingsley

    1 year ago
    🙏
    Karlo Lozovina

    Karlo Lozovina

    1 year ago
    with regards to 1): does setting
    encodeDefaults
    work?
    k

    kingsley

    1 year ago
    Unfortunately not. I believe
    encodeDefaults
    only works for serializing back to json
    Karlo Lozovina

    Karlo Lozovina

    1 year ago
    oh, you're deserializing, and the JSON is missing a property? for me by default that deserializes to a default value, and I usually set those to
    null
    ...
    Javier

    Javier

    1 year ago
    did you add
    ignoreUnknownKeys = true
    ?
    If it doesn't work, you have to add default parameters
    @Serializable
    data class Some(val property: String? = null)
    Karlo Lozovina

    Karlo Lozovina

    1 year ago
    yes, you must have default parameters!
    k

    kingsley

    1 year ago
    Indeed setting to
    null
    works, but I was hoping to avoid that. Since the field’s already nullable
    ignoreUnknownKeys
    only works for json keys that don’t exist in my model
    Karlo Lozovina

    Karlo Lozovina

    1 year ago
    ignoreUnknownKeys are for a different problem, I'd say
    ah I see, I wanted that as well... if the key is nullable, the default should be null without specifying it excplicitly
    Javier

    Javier

    1 year ago
    Yep, I think I read about this problem in an issue and default parameters was the only option, maybe that issue has been updated and has a global workaround
    Karlo Lozovina

    Karlo Lozovina

    1 year ago
    but I couldn't make it work, don't think there's an option for that - I have
    SomeType? = null
    basically everywhere
    k

    kingsley

    1 year ago
    I see. That’s too bad. But I guess I can live with this
    Javier

    Javier

    1 year ago
    yep, in one project I had to replace it with IntelliJ replace feature because I had a lot of them 😅
    Karlo Lozovina

    Karlo Lozovina

    1 year ago
    as far as 4) goes: there's file level anotation
    @file::UseSerializers
    Javier

    Javier

    1 year ago
    4) should not be solved via JsonModule and add it to the
    Json
    used by jake converter?
    Karlo Lozovina

    Karlo Lozovina

    1 year ago
    for 2) I'm also doing it manually, by implementing a Kserializer for my type... don't think there's an annotation that specifies a default enum value...
    k

    kingsley

    1 year ago
    @Javier I’m using
    serializerModule
    +
    @Contextual
    annotation. But I also have another field of the same type but I want a different serializer for that one. So I have to put
    @Serializable(with=Serializer::class)
    , but I was hoping I could apply this on a custom annotation and then I can do
    @CustomAnnotation
    instead
    @Karlo Lozovina for 2, Moshi has
    EnumJsonAdapter#withUnknownFallback
    , but kotlinx.serialization doesn’t seem to have anything similar