The code is based on the following link: Link
I'm getting a run time exception:
java.lang.UnsupportedOperationException: JsonObject when running the following code:
data class Student(
var name: String? = null,
var address: String? = null) {
}
class StudentDeserializer : JsonDeserializer<Student> {
override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): Student {
json as JsonObject
val name = json.asString
val addressJson = json.get("address")
val address = if (addressJson.isJsonObject) addressJson.toString() else addressJson.asString
return Student(name, address)
}
}
the error is in:
val name = json.asString
which is part of deserialize method that is probably being called when i'm writing:
val student = gSon.fromJson<Student>(json2, Student::class.java)
When standing over this line and pressing the ctrl + b keyboard, it takes me to JsonElement class and I see the following method:
public String getAsString() {
throw new UnsupportedOperationException(getClass().getSimpleName());
}
as we can see, there is only a throw statement here. But if we read the method documentation we will see that it is written there that the throw statement happen is only part of the method.
/**
* convenience method to get this element as a string value.
*
* @return get this element as a string value.
* @throws ClassCastException if the element is of not a {@link JsonPrimitive} and is not a valid
* string value.
* @throws IllegalStateException if the element is of the type {@link JsonArray} but contains
* more than a single element.
*/
public String getAsString() {
...
My questions are:
- Where is the full implementation?
- Why when I click on the ctrl + b in a code like:
someJsonObject.asString , that I guess that it's a property, it takes me to method with the name:
getAsStringDoes it mean that the get() implementation of asString property is: return getAsString(this)?