Search in sources :

Example 1 with JsonArray

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

the class AdapterBannerPatterns method deserialize.

// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
// Default serialization is fine.
// We do however provide a smarter deserialiser.
// This is for the sole purpose of upgrading from the old deprecated database format.
// 
// In the old version (around version 2.8.10) we made use of a list with primitives.
// id, color, id, color...
// They were just coming in that order and were not wrapped in some kind of entry.
// 
// The data types are the same in the old and new version.
// String, Number, String, Number...
// 
// In the new version we do however wrap them in the DataBannerPattern.
@Override
public MassiveListDef<DataBannerPattern> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    // Create
    MassiveListDef<DataBannerPattern> ret = new MassiveListDef<>();
    // Null (Def implementation is never null)
    if (json == null)
        return ret;
    if (json.equals(JsonNull.INSTANCE))
        return ret;
    // It is an array in both old and new version
    JsonArray array = json.getAsJsonArray();
    // Empty?
    if (array.size() == 0)
        return ret;
    // First element indicates version
    JsonElement first = array.get(0);
    if (first instanceof JsonObject) {
        // New
        for (JsonElement element : array) {
            DataBannerPattern dataBannerPattern = context.deserialize(element, DataBannerPattern.class);
            ret.add(dataBannerPattern);
        }
    } else {
        // Old aka Upgrade Mode
        Iterator<JsonElement> iterator = array.iterator();
        while (iterator.hasNext()) {
            DataBannerPattern dataBannerPattern = new DataBannerPattern();
            JsonElement idElement = iterator.next();
            String id = idElement.getAsString();
            dataBannerPattern.setId(id);
            JsonElement colorElement = iterator.next();
            Integer color = colorElement.getAsInt();
            dataBannerPattern.setColor(color);
            ret.add(dataBannerPattern);
        }
    }
    return ret;
}
Also used : JsonArray(com.massivecraft.massivecore.xlib.gson.JsonArray) DataBannerPattern(com.massivecraft.massivecore.item.DataBannerPattern) MassiveListDef(com.massivecraft.massivecore.collections.MassiveListDef) JsonElement(com.massivecraft.massivecore.xlib.gson.JsonElement) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject)

Example 2 with JsonArray

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

the class AdapterEntry method serialize.

// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public JsonElement serialize(Entry<?, ?> src, Type type, JsonSerializationContext context) {
    // NULL
    if (src == null)
        return JsonNull.INSTANCE;
    // Create Ret
    JsonArray ret = new JsonArray();
    // Fill Ret
    Object key = src.getKey();
    Object value = src.getValue();
    Type keyType = getKeyType(type);
    Type valueType = getValueType(type);
    JsonElement keyJson = context.serialize(key, keyType);
    JsonElement valueJson = context.serialize(value, valueType);
    ret.add(keyJson);
    ret.add(valueJson);
    // Return Ret
    return ret;
}
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)

Example 3 with JsonArray

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

the class GsonMongoConverter method gson2MongoArray.

public static BasicDBList gson2MongoArray(JsonElement inElement) {
    JsonArray in = inElement.getAsJsonArray();
    BasicDBList out = new BasicDBList();
    for (int i = 0; i < in.size(); i++) {
        JsonElement element = in.get(i);
        if (element.isJsonArray()) {
            out.add(gson2MongoArray(element));
        } else if (element.isJsonObject()) {
            out.add(gson2MongoObject(element));
        } else {
            out.add(gson2MongoPrimitive(element));
        }
    }
    return out;
}
Also used : JsonArray(com.massivecraft.massivecore.xlib.gson.JsonArray) BasicDBList(com.massivecraft.massivecore.xlib.mongodb.BasicDBList) JsonElement(com.massivecraft.massivecore.xlib.gson.JsonElement)

Example 4 with JsonArray

use of com.massivecraft.massivecore.xlib.gson.JsonArray 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)

Example 5 with JsonArray

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

the class GsonEqualsChecker method arrayEquals.

// The argument one must be JsonArray, and can not be null.
// The argument twoObject may be anything, even null.
public static boolean arrayEquals(JsonArray one, Object twoObject) {
    // Null check (one can't ever be null)
    if (twoObject == null)
        return false;
    // Object identity speedup
    if (one == twoObject)
        return true;
    // twoObject must be JsonArray
    if (!(twoObject instanceof JsonArray))
        return false;
    // Cast to JsonArray
    JsonArray two = (JsonArray) twoObject;
    // Size must be the same
    int size = one.size();
    if (two.size() != size)
        return false;
    // And each element index must be the same
    for (int i = 0; i < size; i++) {
        if (!equals(one.get(i), two.get(i)))
            return false;
    }
    return true;
}
Also used : JsonArray(com.massivecraft.massivecore.xlib.gson.JsonArray)

Aggregations

JsonArray (com.massivecraft.massivecore.xlib.gson.JsonArray)6 JsonElement (com.massivecraft.massivecore.xlib.gson.JsonElement)4 JsonObject (com.massivecraft.massivecore.xlib.gson.JsonObject)2 BasicDBList (com.massivecraft.massivecore.xlib.mongodb.BasicDBList)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 MassiveListDef (com.massivecraft.massivecore.collections.MassiveListDef)1 DataBannerPattern (com.massivecraft.massivecore.item.DataBannerPattern)1 JsonNull (com.massivecraft.massivecore.xlib.gson.JsonNull)1 BasicDBObject (com.massivecraft.massivecore.xlib.mongodb.BasicDBObject)1 DBObject (com.massivecraft.massivecore.xlib.mongodb.DBObject)1 SimpleEntry (java.util.AbstractMap.SimpleEntry)1