question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom serializer for nested arrays

See original GitHub issue

What is your use-case and why do you need this feature?

Hello, first of all, please let me know if this issue is not subject to you or you don’t have anything to do. Sorry for any inconvenience in advance!

I am having a server response as follows:

 "baseCapacity": 10,
 "distanceToPolygon": 37633,
 "polygon": [
     [
         52.399182775989175,
         12.84729402512312
      ]

I can serialize polygon field if I use like List<List<Double>> in my data class. However, I’d like to have a separate data class that only contains double values if possible. What I mean;

Working approach:

data class Polygons (
    val baseCapacity: Int,
    val distanceToPolygon: Int,
    val polygon: List<List<Dobule>>
)

What I want:

data class Polygons (
    val baseCapacity: Int,
    val distanceToPolygon: Int,
    @Serializable(with = PolygonSerializer::class)
    val polygon: List<Polygon>
)

data class Polygon (
    val values: List<Double>
)

I tried to write a custom serializer:

object PolygonSerializer : KSerializer<String> {

    override val descriptor: SerialDescriptor
        get() = StringDescriptor.withName("polygon")

    override fun serialize(encoder: Encoder, obj: String) {
        //
    }

    override fun deserialize(decoder: Decoder): String {
        val jsonInput = decoder as JsonInput
        return jsonInput.decodeString()
    }
}

However, I am getting Invalid JSON at 83: Expected string or non-null literal

I want to ask if is there any way to ignore field in Polygon data class and achieve the same result like List<List<Double>>

Describe the solution you’d like

This could be available however I couldn’t find any solution on the internet.

Desired solution: A custom serializer that ignores key and returns only values. At the end of the day, I am expecting to have a list of list of doubles.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

16reactions
Dominaezzzcommented, Mar 3, 2020

Here you go!

data class Polygons (
    val baseCapacity: Int,
    val distanceToPolygon: Int,
    val polygon: List<Polygon>
)

@Serializable(with = PolygonSerializer::class)
data class Polygon (
    val values: List<Double>
)

object PolygonSerializer : KSerializer<Polygon> {
    private val serializer = ArrayListSerializer(Double.serializer())

    override val descriptor: SerialDescriptor = StringDescriptor.withName("Polygon")

    override fun serialize(encoder: Encoder, obj: Polygon) {
        encoder.encode(serializer, obj.values)
    }

    override fun deserialize(decoder: Decoder): Polygon {
        return Polygon(decoder.decode(serializer))
    }
}

EDIT: Haven’t tested it but I’m sure you get the gist of it.

2reactions
Dominaezzzcommented, May 6, 2022

You can just do ListSerializer().descriptor now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

serialization - How to deserialize a nested array of objects ...
How to deserialize a nested array of objects declared on the constructor via promoted properties, with Symfony Serializer? ; $encoders = [new ...
Read more >
Mapping Nested Values with Jackson
In this quick tutorial, we'll look at how to map nested values with Jackson to flatten out a complex data structure. We'll deserialize...
Read more >
SerializeJSON
A struct that contains metadata information for nested structs. Items. Array of datatypes when setting the metadata for serializing elements in ...
Read more >
serialize nested objects with JsonUtility
If a field isn't displayed, then JsonUtility won't serialize it. In your LevelInfo struct, nested arrays and optional types aren't supported.
Read more >
How can I generate arrays [...] to serialize an object? I want ...
Assuming your question is how to generate arrays [...] using this coding pattern, the answer is to use new List<Object> for those e.g.:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found