Class JsonUtil

java.lang.Object
io.sockit.gameserver.JsonUtil

public class JsonUtil
extends Object
This class contains convenience/utility methods for the java Json processing api.
  • Constructor Summary

    Constructors 
    Constructor Description
    JsonUtil()  
  • Method Summary

    Modifier and Type Method Description
    static JsonArrayBuilder createArrayBuilder()
    Creates a JsonArrayBuilder that handles nulls gracefully
    static JsonArrayBuilder createArrayBuilder​(Collection<?> collection)
    Creates a JsonArrayBuilder that handles nulls gracefully
    static JsonArrayBuilder createArrayBuilder​(JsonArray array)
    Creates a JsonArrayBuilder that handles nulls gracefully
    static JsonObjectBuilder createObjectBuilder()
    Creates a JsonObjectBuilder that handles nulls gracefully
    static JsonObjectBuilder createObjectBuilder​(JsonObject jsonObject)
    Creates a JsonObjectBuilder that handles nulls gracefully
    static boolean getAsBoolean​(JsonObject jsonObject, String property)
    Returns the value of the specified property in the jsonObject as a boolean or false if the property does not exist.
    static boolean getAsBoolean​(JsonObject jsonObject, String property, boolean defaultValue)
    Returns the value of the specified property in the jsonObject as a boolean or defaultValue if the property does not exist.
    static double getAsDouble​(JsonObject jsonObject, String property)
    Returns the value of the specified property in the jsonObject as a double or 0 if the property does not exist.
    static double getAsDouble​(JsonObject jsonObject, String property, double defaultValue)
    Returns the value of the specified property in the jsonObject as a double or defaultValue if the property does not exist.
    static float getAsFloat​(JsonObject jsonObject, String property)
    Returns the value of the specified property in the jsonObject as a float or 0 if the property does not exist.
    static float getAsFloat​(JsonObject jsonObject, String property, float defaultValue)
    Returns the value of the specified property in the jsonObject as a float or defaultValue if the property does not exist.
    static int getAsInt​(JsonObject jsonObject, String property)
    Returns the value of the specified property in the jsonObject as an int or 0 if the property does not exist.
    static int getAsInt​(JsonObject jsonObject, String property, int defaultValue)
    Returns the value of the specified property in the jsonObject as an int or defaultValue if the property does not exist.
    static JsonArray getAsJsonArray​(JsonObject jsonObject, String property)
    Returns the value of the specified property in the jsonObject as a jsonArray or null if the property does not exist.
    static JsonObject getAsJsonObject​(JsonObject jsonObject, String property)
    Returns the value of the specified property in the jsonObject as a jsonObject or null if the property does not exist.
    static long getAsLong​(JsonObject jsonObject, String property)
    Returns the value of the specified property in the jsonObject as a long or 0 if the property does not exist.
    static long getAsLong​(JsonObject jsonObject, String property, long defaultValue)
    Returns the value of the specified property in the jsonObject as a long or defaultValue if the property does not exist.
    static short getAsShort​(JsonObject jsonObject, String property)
    Returns the value of the specified property in the jsonObject as a short or 0 if the property does not exist.
    static short getAsShort​(JsonObject jsonObject, String property, short defaultValue)
    Returns the value of the specified property in the jsonObject as a short or defaultValue if the property does not exist.
    static String getAsString​(JsonObject jsonObject, String property)
    Returns the value of the specified property in the jsonObject as a String or null if the property does not exist.
    static String getAsString​(JsonObject jsonObject, String property, String defaultValue)
    Returns the value of the specified property in the jsonObject as a String or defaultValue if the property does not exist.
    static String getValueAsString​(JsonValue jsonValue)
    Returns the json value as a String
    static JsonArray readArray​(String jsonString)
    Parses the specified json String and creates a JsonArray
    static JsonObject readObject​(String jsonString)
    Parses the specified json String and creates a JsonObject
    static JsonArray toJsonArray​(Collection collection)
    Creates a JsonArray from the specified Collection

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • getValueAsString

      public static String getValueAsString​(JsonValue jsonValue)
      Returns the json value as a String
      Parameters:
      jsonValue - - the json value
      Returns:
      String - the json value as a String
    • getAsString

      public static String getAsString​(JsonObject jsonObject, String property)
      Returns the value of the specified property in the jsonObject as a String or null if the property does not exist.
      Parameters:
      jsonObject - - the jsonObject whose property value is to be returned
      property - - the property whose value is to be returned
      Returns:
      String - the value of the specified property in the jsonObject
    • getAsString

      public static String getAsString​(JsonObject jsonObject, String property, String defaultValue)
      Returns the value of the specified property in the jsonObject as a String or defaultValue if the property does not exist.
      Parameters:
      jsonObject - - the jsonObject whose property value is to be returned
      property - - the property whose value is to be returned
      defaultValue - - the value to be returned if the property does not exist
      Returns:
      String - the value of the specified property in the jsonObject
    • getAsInt

      public static int getAsInt​(JsonObject jsonObject, String property)
      Returns the value of the specified property in the jsonObject as an int or 0 if the property does not exist.
      Parameters:
      jsonObject - - the jsonObject whose property value is to be returned
      property - - the property whose value is to be returned
      Returns:
      int - the value of the specified property in the jsonObject as an int
    • getAsInt

      public static int getAsInt​(JsonObject jsonObject, String property, int defaultValue)
      Returns the value of the specified property in the jsonObject as an int or defaultValue if the property does not exist.
      Parameters:
      jsonObject - - the jsonObject whose property value is to be returned
      property - - the property whose value is to be returned
      defaultValue - - the value to be returned if the property does not exist
      Returns:
      int - the value of the specified property in the jsonObject as an int
    • getAsLong

      public static long getAsLong​(JsonObject jsonObject, String property)
      Returns the value of the specified property in the jsonObject as a long or 0 if the property does not exist.
      Parameters:
      jsonObject - - the jsonObject whose property value is to be returned
      property - - the property whose value is to be returned
      Returns:
      long - the value of the specified property in the jsonObject as a long
    • getAsLong

      public static long getAsLong​(JsonObject jsonObject, String property, long defaultValue)
      Returns the value of the specified property in the jsonObject as a long or defaultValue if the property does not exist.
      Parameters:
      jsonObject - - the jsonObject whose property value is to be returned
      property - - the property whose value is to be returned
      defaultValue - - the value to be returned if the property does not exist
      Returns:
      long - the value of the specified property in the jsonObject as a long
    • getAsFloat

      public static float getAsFloat​(JsonObject jsonObject, String property)
      Returns the value of the specified property in the jsonObject as a float or 0 if the property does not exist.
      Parameters:
      jsonObject - - the jsonObject whose property value is to be returned
      property - - the property whose value is to be returned
      Returns:
      float - the value of the specified property in the jsonObject as a float
    • getAsFloat

      public static float getAsFloat​(JsonObject jsonObject, String property, float defaultValue)
      Returns the value of the specified property in the jsonObject as a float or defaultValue if the property does not exist.
      Parameters:
      jsonObject - - the jsonObject whose property value is to be returned
      property - - the property whose value is to be returned
      defaultValue - - the value to be returned if the property does not exist
      Returns:
      float - the value of the specified property in the jsonObject as a float
    • getAsDouble

      public static double getAsDouble​(JsonObject jsonObject, String property)
      Returns the value of the specified property in the jsonObject as a double or 0 if the property does not exist.
      Parameters:
      jsonObject - - the jsonObject whose property value is to be returned
      property - - the property whose value is to be returned
      Returns:
      double - the value of the specified property in the jsonObject as a double
    • getAsDouble

      public static double getAsDouble​(JsonObject jsonObject, String property, double defaultValue)
      Returns the value of the specified property in the jsonObject as a double or defaultValue if the property does not exist.
      Parameters:
      jsonObject - - the jsonObject whose property value is to be returned
      property - - the property whose value is to be returned
      defaultValue - - the value to be returned if the property does not exist
      Returns:
      double - the value of the specified property in the jsonObject as a double
    • getAsBoolean

      public static boolean getAsBoolean​(JsonObject jsonObject, String property)
      Returns the value of the specified property in the jsonObject as a boolean or false if the property does not exist.
      Parameters:
      jsonObject - - the jsonObject whose property value is to be returned
      property - - the property whose value is to be returned
      Returns:
      boolean - the value of the specified property in the jsonObject as a boolean
    • getAsBoolean

      public static boolean getAsBoolean​(JsonObject jsonObject, String property, boolean defaultValue)
      Returns the value of the specified property in the jsonObject as a boolean or defaultValue if the property does not exist.
      Parameters:
      jsonObject - - the jsonObject whose property value is to be returned
      property - - the property whose value is to be returned
      defaultValue - - the value to be returned if the property does not exist
      Returns:
      boolean - the value of the specified property in the jsonObject as a boolean
    • getAsShort

      public static short getAsShort​(JsonObject jsonObject, String property)
      Returns the value of the specified property in the jsonObject as a short or 0 if the property does not exist.
      Parameters:
      jsonObject - - the jsonObject whose property value is to be returned
      property - - the property whose value is to be returned
      Returns:
      short - the value of the specified property in the jsonObject as a short
    • getAsShort

      public static short getAsShort​(JsonObject jsonObject, String property, short defaultValue)
      Returns the value of the specified property in the jsonObject as a short or defaultValue if the property does not exist.
      Parameters:
      jsonObject - - the jsonObject whose property value is to be returned
      property - - the property whose value is to be returned
      defaultValue - - the value to be returned if the property does not exist
      Returns:
      short - the value of the specified property in the jsonObject as a short
    • getAsJsonObject

      public static JsonObject getAsJsonObject​(JsonObject jsonObject, String property)
      Returns the value of the specified property in the jsonObject as a jsonObject or null if the property does not exist.
      Parameters:
      jsonObject - - the jsonObject whose property value is to be returned
      property - - the property whose value is to be returned
      Returns:
      JsonObject - the value of the specified property in the jsonObject as a JsonObject
    • getAsJsonArray

      public static JsonArray getAsJsonArray​(JsonObject jsonObject, String property)
      Returns the value of the specified property in the jsonObject as a jsonArray or null if the property does not exist.
      Parameters:
      jsonObject - - the jsonObject whose property value is to be returned
      property - - the property whose value is to be returned
      Returns:
      JsonArray - the value of the specified property in the jsonObject as a JsonArray
    • toJsonArray

      public static JsonArray toJsonArray​(Collection collection)
      Creates a JsonArray from the specified Collection
      Parameters:
      collection - - the collection from which the JsonArray will be created
      Returns:
      JsonArray - a JsonArray based on the specified Collection
    • readObject

      public static JsonObject readObject​(String jsonString)
      Parses the specified json String and creates a JsonObject
      Parameters:
      jsonString - - the json string to be parsed
      Returns:
      JsonObject - the parsed json String as a JsonObject
    • readArray

      public static JsonArray readArray​(String jsonString)
      Parses the specified json String and creates a JsonArray
      Parameters:
      jsonString - - the json string to be parsed
      Returns:
      JsonArray - the parsed json String as a JsonArray
    • createObjectBuilder

      public static JsonObjectBuilder createObjectBuilder()
      Creates a JsonObjectBuilder that handles nulls gracefully
      Returns:
      JsonObjectBuilder - a JsonObjectBuilder that handles nulls gracefully
    • createObjectBuilder

      public static JsonObjectBuilder createObjectBuilder​(JsonObject jsonObject)
      Creates a JsonObjectBuilder that handles nulls gracefully
      Parameters:
      jsonObject - - the initial object in the builder
      Returns:
      JsonObjectBuilder - a JsonObjectBuilder that handles nulls gracefully
    • createArrayBuilder

      public static JsonArrayBuilder createArrayBuilder()
      Creates a JsonArrayBuilder that handles nulls gracefully
      Returns:
      JsonArrayBuilder - a JsonArrayBuilder that handles nulls gracefully
    • createArrayBuilder

      public static JsonArrayBuilder createArrayBuilder​(JsonArray array)
      Creates a JsonArrayBuilder that handles nulls gracefully
      Parameters:
      array - - the initial array in the builder
      Returns:
      JsonArrayBuilder - a JsonArrayBuilder that handles nulls gracefully
    • createArrayBuilder

      public static JsonArrayBuilder createArrayBuilder​(Collection<?> collection)
      Creates a JsonArrayBuilder that handles nulls gracefully
      Parameters:
      collection - - the initial data for the builder
      Returns:
      JsonArrayBuilder - a JsonArrayBuilder that handles nulls gracefully