Search in sources :

Example 1 with JsonParseException

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

the class AdapterPolymorphic method deserialize.

@Override
public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    if (!json.isJsonObject()) {
        throw new JsonParseException("A polymorph must be an object.");
    }
    JsonObject jsonObject = json.getAsJsonObject();
    if (!jsonObject.has(TYPE)) {
        throw new JsonParseException("A polymorph must be have a \"" + TYPE + "\" field.");
    }
    if (!jsonObject.has(VALUE)) {
        throw new JsonParseException("A polymorph must be have a \"+VALUE+\" field.");
    }
    String type = ((JsonPrimitive) jsonObject.get(TYPE)).getAsString();
    Class<?> typeClass = null;
    try {
        typeClass = Class.forName(type);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        throw new JsonParseException(e.getMessage());
    }
    return context.deserialize(jsonObject.get(VALUE), typeClass);
}
Also used : JsonPrimitive(com.massivecraft.massivecore.xlib.gson.JsonPrimitive) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) JsonParseException(com.massivecraft.massivecore.xlib.gson.JsonParseException)

Aggregations

JsonObject (com.massivecraft.massivecore.xlib.gson.JsonObject)1 JsonParseException (com.massivecraft.massivecore.xlib.gson.JsonParseException)1 JsonPrimitive (com.massivecraft.massivecore.xlib.gson.JsonPrimitive)1