org.kyojo.schemaorg.m3n3.gson.core.container.NumChildrenDeserializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kyojo-schemaorg-m3n3-gson Show documentation
Show all versions of kyojo-schemaorg-m3n3-gson Show documentation
A Java implementation of schema.org data structure
The newest version!
package org.kyojo.schemaorg.m3n3.gson.core.container;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import org.kyojo.gson.JsonDeserializationContext;
import org.kyojo.gson.JsonDeserializer;
import org.kyojo.gson.JsonElement;
import org.kyojo.gson.JsonObject;
import org.kyojo.gson.JsonParseException;
import org.kyojo.gson.reflect.TypeToken;
import org.kyojo.schemaorg.m3n3.core.impl.NUM_CHILDREN;
import org.kyojo.schemaorg.m3n3.core.Container.NumChildren;
public class NumChildrenDeserializer implements JsonDeserializer {
@Override
public NumChildren deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context)
throws JsonParseException {
if(jsonElement.isJsonPrimitive()) {
try {
return new NUM_CHILDREN(jsonElement.getAsLong());
} catch(NumberFormatException nfe) {
return null;
}
}
JsonObject jsonObject = jsonElement.getAsJsonObject();
NumChildren obj = new NUM_CHILDREN();
HashMap fldMap = new HashMap<>();
Field[] flds = NUM_CHILDREN.class.getFields();
for(Field fld : flds) {
fldMap.put(fld.getName(), fld);
}
for(Entry ent : jsonObject.entrySet()) {
if(fldMap.containsKey(ent.getKey())) {
Field fld = fldMap.get(ent.getKey());
JsonElement elm = ent.getValue();
try {
if(fld.getType().equals(List.class)) {
ParameterizedType gType = (ParameterizedType)fld.getGenericType();
Type[] aTypes = gType.getActualTypeArguments();
Type listType = TypeToken.getParameterized(ArrayList.class, (Class)aTypes[0]).getType();
List list = context.deserialize(elm, listType);
fld.set(obj, list);
} else {
Object val = context.deserialize(elm, fld.getType());
fld.set(obj, val);
}
} catch(IllegalArgumentException iae) {
throw new JsonParseException(iae);
} catch(IllegalAccessException iae) {
throw new JsonParseException(iae);
}
}
}
return obj;
}
}