Hi,
I made a post on StackOverflow about how to serialize char/character data.
The serialization is working, but not the deserialization.
The complete post is here:
The code is:
private fun createGson(): Gson {
val builder = GsonBuilder()
val customCharSerializer = object : JsonDeserializer<Any?>, JsonSerializer<Char?> {
override fun deserialize(json: JsonElement, typeOfT: Type?, context: JsonDeserializationContext?): Any? {
return try {
Char(json.asJsonPrimitive.asInt)
} catch (e: Exception) {
null
}
}
override fun serialize(src: Char?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement? {
return src?.let { JsonPrimitive(it.code) }
}
}
builder.registerTypeAdapter(Char::class.java, customCharSerializer,)
builder.registerTypeAdapter(Character::class.java, customCharSerializer,)
return builder.create()
}
The error is:
Expecting character, got: 122; at $.r.typeChar