Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
Serializing Lists with external kotlinx Serializer

So, I have this class Item.kt

class Item {
    val name = ""
    val loc = ""
    val price = 0.0
    override fun toString() = "$name <$loc> $price"
}

Since this class is in another library (I can't edit its source), I have an external Serializer for it.

ItemSerializer.kt

@Serializer(forClass = Item::class)
object ItemSerializer: KSerializer<Item> {
    override fun serialize(output: Encoder, obj: Item) {

    }

    override fun deserialize(input: Decoder): Item {
        return df.parse(input.decode())
    }
}

Now, comes the hard part. I can use this class in another class a shown below

Cart.kt

@Serializable
class Cart {
    val id: Long? = null
    @Serialize(with=ItemSerializer::class)
    val item:Item = Item()
}

but I dont know how to make a utilize my serializer when ever I use a list of item objects. e.g.

Cart.kt

@Serializable
class Cart {
    val id: Long? = null
    @Serialize(with=ItemSerializer::class) // doesn't work
    val items = mutableListOf<Item>()
}

How should I go about it using kotlinx serialization? do I have to write a whole new lib for serializing a list and a map of the Item implementation?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

for now, just add a file annotation statement like this at the very beginning of your file (before your package name)

@file:useSerializer(ItemSerializer::class)
package blah.blah
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!