I'm trying to deserialize a JSON file using custom deserialization from Gson but I guess I'm failing to do it, nothing is being deserialized. Here's my code
Please also guide me if I'm using incorrect return type. I'm not sure if I'm doing it correctly as well.
GsonHelper.java
public <T> T ProcessData(Class<T> ClassType, String Data)
{
Gson gson = new GsonBuilder().registerTypeAdapter(ClassType , new
JsonDeserializerHelper(ClassType)).create();
return gson.fromJson(Data,ClassType);
}
JsonDeserializerHelper.java
public class JsonDeserializerHelper implements JsonDeserializer {
private Class<?> InstantiatedClass;
private static final Logger logger =
Logger.getLogger(JsonDeserializerHelper.class.getName());
public JsonDeserializerHelper(Class instantiatedClass) {
this.InstantiatedClass = instantiatedClass;
}
#Override
public Object deserialize(JsonElement json, Type type,
JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = (JsonObject) json;
// Implementation here
}
return null;
}
}
Also whenever I try to return a generic type of T, I can't .
What I try to achieve is to have a generic function that returns any class based on the instantiated class in it's private class.
The variable that is consuming the functions afterall
Class<?> AnonymousClass = Class.forName(ClassName);
ProcessClassHelper helperClass = new ProcessClassHelper();
Object ParsedData = (AccountInfo)helperClass.ProcessData(AnonymousClass,json);
ArrayList<AccountInfo> test = (ArrayList<AccountInfo>) ParsedData;
for (AccountInfo acc : test)
{
logger.info("First Name: " + acc.getFirstName());
}
Also the last logger outputs nothing.
Related
When reading a JSON :
{"field":"value"}
into a String field :
public class Test {
private String field;
}
using Gson.fromJson it works as intended and the member String field gets the value "value".
My question is, is there a way to read the same JSON into a custom class so that the custom class object can be constructed with the String value? e.g.
public class Test {
private MyField<String> field;
}
public class MyField<T> {
private T value;
public MyField(T v) {
value = v;
}
}
The reason being the String class is final and cannot be extended, yet I don't want the JSON to be changed into this :
{"field":{"value":"value"}}
If there is a way to extend the String class, it is the best. Otherwise, will need a way for Gson to read string into a custom class that can be constructed by string. Something to do with writing a custom TypeAdapter?
You can use custom JsonDeserializer, JsonSerializer. Here is simple demo version:
static class MyFieldAsValueTypeAdapter<T> implements
JsonDeserializer<MyField<T>>, JsonSerializer<MyField<T>> {
private Gson gson = new Gson();
#Override
public MyField<T> deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context)
throws JsonParseException {
JsonObject obj = new JsonObject();
obj.add("value", json);
return gson.fromJson(obj, typeOfT);
}
#Override
public JsonElement serialize(MyField<T> src, Type typeOfSrc,
JsonSerializationContext context) {
return context.serialize(src.getValue());
}
}
public static void main(String[] args) {
GsonBuilder b = new GsonBuilder();
b.registerTypeAdapter(MyField.class , new MyFieldAsValueTypeAdapter());
Gson gson = b.create();
String json = "{\"field\":\"value1\"}";
Test test = gson.fromJson(json, Test.class);
}
Be careful with internal Gson gson = new Gson(). If you have some other setup, you will need to register it on internal version or pass default MyField deserializer/serializer to your custom implementation.
Whenever I get error, the error body is as follows:
[
{
"errorCode": 10001,
"resource": null,
"resourceId": null,
"field": null,
"parameter": null,
"header": null,
"allowedValues": null,
"maxLength": null,
"minLength": null
}
]
The error body is an array. I have different bodies for success of many API methods, but the error array response is standardized. I tried doing many things
making wrapper class with generic type success response and array of error response and made deserializer for that, but I can't deserialize from type variable and from paramaterized class.
made a ErrorDeserializer but I have no idea how can I make Retrofit use it for error responses.
I could definitely just serialize raw string everytime on every callback for all my api methods, but I have so many of them, I need generalized solution. If I didn't explain myself properly, please ask.
I'll add examples of what I tried (they will be incomplete however):
Response wrap class:
public class ResponseWrap<T> {
#Nullable
private final T response;
#Nullable
private final List<ErrorResponse> errorResponses;
public ResponseWrap(#Nullable T response, #Nullable List<ErrorResponse> errorResponses) {
this.response = response;
this.errorResponses = errorResponses;
}
}
Error response class:
public class ErrorResponse {
private int errorCode;
private String resource;
private String resourceId;
private String field;
private String parameter;
private String header;
private String allowedValues;
private int maxLength;
private int minLength;
// getters and setters
}
Error deserializer:
public class ErrorDeserializer implements JsonDeserializer<ArrayList<ErrorResponse>> {
#Override
public ArrayList<ErrorResponse> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
Gson gson = new Gson();
Type listType = new TypeToken<ArrayList<ErrorResponse>>(){}.getType();
ArrayList<ErrorResponse> list = new Gson().fromJson(json, listType);
final JsonArray jsonArray = json.getAsJsonArray();
for (int i = 0; i < jsonArray.size(); i++) {
ErrorResponse error = new ErrorResponse();
JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
error.setErrorCode(jsonObject.get("errorCode").getAsInt());
error.setResource(jsonObject.get("resource").getAsString());
error.setResourceId(jsonObject.get("resourceId").getAsString());
error.setField(jsonObject.get("field").getAsString());
error.setParameter(jsonObject.get("parameter").getAsString());
error.setHeader(jsonObject.get("header").getAsString());
error.setAllowedValues(jsonObject.get("allowedValues").getAsString());
error.setMaxLength(jsonObject.get("maxLength").getAsInt());
error.setMinLength(jsonObject.get("minLength").getAsInt());
list.add(error);
}
return list;
}
}
Response wrap deserializer - it's not working, 2 errors:
List error = new Gson().fromJson(jsonObject.getAsJsonObject("error"), ArrayList.class); // Can't select from parameterized class
T success = new Gson().fromJson(jsonObject, T.class); // Can't select from type variable
public class ResponseWrapDeserializer<T> implements JsonDeserializer<ResponseWrap<T>> {
#Override
public ResponseWrap<T> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
// Get JsonObject
final JsonObject jsonObject = json.getAsJsonObject();
if (jsonObject.has("error")) {
Gson gson = new GsonBuilder()
.registerTypeAdapter(typeOfT, new ErrorDeserializer())
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss")
.create();
List<ErrorResponse> error = new Gson().fromJson(jsonObject.getAsJsonObject("error"), ArrayList<ErrorResponse>.class);
return new ResponseWrap<T>(null, error);
} else {
T success = new Gson().fromJson(jsonObject, T.class);
return new ResponseWrap<T>(success, null);
}
}
}
The idea was to use them all like this:
#POST("Login")
Call<ResponseWrap<AccessTokenResponse>> Login(#Body LoginRequest request);
But I can't because of above mentioned reasons.
The question is: How to process error responses in a generic way that are in an array using Retrofit2?
You cannot write T.class -- this is illegal in Java. In order to overcome this limitation you must either pas a Type instance yourself somehow or resolve generic types parameters from what Gson gives you. In the first case you'd need dozen JSON deserializers to bind various ResponseWrap<T> parametization; whilst in the second case can simply resolve the actual type parameter yourself. At the call site you can use TypeTokens -- a special Gson mechanism to define a type parameter via a type parameterization. Also note that you don't have to instantiate internal Gson instances: this might be relatively expensive (especially in sequence) and disrespect the Gson configuration the current deserializer is bound for - use JsonDeserializationContext since it can give you all you need (except downstream type adapters).
The following JSON deserializer uses the second approach as I find it more convenient.
final class ResponseWrapJsonDeserializer<T>
implements JsonDeserializer<ResponseWrap<T>> {
// This deserializer holds no state, so we can hide its instantiation details away
private static final JsonDeserializer<ResponseWrap<Object>> responseWrapJsonDeserializer = new ResponseWrapJsonDeserializer<>();
// Type instances from TypeToken seems to be fully immutable and can be treated as value types, thus we can make them static final to re-use (it's safe)
private static final Type errorResponseListType = new TypeToken<List<ErrorResponse>>() {
}.getType();
private ResponseWrapJsonDeserializer() {
}
// Just cheating the call site: we always return the same instance if the call site requests for a specially typed deserializer (it's always the same instance however, this is just how Java generics work)
static <T> JsonDeserializer<ResponseWrap<T>> getResponseWrapJsonDeserializer() {
#SuppressWarnings({ "rawtypes", "unchecked" })
final JsonDeserializer<ResponseWrap<T>> cast = (JsonDeserializer) responseWrapJsonDeserializer;
return cast;
}
#Override
public ResponseWrap<T> deserialize(final JsonElement jsonElement, final Type type, final JsonDeserializationContext context)
throws JsonParseException {
// Checking if jsonElement looks like an error (I'm not sure if it's possible to check HTTP statuses delegating them to request/response converters in Retrofit)
if ( isError(jsonElement) ) {
final List<ErrorResponse> errorResponses = context.deserialize(jsonElement, errorResponseListType);
return new ResponseWrap<>(null, errorResponses);
}
// If it does not look an error, then:
// * resolve what's the actual T in the given ResponseWrap<T>
// * deserialize the JSON tree as an instance of T -- it's like we're stripping the wrapper and then instantiate the wrap due to our rules
final T response = context.deserialize(jsonElement, resolveTypeParameter0(type));
return new ResponseWrap<>(response, null);
}
private static Type resolveTypeParameter0(final Type type) {
// The given type does not have parameterization?
if ( !(type instanceof ParameterizedType) ) {
// Then it's raw, simply <Object> or <?>
return Object.class;
}
// If it's parameterized, let's take it's first parameter as ResponseWrap is known to a have a single type parameter only
return ((ParameterizedType) type).getActualTypeArguments()[0];
}
// Some AI party here, he-he
private static boolean isError(final JsonElement jsonElement) {
if ( !jsonElement.isJsonArray() ) {
return false;
}
final JsonArray jsonArray = jsonElement.getAsJsonArray();
for ( final JsonElement innerJsonElement : jsonArray ) {
if ( !innerJsonElement.isJsonObject() ) {
return false;
}
final JsonObject innerJsonObject = innerJsonElement.getAsJsonObject();
final boolean looksLikeErrorObject = innerJsonObject.has("errorCode");
if ( !looksLikeErrorObject ) {
return false;
}
}
return true;
}
}
Next, register the deserializer for your Gson instance:
private static final Gson gson = new GsonBuilder()
.registerTypeAdapter(ResponseWrap.class, getResponseWrapJsonDeserializer())
.create();
And test it with
success.json
{
"foo": [1, 2, 3]
}
failure.json
[
{"errorCode": 10001},
{"errorCode": 10002}
]
// It's a constant
// Also, ResponseWrap<Map<String,List<Integer>>>.class is illegal in Java
private static final Type type = new TypeToken<ResponseWrap<Map<String, List<Integer>>>>() {
}.getType();
public static void main(String... args)
throws IOException {
final String successJson = getPackageResourceString(Q43525433.class, "success.json");
final String failureJson = getPackageResourceString(Q43525433.class, "failure.json");
final ResponseWrap<Map<String, List<Integer>>> success = gson.fromJson(successJson, type);
final ResponseWrap<Map<String, List<Integer>>> failure = gson.fromJson(failureJson, type);
System.out.println("SUCCESS: " + success.response);
for ( final ErrorResponse response : failure.errorResponses ) {
System.out.println("FAILURE: " + response.errorCode);
}
}
The output:
SUCCESS: {foo=[1, 2, 3]}
FAILURE: 10001
FAILURE: 10002
And yes, don't forget to add gson to Retrofit using GsonConverterFactory.create(gson).
Also, you might be interested in Json response parser for Array or Object that describe the almost the same issue but from another perspective.
I am trying to get Gson to deserialize a nested object in my JSON into a class that implements an interface.
Here is my interface:
public interface ActivationFunction {
public float activate(float input);
}
I have an implementing class named LinearActivation and a Layer class that has a class variable of type ActivationFunction. Here is my JSON:
{
"layers" : [
{
"input":6,
"output":2,
"weights":[[1,2,3,4,5,6],[7,8,9,10,11,12]],
"function":{"LinearFunction"
}
]
}
I read this post: Polymorphism with gson and I searched Gson docs here: https://github.com/google/gson/blob/master/UserGuide.md#TOC-Writing-a-Deserializer
but I can't find any documentation on creating a typeHierarchyAdapter. I followed the example in the first link, but I am not sure how INSTANCE and CLASSNAME should be used within my JSON structure.
Here is the type hierarchy adapter:
public class ActivationFunctionAdapter implements JsonDeserializer<ActivationFunction> {
private static final String CLASSNAME = "CLASSNAME";
private static final String INSTANCE = "INSTANCE";
#Override
public ActivationFunction deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
JsonPrimitive prim = (JsonPrimitive) jsonObject.get(CLASSNAME);
String className = prim.getAsString();
Class<?> klass = null;
try {
klass = Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new JsonParseException(e.getMessage());
}
return context.deserialize(jsonObject.get(INSTANCE), klass);
}
}
Any help or guidance would be much appreciated
I was able to get this to work by following the guide here:
https://futurestud.io/tutorials/how-to-deserialize-a-list-of-polymorphic-objects-with-gson
also same info can be seen in the answer by Perception in this question: Using Gson with Interface Types
Basically just had to add the RuntimeTypeAdapterFactory class to my project, (which the code is available in the extras package in the Gson github), then define a type token and use it. For example:
TypeToken<ActivationFunction> functionTypeToken = new TypeToken<ActivationFunction>() {};
RuntimeTypeAdapterFactory<ActivationFunction> typeFactory =
RuntimeTypeAdapterFactory.of(ActivationFunction.class, "type")
.registerSubtype(LinearFunction.class, "linear");
final Gson gson = new GsonBuilder().registerTypeAdapterFactory(typeFactory).create();
I have a number of classes that implement a single interface type. I want to write a custom deserializer to be able to handle some special case with the json I have to deserialize. Is this possible with google gson? Here is the sample code I have so far:
Class Type:
public class MyClass implements MyInterface {
.......
}
Deserializer
public class ResponseDeserializer implements JsonDeserializer<MyInterface>
{
private Gson fGson;
public ResponseDeserializer()
{
fGson = new Gson();
}
#Override
public MyInterface deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
String jsonString = json.toString();
if(jsonString.substring(0, 0).equals("["))
{
jsonString = "{ \"parameter\":" + jsonString + "}";
}
return context.deserialize(json, typeOfT);
}
}
register and call fromJson method:
#Override
public <T extends MyInterface> T createResponse(Class<T> responseType)
{
T returnObject = new GsonBuilder().registerTypeAdapter(MyInterface.class, new ResponseDeserializer())
.create().fromJson(getEntityText(), responseType);
return returnObject;
}
thanks for the help in advance
First of all, registerTypeAdapter() is not covariant; if you want to link a TypeAdapter to an interface, you have to use a TypeAdapterFactory. See: How do I implement TypeAdapterFactory in Gson?
Secondly, why do you think that [{...},{...},{...}] is not valid Json? Of course it is:
{
"foo":[
{
"type":"bar"
},
{
"type":"baz"
}
]
}
This is a mapping of key foo an array of objects, with one member variable. Gson would automatically deserialize it with the following POJOs:
public class MyObject {
List<TypedObject> foo;
}
public class TypedObject {
String type;
}
Beyond that, I can't help you more without knowing your specific Json string. This (especially that first link) should be enough to get started, however.
I have never done much with serialization, but am trying to use Google's gson to serialize a Java object to a file. Here is an example of my issue:
public interface Animal {
public String getName();
}
public class Cat implements Animal {
private String mName = "Cat";
private String mHabbit = "Playing with yarn";
public String getName() {
return mName;
}
public void setName(String pName) {
mName = pName;
}
public String getHabbit() {
return mHabbit;
}
public void setHabbit(String pHabbit) {
mHabbit = pHabbit;
}
}
public class Exhibit {
private String mDescription;
private Animal mAnimal;
public Exhibit() {
mDescription = "This is a public exhibit.";
}
public String getDescription() {
return mDescription;
}
public void setDescription(String pDescription) {
mDescription = pDescription;
}
public Animal getAnimal() {
return mAnimal;
}
public void setAnimal(Animal pAnimal) {
mAnimal = pAnimal;
}
}
public class GsonTest {
public static void main(String[] argv) {
Exhibit exhibit = new Exhibit();
exhibit.setAnimal(new Cat());
Gson gson = new Gson();
String jsonString = gson.toJson(exhibit);
System.out.println(jsonString);
Exhibit deserializedExhibit = gson.fromJson(jsonString, Exhibit.class);
System.out.println(deserializedExhibit);
}
}
So this serializes nicely -- but understandably drops the type information on the Animal:
{"mDescription":"This is a public exhibit.","mAnimal":{"mName":"Cat","mHabbit":"Playing with yarn"}}
This causes real problems for deserialization, though:
Exception in thread "main" java.lang.RuntimeException: No-args constructor for interface com.atg.lp.gson.Animal does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
I get why this is happening, but am having trouble figuring out the proper pattern for dealing with this. I did look in the guide but it didn't address this directly.
Here is a generic solution that works for all cases where only interface is known statically.
Create serialiser/deserialiser:
final class InterfaceAdapter<T> implements JsonSerializer<T>, JsonDeserializer<T> {
public JsonElement serialize(T object, Type interfaceType, JsonSerializationContext context) {
final JsonObject wrapper = new JsonObject();
wrapper.addProperty("type", object.getClass().getName());
wrapper.add("data", context.serialize(object));
return wrapper;
}
public T deserialize(JsonElement elem, Type interfaceType, JsonDeserializationContext context) throws JsonParseException {
final JsonObject wrapper = (JsonObject) elem;
final JsonElement typeName = get(wrapper, "type");
final JsonElement data = get(wrapper, "data");
final Type actualType = typeForName(typeName);
return context.deserialize(data, actualType);
}
private Type typeForName(final JsonElement typeElem) {
try {
return Class.forName(typeElem.getAsString());
} catch (ClassNotFoundException e) {
throw new JsonParseException(e);
}
}
private JsonElement get(final JsonObject wrapper, String memberName) {
final JsonElement elem = wrapper.get(memberName);
if (elem == null) throw new JsonParseException("no '" + memberName + "' member found in what was expected to be an interface wrapper");
return elem;
}
}
make Gson use it for the interface type of your choice:
Gson gson = new GsonBuilder().registerTypeAdapter(Animal.class, new InterfaceAdapter<Animal>())
.create();
Put the animal as transient, it will then not be serialized.
Or you can serialize it yourself by implementing defaultWriteObject(...) and defaultReadObject(...) (I think thats what they were called...)
EDIT See the part about "Writing an Instance Creator" here.
Gson cant deserialize an interface since it doesnt know which implementing class will be used, so you need to provide an instance creator for your Animal and set a default or similar.
#Maciek solution works perfect if the declared type of the member variable is the interface / abstract class. It won't work if the declared type is sub-class / sub-interface / sub-abstract class unless we register them all through registerTypeAdapter(). We can avoid registering one by one with the use of registerTypeHierarchyAdapter, but I realize that it will cause StackOverflowError because of the infinite loop. (Please read reference section below)
In short, my workaround solution looks a bit senseless but it works without StackOverflowError.
#Override
public JsonElement serialize(T object, Type interfaceType, JsonSerializationContext context) {
final JsonObject wrapper = new JsonObject();
wrapper.addProperty("type", object.getClass().getName());
wrapper.add("data", new Gson().toJsonTree(object));
return wrapper;
}
I used another new Gson instance of work as the default serializer / deserializer to avoid infinite loop. The drawback of this solution is you will also lose other TypeAdapter as well, if you have custom serialization for another type and it appears in the object, it will simply fail.
Still, I am hoping for a better solution.
Reference
According to Gson 2.3.1 documentation for JsonSerializationContext and JsonDeserializationContext
Invokes default serialization on the specified object passing the specific type information. It should never be invoked on the element received as a parameter of the JsonSerializer.serialize(Object, Type, JsonSerializationContext) method. Doing so will result in an infinite loop since Gson will in-turn call the custom serializer again.
and
Invokes default deserialization on the specified object. It should never be invoked on the element received as a parameter of the JsonDeserializer.deserialize(JsonElement, Type, JsonDeserializationContext) method. Doing so will result in an infinite loop since Gson will in-turn call the custom deserializer again.
This concludes that below implementation will cause infinite loop and cause StackOverflowError eventually.
#Override
public JsonElement serialize(Animal src, Type typeOfSrc,
JsonSerializationContext context) {
return context.serialize(src);
}
I had the same problem, except my interface was of primitive type (CharSequence) and not JsonObject:
if (elem instanceof JsonPrimitive){
JsonPrimitive primitiveObject = (JsonPrimitive) elem;
Type primitiveType =
primitiveObject.isBoolean() ?Boolean.class :
primitiveObject.isNumber() ? Number.class :
primitiveObject.isString() ? String.class :
String.class;
return context.deserialize(primitiveObject, primitiveType);
}
if (elem instanceof JsonObject){
JsonObject wrapper = (JsonObject) elem;
final JsonElement typeName = get(wrapper, "type");
final JsonElement data = get(wrapper, "data");
final Type actualType = typeForName(typeName);
return context.deserialize(data, actualType);
}