hello :wave: any idea why `kotlinx-serialization` ...
# serialization
d
hello đź‘‹ any idea why
kotlinx-serialization
wraps some json elements in
"
making them strings?, i.e. given generic
KSerializer
Copy code
object AnyKSerializer : KSerializer<Any> {
    override val descriptor: SerialDescriptor = buildClassSerialDescriptor("Any")
   ...
    override fun deserialize(decoder: Decoder): Any {
        val jsonDecoder = decoder as JsonDecoder
        val element = jsonDecoder.decodeJsonElement() // why primitives are always strings?

        return deserializeJsonElement(element)
    }

    private fun deserializeJsonElement(element: JsonElement): Any = when (element) {
        is JsonObject -> {
            element.mapValues { deserializeJsonElement(it.value) }
        }
        is JsonArray -> {
            element.map { deserializeJsonElement(it) }
        }
        is JsonPrimitive -> element.content
    }
}
and a simple data class
Copy code
@Serializable
data class AnyMap(
    val data: Map<String, @Serializable(with = AnyKSerializer::class) Any>
)
trying to deserialize
Copy code
{
  "data": {
    "intVal": 123
  }
}
but
jsonDecoder.decodeJsonElement()
converts 123 int to "123" string any ideas?
r
When you call
element.content
it will always return a
String
. You can call
toInt()
or
toDouble()
(or the nullable variants) to get one of those types instead, but you won’t know statically whether you can do that safely if your type is
Any
.
d
hmm indeed
element
is a String guess part of the confusion was the kdoc that is says it is a given element without quotes
Copy code
/**
 * Content of given element without quotes. For [JsonNull] this methods returns `null`
 */
public abstract val content: String
thanks
to add to this -> we can check
element.isString()
whether we should be converting the underlying content string or not