I am calling a method this way:
GsonBuilder gsonBuilder = new GsonBuilder()
.registerTypeAdapter(CoindeskRateResult.class, CurrencyRateDeserializer.class)
CurrencyRateDeserializer is a com.google.gson.JsonDeserializer
import com.google.gson.JsonDeserializer;
public class CurrencyRateDeserializer implements JsonDeserializer<CoindeskRateResult> {
@Override
public CoindeskRateResult deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context)
throws JsonParseException {
CoindeskRateResult result = new CoindeskRateResult();
return result;
}
}
The error occurs in the GsonBuilder
class. I know that typeAdapter
is an instance of JsonDeserializer<?>
but $Gson$Preconditions.checkArgument
throws an IllegalArgumentException
public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
$Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?>
|| typeAdapter instanceof JsonDeserializer<?>
|| typeAdapter instanceof InstanceCreator<?>
|| typeAdapter instanceof TypeAdapter<?>);
....
StackTrace:
*Caused by: java.lang.IllegalArgumentException
at com.google.gson.internal.$Gson$Preconditions.checkArgument($Gson$Preconditions.java:46)
at com.google.gson.GsonBuilder.registerTypeAdapter(GsonBuilder.java:472)*
.....
My CoindeskRateResult class:
public class CoindeskRateResult {
public Map<String, String> data = new HashMap<>();
}
This is wrong:
.registerTypeAdapter(CoindeskRateResult.class, CurrencyRateDeserializer.class)
This is why all of the instanceof
checks fail for the first case. What you're looking for is:
.registerTypeAdapter(CoindeskRateResult.class, new CurrencyRateDeserializer())
To be honest, I think this is a Gson design flaw.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With