0

I am trying to set a property for my feature which is a list of string objects. Which for the feature has to be converted to a jsonElement. But when I try to get the result I get a serialization error for a ArrayList instead.

The error is the following:

Request error kotlinx.serialization.SerializationException: Serializer for class 'ArrayList' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
    at kotlinx.serialization.internal.PlatformKt.serializerNotRegistered(Platform.kt:32)
    at kotlinx.serialization.SerializersKt__SerializersJvmKt.serializer(SerializersJvm.kt:59)
    at kotlinx.serialization.SerializersKt.serializer(Unknown Source)

With the following code:

private fun buildFeature(id: String?, geometry: Geometry, stations: List<String>): Feature {
    val feature = Feature(geometry = geometry, id = id)

    if (stations.isNotEmpty()) {
        feature.setJsonProperty("stations", asJsonObject(stations))
    }

    return feature
}

The asJsonObject is from the HTTP4K library with the following code from them:

override fun asJsonObject(input: Any): JsonElement =
        json.encodeToJsonElement(json.serializersModule.serializer(input::class.java), input)

The Feature GeoJson object is from the Spatial K geojson library. What needs to be changed or added for the serialization to work?

MrAndre
  • 811
  • 1
  • 10
  • 26
  • How is `asJsonObject` defined? – k314159 Jul 29 '22 at 10:56
  • @k314159 asJsonObject is from the http4k library – MrAndre Jul 29 '22 at 10:59
  • 1
    I don't know why your original doesn't work, but you can fix it by replacing `asJsonObject(stations)` with `Json.encodeToJsonElement(stations)`. (`import kotlinx.serialization.json.Json` and `import kotlinx.serialization.json.encodeToJsonElement`) – k314159 Jul 29 '22 at 11:28

1 Answers1

0
HttpClient(Android) {
        defaultRequest {
            contentType(io.ktor.http.ContentType.Application.Json)
        }
}

Adding content type helped me resolve this.