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;
}
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;
}
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;
}
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);
}
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;
}
Aggregations