Search in sources :

Example 11 with JsonObject

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

the class GsonMongoConverter method gson2MongoObject.

public static BasicDBObject gson2MongoObject(JsonElement inElement, BasicDBObject out) {
    JsonObject in = inElement.getAsJsonObject();
    for (Entry<String, JsonElement> entry : in.entrySet()) {
        String key = gson2MongoKey(entry.getKey());
        JsonElement val = entry.getValue();
        if (val.isJsonArray()) {
            out.put(key, gson2MongoArray(val));
        } else if (val.isJsonObject()) {
            out.put(key, gson2MongoObject(val));
        } else {
            out.put(key, gson2MongoPrimitive(val));
        }
    }
    return out;
}
Also used : JsonElement(com.massivecraft.massivecore.xlib.gson.JsonElement) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject)

Example 12 with JsonObject

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

Example 13 with JsonObject

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

the class EngineMassiveCoreDatabase method massiveStoreLoginSync.

// Can not be cancelled.
@EventHandler(priority = EventPriority.LOWEST)
public void massiveStoreLoginSync(PlayerLoginEvent event) {
    // Get player id ...
    Player player = event.getPlayer();
    if (MUtil.isntPlayer(player))
        return;
    final String playerId = player.getUniqueId().toString();
    // ... get remote entries ...
    Map<SenderColl<?>, Entry<JsonObject, Long>> remoteEntries = getRemoteEntries(playerId);
    // ... and sync each of them.
    for (Entry<SenderColl<?>, Entry<JsonObject, Long>> entry : remoteEntries.entrySet()) {
        SenderColl<?> coll = entry.getKey();
        Entry<JsonObject, Long> remoteEntry = entry.getValue();
        coll.syncId(playerId, null, remoteEntry);
    }
}
Also used : Player(org.bukkit.entity.Player) Entry(java.util.Map.Entry) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) SenderColl(com.massivecraft.massivecore.store.SenderColl) EventHandler(org.bukkit.event.EventHandler)

Example 14 with JsonObject

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

the class MigratorInventory001StringId method updateField.

public void updateField(JsonObject json, String name) {
    JsonElement element = json.get(name);
    if (element == null)
        return;
    if (!element.isJsonObject())
        return;
    JsonObject object = element.getAsJsonObject();
    MigratorUtil.migrate(DataItemStack.class, object);
}
Also used : JsonElement(com.massivecraft.massivecore.xlib.gson.JsonElement) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject)

Example 15 with JsonObject

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

the class DriverFlatfile method loadAll.

@Override
public Map<String, Entry<JsonObject, Long>> loadAll(Coll<?> coll) {
    // Create Ret
    Map<String, Entry<JsonObject, Long>> ret = null;
    // Get Directory
    File directory = getDirectory(coll);
    if (!directory.isDirectory())
        return ret;
    // Find All
    File[] files = directory.listFiles(JsonFileFilter.get());
    // Create Ret
    ret = new MassiveMap<>(files.length);
    // For Each Found
    for (File file : files) {
        // Get ID
        String id = idFromFile(file);
        // Get Entry
        Entry<JsonObject, Long> entry = loadFile(file);
        // NOTE: The entry can be a failed one with null and 0.
        // NOTE: We add it anyways since it's an informative failure.
        // NOTE: This is supported by our defined specification.
        // Add
        ret.put(id, entry);
    }
    // Return Ret
    return ret;
}
Also used : Entry(java.util.Map.Entry) SimpleEntry(java.util.AbstractMap.SimpleEntry) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) File(java.io.File)

Aggregations

JsonObject (com.massivecraft.massivecore.xlib.gson.JsonObject)21 JsonElement (com.massivecraft.massivecore.xlib.gson.JsonElement)9 SimpleEntry (java.util.AbstractMap.SimpleEntry)4 BasicDBObject (com.massivecraft.massivecore.xlib.mongodb.BasicDBObject)3 Entry (java.util.Map.Entry)3 DataItemStack (com.massivecraft.massivecore.item.DataItemStack)2 JsonPrimitive (com.massivecraft.massivecore.xlib.gson.JsonPrimitive)2 JsonSyntaxException (com.massivecraft.massivecore.xlib.gson.JsonSyntaxException)2 ItemStack (org.bukkit.inventory.ItemStack)2 PlayerInventory (org.bukkit.inventory.PlayerInventory)2 MassiveList (com.massivecraft.massivecore.collections.MassiveList)1 MassiveListDef (com.massivecraft.massivecore.collections.MassiveListDef)1 MassiveMap (com.massivecraft.massivecore.collections.MassiveMap)1 TypeString (com.massivecraft.massivecore.command.type.primitive.TypeString)1 DataBannerPattern (com.massivecraft.massivecore.item.DataBannerPattern)1 MixinInventory (com.massivecraft.massivecore.mixin.MixinInventory)1 Coll (com.massivecraft.massivecore.store.Coll)1 Db (com.massivecraft.massivecore.store.Db)1 SenderColl (com.massivecraft.massivecore.store.SenderColl)1 JsonArray (com.massivecraft.massivecore.xlib.gson.JsonArray)1