Package com.massivecraft.mcore.xlib.gson

Examples of com.massivecraft.mcore.xlib.gson.JsonObject


    popStack(); // array
  }

  @Override public void beginObject() throws IOException {
    expect(JsonToken.BEGIN_OBJECT);
    JsonObject object = (JsonObject) peekStack();
    stack.add(object.entrySet().iterator());
  }
View Full Code Here


  }

  private void put(JsonElement value) {
    if (pendingName != null) {
      if (!value.isJsonNull() || getSerializeNulls()) {
        JsonObject object = (JsonObject) peek();
        object.add(pendingName, value);
      }
      pendingName = null;
    } else if (stack.isEmpty()) {
      product = value;
    } else {
View Full Code Here

    }
    throw new IllegalStateException();
  }

  @Override public JsonWriter beginObject() throws IOException {
    JsonObject object = new JsonObject();
    put(object);
    stack.add(object);
    return this;
  }
View Full Code Here

  public static PS valueOf(final JsonElement jsonElement)
  {
    if (jsonElement == null) return null;
    if (jsonElement.isJsonNull()) return null;
   
    final JsonObject jsonObject = jsonElement.getAsJsonObject();
    final PSBuilder builder = new PSBuilder();
   
    if (jsonObject.has("world") && jsonObject.has("yaw"))
    {
      // Old Faction LazyLocation
      for (Entry<String, JsonElement> entry : jsonObject.entrySet())
      {
        final String key = entry.getKey();
        final JsonElement value = entry.getValue();
       
        switch(key)
        {
          case "world":
            builder.world(value.getAsString());
          break;
          case "x":
            builder.locationX(value.getAsDouble());
          break;
          case "y":
            builder.locationY(value.getAsDouble());
          break;
          case "z":
            builder.locationZ(value.getAsDouble());
          break;
          case "pitch":
            builder.pitch(value.getAsFloat());
          break;
          case "yaw":
            builder.yaw(value.getAsFloat());
          break;
        }
      }
    }
    else
    {
      // The Standard Format
      for (Entry<String, JsonElement> entry : jsonObject.entrySet())
      {
        final String key = entry.getKey();
        final JsonElement value = entry.getValue();
       
        switch(key)
View Full Code Here

    return key;
  }
 
  public static BasicDBObject gson2MongoObject(JsonElement inElement)
  {
    JsonObject in = inElement.getAsJsonObject();
    BasicDBObject out = new BasicDBObject();
    for (Entry<String, JsonElement> entry : in.entrySet())
    {
      String key = gson2MongoKey(entry.getKey());
      JsonElement val = entry.getValue();
      if (val.isJsonArray())
      {
View Full Code Here

  public static JsonObject mongo2GsonObject(DBObject inObject)
  {
    if (!(inObject instanceof BasicDBObject)) throw new IllegalArgumentException("Expected BasicDBObject as argument type!");
    BasicDBObject in = (BasicDBObject)inObject;
   
    JsonObject jsonObject = new JsonObject();
    for (Entry<String, Object> entry : in.entrySet())
    {
      String key = mongo2GsonKey(entry.getKey());
      Object val = entry.getValue();
      if (val instanceof BasicDBList)
      {
        jsonObject.add(key, mongo2GsonArray((BasicDBList)val));
      }
      else if (val instanceof BasicDBObject)
      {
        jsonObject.add(key, mongo2GsonObject((BasicDBObject)val));
      }
      else
      {
        jsonObject.add(key, mongo2GsonPrimitive(val));
      }
    }
    return jsonObject;
  }
View Full Code Here

   
    // twoObject must be JsonObject
    if (!(twoObject instanceof JsonObject)) return false;
   
    // Cast to JsonObject
    JsonObject two = (JsonObject)twoObject;
   
    // Size must be the same
    if (one.entrySet().size() != two.entrySet().size()) return false;
   
    // And each entry must exist and be the same
    for (Entry<String, JsonElement> entry : one.entrySet())
    {
      if (!equals(entry.getValue(), two.get(entry.getKey()))) return false;
    }
    return true;
  }
View Full Code Here

    return Factions.get().gsonWithoutPreprocessors.fromJson(json, typeOfT);
  }
 
  public void preprocess(JsonElement json)
  {
    JsonObject jsonObject = json.getAsJsonObject();
   
    // Renamed fields
    // 1.8.X --> 2.0.0
    rename(jsonObject, "tag", "name");
    rename(jsonObject, "invites", "invitedPlayerIds");
View Full Code Here

  // -------------------------------------------- //
 
  public static JsonElement toJson(Inventory src)
  {
    // The return value is this object:
    JsonObject jsonInventory = new JsonObject();
   
    // These variables are used in loops and repetitive logic.
    ItemStack itemStack = null;
    JsonElement jsonItemStack = null;
   
    // Every inventory has a content part.
    ItemStack[] itemStacks = src.getContents();
   
    if (src instanceof PlayerInventory)
    {
      // Add the size "player"
      jsonInventory.addProperty(SIZE, PLAYER);
     
      // Cast to PlayerInventory
      PlayerInventory psrc = (PlayerInventory)src;
     
      // helmet
      itemStack = psrc.getHelmet();
      if (itemStack != null)
      {
        jsonItemStack = MCore.gson.toJsonTree(itemStack, ItemStack.class);
        jsonInventory.add(HELMET, jsonItemStack);
      }
     
      // chestplate
      itemStack = psrc.getChestplate();
      if (itemStack != null)
      {
        jsonItemStack = MCore.gson.toJsonTree(itemStack, ItemStack.class);
        jsonInventory.add(CHESTPLATE, jsonItemStack);
      }
     
      // leggings
      itemStack = psrc.getLeggings();
      if (itemStack != null)
      {
        jsonItemStack = MCore.gson.toJsonTree(itemStack, ItemStack.class);
        jsonInventory.add(LEGGINGS, jsonItemStack);
      }
     
      // boots
      itemStack = psrc.getBoots();
      if (itemStack != null)
      {
        jsonItemStack = MCore.gson.toJsonTree(itemStack, ItemStack.class);
        jsonInventory.add(BOOTS, jsonItemStack);
      }
    }
    else
    {
      // Add the size *length*
      jsonInventory.addProperty(SIZE, itemStacks.length);
    }
   
    // Add the content at the end since we like to have it at the bottom of return json.
    for (int i = 0; i < itemStacks.length; i++)
    {
      itemStack = itemStacks[i];
      jsonItemStack = MCore.gson.toJsonTree(itemStack, ItemStack.class);
      if (jsonItemStack == null) continue;
      jsonInventory.add(String.valueOf(i), jsonItemStack);
    }
   
    return jsonInventory;
  }
View Full Code Here

 
  public static Inventory fromJson(JsonElement json)
  {
    // If must be an object!
    if ( ! json.isJsonObject()) return null;
    JsonObject jsonInventory = json.getAsJsonObject();
   
    // The return value
    Inventory ret = null;
   
    // These variables are used in loops and repetitive logic.
    ItemStack itemStack = null;
    JsonElement jsonItemStack = null;
   
    // There must be a size entry!
    if ( ! jsonInventory.has(SIZE)) return null;
   
    JsonPrimitive jsonSize = jsonInventory.get(SIZE).getAsJsonPrimitive();
    int size = 0;
   
    // What size/type is it?
    if (jsonSize.isString() && jsonSize.getAsString().equals(PLAYER))
    {
      // We use 36 here since it's the size of the player inventory (without armor)
      size = 36;
     
      // This is a PlayerInventory
      ret = new CraftInventoryPlayer(new MCorePlayerInventory());
      PlayerInventory pret = (PlayerInventory)ret;
     
      // helmet
      if (jsonInventory.has(HELMET))
      {
        jsonItemStack = jsonInventory.get(HELMET);
        itemStack = MCore.gson.fromJson(jsonItemStack, ItemStack.class);
        pret.setHelmet(itemStack);
      }
     
      // chestplate
      if (jsonInventory.has(CHESTPLATE))
      {
        jsonItemStack = jsonInventory.get(CHESTPLATE);
        itemStack = MCore.gson.fromJson(jsonItemStack, ItemStack.class);
        pret.setChestplate(itemStack);
      }
     
      // leggings
      if (jsonInventory.has(LEGGINGS))
      {
        jsonItemStack = jsonInventory.get(LEGGINGS);
        itemStack = MCore.gson.fromJson(jsonItemStack, ItemStack.class);
        pret.setLeggings(itemStack);
      }
     
      // boots
      if (jsonInventory.has(BOOTS))
      {
        jsonItemStack = jsonInventory.get(BOOTS);
        itemStack = MCore.gson.fromJson(jsonItemStack, ItemStack.class);
        pret.setBoots(itemStack);
      }
    }
    else
    {
      // A custom size were specified
      size = jsonSize.getAsInt();
     
      // This is a "Custom" Inventory (content only).
      ret = new CraftInventoryCustom(null, size);
    }
   
    // Now process content
    ItemStack[] itemStacks = new ItemStack[size];
    for (int i = 0; i < size; i++)
    {
      // Fetch the jsonItemStack or mark it as empty and continue
      String stackIdx = String.valueOf(i);
      jsonItemStack = jsonInventory.get(stackIdx);
      itemStack = MCore.gson.fromJson(jsonItemStack, ItemStack.class);
      itemStacks[i] = itemStack;
    }
    ret.setContents(itemStacks);
   
View Full Code Here

TOP

Related Classes of com.massivecraft.mcore.xlib.gson.JsonObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.