Daniele B
09/24/2021, 1:24 AM@Serializable
data class RpcRequest(
@SerialName("jsonrpc") val jsonrpc : String,
@SerialName("id") val id : Int,
@SerialName("method") val method : String,
@SerialName("params") val params : List<@Contextual Any>?,
)
where params
could be a list of any kind of object
I currently get this error:
Serializer for class 'Any' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
An example of RpcRequest object:
val req = RpcRequest("2.0",2342,"getData",listOf("erwrwee",{"encoding":"base64"}))
How can it be properly serialized?itnoles
09/24/2021, 2:58 AMDaniele B
09/24/2021, 3:11 AMRobert Jaros
09/24/2021, 4:23 AMDaniele B
09/24/2021, 12:36 PMDaniele B
09/24/2021, 2:19 PM@Contextual
, I get a compile error:
Serializer has not been found for type 'Any'. To use context serializer as fallback, explicitly annotate type or property with @Contextual
If I annotate with @Contextual
, I get a runtime error:
Serializer for class 'Any' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
Robert Jaros
09/24/2021, 2:28 PMSerializer
for Any
within your Json
instance?Robert Jaros
09/24/2021, 2:30 PMobject JsonAnySerializer : KSerializer<Any> {
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("kotlin.Any")
override fun deserialize(decoder: Decoder): Any {
return decoder.decodeString()
}
override fun serialize(encoder: Encoder, value: Any) {
encoder.encodeString(value.toString())
}
}
val json = Json {
serializersModule = serializersModuleOf(Any::class, JsonAnySerializer)
}
json.encodeToString(RpcRequest("a", 2, "b", listOf("ABC", 5)))
Robert Jaros
09/24/2021, 2:31 PMDaniele B
09/24/2021, 2:52 PMDaniele B
09/24/2021, 2:53 PM@SerialName("params") val params : JsonArray?,
Moritz Post
09/24/2021, 3:09 PMMoritz Post
09/24/2021, 3:09 PMAny
type should be supported by for basic types imoCLOVIS
09/24/2021, 7:02 PM