Custom serializer for nested arrays
See original GitHub issueWhat 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:
- Created 4 years ago
- Reactions:2
- Comments:8 (3 by maintainers)
Top GitHub Comments
Here you go!
EDIT: Haven’t tested it but I’m sure you get the gist of it.
You can just do
ListSerializer().descriptor
now.