• Yao-Wen Mei

    Yao-Wen Mei

    4 months ago
    hi, I want to serialize a custom class into a nested map (with my own encoder, not introducing JSON into my code base). For example, if I have my data classes defined like this:
    @Serializable
    data class Project(val name:String, val owners: List<Users>)
    
    @Serializable
    data class Users(val name: String)
    After serialization, I wish to get a nested map equals to:
    val map = mutableMapOf<String, Any>()
    map.put("name", "project_name")
    map.put("owners", listOf(mapOf("name" to "user1"), mapOf("name" to "user2"))
    Could you please give me some hint how can I implement this
    Encoder
    please ( I do have a
    Decoder
    working BTW)? Should my Encoder extend the
    AbstractEncoder()
    or
    TaggedEncoder()
    ? May I get some document for how to implement this? The official guide only contains an example for serializing to
    List
    , but does not show how to get the key values if I want to serialize to
    Map
    . Thank you very much.
    Yao-Wen Mei
    1 replies
    Copy to Clipboard
  • Alejandro Moya

    Alejandro Moya

    4 months ago
    Hi, quick question, I have and endpoint that has a dynamic property, it could be a
    String
    , an
    Integer
    or a
    Boolean
    , is there a way to achieve that behaviour with serialization?
    Alejandro Moya
    Paul Woitaschek
    7 replies
    Copy to Clipboard
  • r

    ribesg

    4 months ago
    Beware of the default behavior on default values! It can lead to very unexpected results…
    @Serializable
        data class Event(
            val id: String,
            val date: Date = Date.now(),
        )
    
        @Test
        fun test() {
            repeat(100) {
                println(Json.encodeToString(Event("Test")))
            }
        }
    …
    {"id":"Test"}
    {"id":"Test"}
    {"id":"Test","date":"2022-05-18T10:00:47.011Z"}
    {"id":"Test"}
    {"id":"Test"}
    {"id":"Test"}
    …
    r
    n
    6 replies
    Copy to Clipboard
  • o

    Ovsyannikov Alexey

    4 months ago
    Hello everybody 🙂 I wished ask somebody to help me investigate a bug in compile time related to serialization. This bug break my builds already half a year, fortunately the project in the report is a pet-project and I am able to wait a fix, but it is a little bit long-play issue and I wished to ask help here 🙂 thank you
    o
    1 replies
    Copy to Clipboard
  • zt

    zt

    4 months ago
    How would I write a custom serializer for json input that looks like this
    [ { "somekey": { } }, { "someotherkey": { } } ]
    I wanna be able to check the name of the first key in the object and then serialize to a specific class depending on what it is
    zt
    Landry Norris
    +2
    11 replies
    Copy to Clipboard
  • s

    Slackbot

    4 months ago
    This message was deleted.
    s
    Dominaezzz
    2 replies
    Copy to Clipboard
  • Matthew Olsson

    Matthew Olsson

    4 months ago
    Is it possible to deserialize an array into an object? For example, turning something like
    ["Bob", 24]
    into
    class Person(val name: String, val age: Int)
    . Assuming of course that I already know the array corresponds to the
    Person
    class (in reality, my data looks more like
    { "person": ["Bob", 24] }
    Matthew Olsson
    j
    +1
    18 replies
    Copy to Clipboard
  • Jan

    Jan

    4 months ago
    Why doesn't that work you literally can see that I have kotlinx serialization, ktor client and content negotiations:
    Jan
    Big Chungus
    +1
    17 replies
    Copy to Clipboard
  • Pedro Henrique

    Pedro Henrique

    4 months ago
    I'm trying to deserialize a CBOR message that I'm getting over FFI (Project Panama), but I keep getting a
    kotlinx.serialization.cbor.internal.CborDecodingException: Expected start of string, but found 50
    error. The byte array data seems to be the same as the original one in Rust when converting them to a hexadecimal format. I can deserialize normally the same data on Rust through the
    cbor4ii
    library. I also tried using another library (
    cborium
    ) and I still get the same error in the Kotlin side.
    Pedro Henrique
    Dominaezzz
    8 replies
    Copy to Clipboard
  • v

    Václav Škorpil

    4 months ago
    Hi i need to call SOAP api. There is no official library so i am using https://github.com/pdvrieze/xmlutil . Is there a way how to specify more namespaces than one, using this library? My soap request is like this. I need to create a data classes to represent it.
    <?xml version="1.0" encoding="utf-8"?>
    <soapenv:Envelope xmlns:soapenv="<http://schemas.xmlsoap.org/soap/envelope/>" xmlns="<http://www.pds.eu/vOKO/v0200>" xmlns:eph="<http://www.ccv.cz/EPH_GCP01G>">
       <soapenv:Body>
          <Request vOKOid="EPH_GCP01G">
             <RequestContent>
                <eph:REQUEST>
                   <eph:GETDATA>1</eph:GETDATA>
                   <eph:IDLAST>22</eph:IDLAST>
                </eph:REQUEST>
             </RequestContent>
          </Request>
       </soapenv:Body>
    </soapenv:Envelope>
    As you can see, there are more than one namespace in the envelope.I xmlutil is only @XmlSerialName which can specify only one namespace. How should i handle this?
    v
    Dominaezzz
    4 replies
    Copy to Clipboard