Package com.massivecraft.mcore.xlib.gson

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


  // These methods need to be synchronized since JDK DateFormat classes are not thread-safe
  // See issue 162
  public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
    synchronized (localFormat) {
      String dateFormatAsString = enUsFormat.format(src);
      return new JsonPrimitive(dateFormatAsString);
    }
  }
View Full Code Here


  }
 
  public static Object gson2MongoPrimitive(JsonElement inElement)
  {
    if (inElement.isJsonNull()) return null;
    JsonPrimitive in = inElement.getAsJsonPrimitive();
   
    if (in.isBoolean())
    {
      return in.getAsBoolean();
    }
   
    if (in.isNumber())
    {
      Number number = in.getAsNumber();
      boolean floating;
     
      if (number instanceof LazilyParsedNumber)
      {
        floating = StringUtils.contains(number.toString(), '.');
      }
      else
      {
        floating = (number instanceof Double || number instanceof Float);
      }
     
      if (floating)
      {
        return number.doubleValue();
      }
      else
      {
        return number.longValue();
      }
     
    }
   
    if (in.isString())
    {
      return in.getAsString();
    }
   
    throw new IllegalArgumentException("Unsupported value type for: " + in);
  }
View Full Code Here

  }

  public static JsonElement mongo2GsonPrimitive(Object inObject)
  {
    if (inObject == null) return JsonNull.INSTANCE;
    if (inObject instanceof Boolean) return new JsonPrimitive((Boolean) inObject);
    if (inObject instanceof Number) return new JsonPrimitive((Number) inObject);
    if (inObject instanceof String) return new JsonPrimitive((String) inObject);
    if (inObject instanceof Character) return new JsonPrimitive((Character) inObject);
    if (inObject instanceof ObjectId) return new JsonPrimitive(inObject.toString());
    throw new IllegalArgumentException("Unsupported value type for: " + inObject);
  }
View Full Code Here

   
    // if twoObject is JsonObject or JsonArray we are not equal.
    if (!(twoObject instanceof JsonPrimitive)) return false;
   
    // Cast to JsonPrimitive
    JsonPrimitive two = (JsonPrimitive)twoObject;
   
    // Boolean check
    if (one.isBoolean())
    {
      return one.getAsBoolean() == two.getAsBoolean();
    }
   
    // Number check
    if (one.isNumber())
    {
      Number oneNumber = one.getAsNumber();
      Number twoNumber = two.getAsNumber();
     
      boolean floating;
      if (oneNumber instanceof LazilyParsedNumber)
      {
        floating = StringUtils.contains(oneNumber.toString(), '.');
      }
      else
      {
        floating = (oneNumber instanceof Double || oneNumber instanceof Float);
      }
     
      if (floating)
      {
        // Our epsilon is pretty big in order to see float and double as the same.
        return Math.abs(oneNumber.doubleValue() - twoNumber.doubleValue()) < 0.0001D;
      }
      else
      {
        return oneNumber.longValue() == twoNumber.longValue();
      }
     
    }
   
    // String check
    if (one.isString())
    {
      return one.getAsString().equals(two.getAsString());
    }
   
    throw new IllegalArgumentException("Unsupported value type for: " + one);
  }
View Full Code Here

    return objectId.toString();
  }
 
  public static JsonPrimitive convertUUIDToJsonPrimitive(UUID objectId)
  {
    return new JsonPrimitive(convertUUIDToString(objectId));
  }
View Full Code Here

    return objectId.toString();
  }
 
  public static JsonPrimitive convertObjectIdToJsonPrimitive(ObjectId objectId)
  {
    return new JsonPrimitive(convertObjectIdToString(objectId));
  }
View Full Code Here

    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);
    }
   
View Full Code Here

    if (src == null) return null;

    // isDefault <=> simple hostFactionId string
    if (src.isDefault())
    {
      return new JsonPrimitive(src.getHostFactionId());
    }

    // Otherwise object
    JsonObject obj = new JsonObject();
   
View Full Code Here

  public static JsonArray convertStringList(Collection<String> strings)
  {
    JsonArray ret = new JsonArray();
    for (String string : strings)
    {
      ret.add(new JsonPrimitive(string));
    }
    return ret;
  }
View Full Code Here

  public static JsonArray fromColorCollection(Collection<Color> colors)
  {
    JsonArray ret = new JsonArray();
    for (Color color : colors)
    {
      ret.add(new JsonPrimitive(color.asRGB()));
    }
    return ret;
  }
View Full Code Here

TOP

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

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.