Search in sources :

Example 1 with JsonNull

use of com.massivecraft.massivecore.xlib.gson.JsonNull in project MassiveCore by MassiveCraft.

the class AdapterMassiveX method deserialize.

@Override
public T deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
    ParameterizedType ptype = (ParameterizedType) type;
    /*// TODO: Temporary Debug
		if (MUtil.getStackTraceString().contains("com.massivecraft.factions.entity.FactionColl.init"))
		{
			typeDebug(ptype);
			typeDebug(getSuperType(ptype));
			typeDebug(getSuperType(getSuperType(ptype)));
		}*/
    // Calculate def
    Class<?> clazz = getClazz(ptype);
    boolean def = Def.class.isAssignableFrom(clazz);
    // If this is a Def ...
    if (def) {
        // ... then deserialize it as if it were the regular Java collection!
        // SUPER TYPE x2 EXAMPLE: MassiveListDef --> MassiveList --> ArrayList
        Object parent = context.deserialize(json, getSuperType(getSuperType(ptype)));
        return create(parent, def, json, type, context);
    } else // If this a regular Massive structure and not a Def ...
    {
        // ... and the json is null or a JsonNull ...
        if (json == null || json instanceof JsonNull) {
            // ... then deserialize as a null!
            return null;
        } else // ... and it's non null and contains something ...
        {
            // ... then deserialize it as if it were the regular java collection!
            // SUPER TYPE x1 EXAMPLE: MassiveList --> ArrayList
            Object parent = context.deserialize(json, getSuperType(ptype));
            return create(parent, def, json, type, context);
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) JsonNull(com.massivecraft.massivecore.xlib.gson.JsonNull)

Example 2 with JsonNull

use of com.massivecraft.massivecore.xlib.gson.JsonNull in project MassiveCore by MassiveCraft.

the class AdapterEntry method deserialize.

@Override
public Entry<?, ?> deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
    // NULL
    if (json == null)
        return null;
    if (json instanceof JsonNull)
        return null;
    JsonArray jsonArray = (JsonArray) json;
    JsonElement keyJson = jsonArray.get(0);
    JsonElement valueJson = jsonArray.get(1);
    Type keyType = getKeyType(type);
    Type valueType = getValueType(type);
    Object key = context.deserialize(keyJson, keyType);
    Object value = context.deserialize(valueJson, valueType);
    return new SimpleEntry<>(key, value);
}
Also used : JsonArray(com.massivecraft.massivecore.xlib.gson.JsonArray) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JsonElement(com.massivecraft.massivecore.xlib.gson.JsonElement) SimpleEntry(java.util.AbstractMap.SimpleEntry) JsonNull(com.massivecraft.massivecore.xlib.gson.JsonNull)

Aggregations

JsonNull (com.massivecraft.massivecore.xlib.gson.JsonNull)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 JsonArray (com.massivecraft.massivecore.xlib.gson.JsonArray)1 JsonElement (com.massivecraft.massivecore.xlib.gson.JsonElement)1 Type (java.lang.reflect.Type)1 SimpleEntry (java.util.AbstractMap.SimpleEntry)1