• gsala

    gsala

    1 year ago
    I'm running into some issues with generic or polymorphic types:
    @Serializable
    data class Folder(
        val content: SingleOrCollection<File>
    )
    
    @Serializable
    data class File(val name: String)
    
    @Serializable
    sealed class SingleOrCollection<T> {
    
        @Serializable
        @SerialName("single")
        class Single<T>(val data: T) : SingleOrCollection<T>()
    
        @Serializable
        @SerialName("collection")
        class Collection<T>(val data: List<T>) : SingleOrCollection<T>()
    
    }
    I get a an exception when trying to serialize 
    Folder(SingleOrCollection.Single(File(fileName)))
    with
    json.encodeToString(folder)
    kotlinx.serialization.SerializationException: Class 'File' is not registered for polymorphic serialization in the scope of 'Any'.
    Mark the base class as 'sealed' or register the serializer explicitly.
    It seems looks like a pretty simple use case. Does anyone know what the issue could be?
    gsala
    p
    5 replies
    Copy to Clipboard
  • Matthew Cachia

    Matthew Cachia

    1 year ago
    @Serializable
    data class Request(
        @Serializable(with = EitherSerializer::class)
        val personOrCar: Either<Person, Car>,
        @Serializable(with = EitherSerializer::class)
        val catOrDog: Either<Cat, Dog>
    )
    I can easily specify my custom serializer to this sealed, third-party
    Either
    class. However,
    @Serializable
    data class Request(
        //@Serializable(???)
        val map: Map<String, Either<Cat, Dog>>
    )
    Besides some other ideas†, Is there a way I can specify a serializer for my entry value? --- † one idea is to use a custom type to replace
    Map
    and define its own custom serializer. But this is definitely not ideal.
    Matthew Cachia
    Dominaezzz
    3 replies
    Copy to Clipboard
  • n

    Nikolay Kasyanov

    1 year ago
    hey folks, is anyone experiencing else
    e: Compilation failed: Unbound symbols not allowed
    with K/N after bumping
    kotlinx.serialization
    to 1.1.0? Kotlin 1.4.31 is used,
    kotlinx.serialization
    1.0.1 works fine.
    n
    4 replies
    Copy to Clipboard
  • c

    chao

    1 year ago
    Hello friends, does anyone know a good tool that will convert JSON Schema to Kotlinx-serialization annotated kotlin class, and preserves the property order even if JSON schema does not require any order? • https://github.com/wuseal/JsonToKotlinClass - I tried by it is crashing for my JSON schema • https://quicktype.io/ This does allow unstandard propertyOrder based on https://github.com/quicktype/quicktype/issues/336, but requires changing the JSON schema.
    c
    c
    2 replies
    Copy to Clipboard
  • b

    bjonnh

    1 year ago
    I'm sure this has been asked 20 times, but how can I serialize something like :
    data class Vega(
        val background: StringOrMoreComplex
    )
    b
    e
    69 replies
    Copy to Clipboard
  • v79

    v79

    1 year ago
    How do I add kotlinx serialization to a subproject? I think I need to add it to build.gradle.kts
    subprojects { apply { plugin(...) } }
    but that certainly doesn't work. I don't even know what the plugin ID is. NOTE: nothing to do with multiplatform/mobile/iOS; this is a libGDX game project.
    v79
    2 replies
    Copy to Clipboard
  • c

    crummy

    1 year ago
    Tutorials refer to
    org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$serialization_version
    . But they seem to be using an older $serialization_version as me - I've got 1.1.0 but get "Could not find org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:1.1.0". Has this been replaced with another library or am I missing a repo?
    c
    e
    4 replies
    Copy to Clipboard
  • Karlo Lozovina

    Karlo Lozovina

    1 year ago
    I often have to deserialize objects with fields that are possibly null, for example lots of them are
    Boolean?
    , and if the field is null, that's equivalent to it being
    false
    . Is there a way to get rid of nulls, and encode them as the field being false?
    Karlo Lozovina
    Javier
    6 replies
    Copy to Clipboard
  • Dariusz Kuc

    Dariusz Kuc

    1 year ago
    hello 👋 it appears that
    ListSerializer
    only accepts a single serializer - any idea whether it is possible to specify list of different serializers to be used?
    Dariusz Kuc
    2 replies
    Copy to Clipboard
  • j

    Jonathan Olsson

    1 year ago
    Hi! I was wondering if there is any community initiative for encoding/decoding commonly used jdk classes such as BigDecimal and Date with kotlinx.serialization? Browsing the internet it seems as if this is not the case but that rather people are writing their own serializers. Is this by design or further down the priority list? (Jackson by comparison has jackson-module-kotlinto support kotlin classes). Thanks!
    j
    Dominaezzz
    +1
    8 replies
    Copy to Clipboard