Class JsonEntry

  • All Implemented Interfaces:
    Serializable

    public class JsonEntry
    extends Object
    implements Serializable

    A convenience wrapper around JsonElement allowing a single interface for objects, arrays, and primitives

    See Also:
    Serialized Form
    • Constructor Detail

      • JsonEntry

        public JsonEntry​(@NonNull
                         @NonNull com.fasterxml.jackson.databind.JsonNode element)
    • Method Detail

      • array

        public static JsonEntry array​(Iterable<?> items)
        Array json entry.
        Parameters:
        items - the items
        Returns:
        the json entry
      • array

        public static JsonEntry array​(Object... items)
        Creates a new array
        Parameters:
        items - Items to add to the array
        Returns:
        the json entry
      • from

        public static JsonEntry from​(Object v)
        Creates an entry from the given object.
        Parameters:
        v - the value to create the entry from.
        Returns:
        the json entry
      • nullValue

        public static JsonEntry nullValue()
        Creates a null valued entry
        Returns:
        the null valued entry
      • object

        public static JsonEntry object()
        Creates a new empty object
        Returns:
        the json entry
      • addProperty

        public JsonEntry addProperty​(@NonNull
                                     @NonNull String name,
                                     Object value)
        Adds a property to the entry checking that it is a json object
        Parameters:
        name - the property name
        value - the property value
        Returns:
        this json entry
        Throws:
        IllegalStateException - if the entry's element is not a json object
      • addValue

        public JsonEntry addValue​(Object value)
        Adds a value to the entry checking that it is a json array
        Parameters:
        value - the value
        Returns:
        this json entry
        Throws:
        IllegalStateException - if the entry's element is not a json array
      • as

        public <T> T as​(@NonNull
                        @NonNull Class<T> type)
        Gets the value of this entry as the given class.
        Type Parameters:
        T - the type parameter
        Parameters:
        type - the type information for the type to be generated
        Returns:
        the value
      • as

        public <T> T as​(@NonNull
                        @NonNull Type type)
        Gets the value of this entry as the given class.
        Type Parameters:
        T - the type parameter
        Parameters:
        type - the type information for the type to be generated
        Returns:
        the value
      • asArray

        public List<JsonEntry> asArray()
        Converts the entry into a list of elements checking if the underlying entry is a json array.
        Returns:
        the list
        Throws:
        IllegalStateException - if the entry's element is not a json array
      • asArray

        public <T> List<T> asArray​(@NonNull
                                   @NonNull Class<? extends T> clazz)
        Converts the entry into a list of elements checking if the underlying entry is a json array.
        Type Parameters:
        T - the type parameter
        Parameters:
        clazz - the clazz
        Returns:
        the list
        Throws:
        IllegalStateException - if the entry's element is not a json array
      • asArray

        public <T> List<T> asArray​(@NonNull
                                   @NonNull Type clazz)
        Converts the entry into a list of elements checking if the underlying entry is a json array.
        Type Parameters:
        T - the type parameter
        Parameters:
        clazz - the clazz
        Returns:
        the list
        Throws:
        IllegalStateException - if the entry's element is not a json array
      • asArray

        public <T,​E extends Collection<T>> E asArray​(@NonNull
                                                           @NonNull Class<? extends T> clazz,
                                                           @NonNull
                                                           E collection)
      • asBoolean

        public boolean asBoolean()
        Gets this entry as a boolean value.
        Returns:
        the as boolean value
        Throws:
        IllegalStateException - if the entry's element is not a json primitive
      • asBooleanArray

        public boolean[] asBooleanArray()
      • asByte

        public byte asByte()
        Gets this entry as a byte value.
        Returns:
        the as byte value
        Throws:
        IllegalStateException - if the entry's element is not a json primitive
      • asCharacter

        public Character asCharacter()
        Gets this entry as a Character value.
        Returns:
        the as Character value
        Throws:
        IllegalStateException - if the entry's element is not a json primitive
      • asDouble

        public double asDouble()
        Gets this entry as a double value.
        Returns:
        the as double value
        Throws:
        IllegalStateException - if the entry's element is not a json primitive
      • asDoubleArray

        public double[] asDoubleArray()
      • asFloat

        public float asFloat()
        Gets this entry as a float value.
        Returns:
        the as float value
        Throws:
        IllegalStateException - if the entry's element is not a json primitive
      • asInt

        public int asInt()
        Gets this entry as a int value.
        Returns:
        the as int value
        Throws:
        IllegalStateException - if the entry's element is not a json primitive
      • asIntArray

        public int[] asIntArray()
      • asLong

        public long asLong()
        Gets this entry as a long value.
        Returns:
        the as long value
        Throws:
        IllegalStateException - if the entry's element is not a json primitive
      • asMap

        public Map<String,​JsonEntry> asMap()
        Converts the entry into a map of string keys and entry elements checking if the underlying entry is a json object.
        Returns:
        the map
        Throws:
        IllegalStateException - if the entry's element is not a json object
      • asMap

        public <T> Map<String,​T> asMap​(@NonNull
                                             @NonNull Class<? extends T> clazz)
        Converts the entry into a map of string keys and entry elements checking if the underlying entry is a json object.
        Type Parameters:
        T - the type parameter
        Parameters:
        clazz - the clazz
        Returns:
        the map
        Throws:
        IllegalStateException - if the entry's element is not a json object
      • asNumber

        public Number asNumber()
        Gets this entry as a Number value.
        Returns:
        the as Number value
        Throws:
        IllegalStateException - if the entry's element is not a json primitive
      • asShort

        public short asShort()
        Gets this entry as a short value.
        Returns:
        the as short value
        Throws:
        IllegalStateException - if the entry's element is not a json primitive
      • asString

        public String asString()
        Gets this entry as a String value.
        Returns:
        the as String value
        Throws:
        IllegalStateException - if the entry's element is not a json primitive
      • asVal

        public Val asVal()
        Gets this entry as a Val value.
        Returns:
        the as Val value
      • elementIterator

        public Iterator<JsonEntry> elementIterator()
        Gets an iterator over the elements in this element checking if the underlying entry is a json array.
        Returns:
        the iterator
        Throws:
        IllegalStateException - if the entry's element is not a json array
      • forEachElement

        public void forEachElement​(Consumer<JsonEntry> consumer)
        Performs the given action for entry in this array.
        Parameters:
        consumer - the action to perform
        Throws:
        IllegalStateException - if the entry's element is not a json array
      • forEachProperty

        public void forEachProperty​(BiConsumer<String,​JsonEntry> consumer)
        Performs the given action for property name and value in this object.
        Parameters:
        consumer - the action to perform
        Throws:
        IllegalStateException - if the entry's element is not a json object
      • getBooleanProperty

        public boolean getBooleanProperty​(String propertyName)
        Gets the value of the given property name as a boolean
        Parameters:
        propertyName - the property name
        Returns:
        the boolean property
        Throws:
        IllegalStateException - if the entry is not a json object
      • getBooleanProperty

        public boolean getBooleanProperty​(String propertyName,
                                          boolean defaultValue)
        Gets the value of the given property name as a boolean
        Parameters:
        propertyName - the property name
        defaultValue - the default value if the property does not exist
        Returns:
        the boolean property
        Throws:
        IllegalStateException - if the entry is not a json object
      • getCharacterProperty

        public Character getCharacterProperty​(String propertyName)
        Gets the value of the given property name as a Character
        Parameters:
        propertyName - the property name
        Returns:
        the Character property
        Throws:
        IllegalStateException - if the entry is not a json object
      • getCharacterProperty

        public Character getCharacterProperty​(String propertyName,
                                              Character defaultValue)
        Gets the value of the given property name as a Character
        Parameters:
        propertyName - the property name
        defaultValue - the default value if the property does not exist
        Returns:
        the Character property
        Throws:
        IllegalStateException - if the entry is not a json object
      • getDoubleProperty

        public double getDoubleProperty​(String propertyName)
        Gets the value of the given property name as a double
        Parameters:
        propertyName - the property name
        Returns:
        the double property
        Throws:
        IllegalStateException - if the entry is not a json object
      • getDoubleProperty

        public double getDoubleProperty​(String propertyName,
                                        double defaultValue)
        Gets the value of the given property name as a double
        Parameters:
        propertyName - the property name
        defaultValue - the default value if the property does not exist
        Returns:
        the double property
        Throws:
        IllegalStateException - if the entry is not a json object
      • getElement

        public com.fasterxml.jackson.databind.JsonNode getElement()
        Gets the underlying JsonElement.
        Returns:
        the element
      • getFloatProperty

        public float getFloatProperty​(String propertyName)
        Gets the value of the given property name as a float
        Parameters:
        propertyName - the property name
        Returns:
        the float property
        Throws:
        IllegalStateException - if the entry is not a json object
      • getFloatProperty

        public float getFloatProperty​(String propertyName,
                                      float defaultValue)
        Gets the value of the given property name as a float
        Parameters:
        propertyName - the property name
        defaultValue - the default value if the property does not exist
        Returns:
        the float property
        Throws:
        IllegalStateException - if the entry is not a json object
      • getIntProperty

        public int getIntProperty​(String propertyName)
        Gets the value of the given property name as a int
        Parameters:
        propertyName - the property name
        Returns:
        the int property
        Throws:
        IllegalStateException - if the entry is not a json object
      • getIntProperty

        public int getIntProperty​(String propertyName,
                                  int defaultValue)
        Gets the value of the given property name as a int
        Parameters:
        propertyName - the property name
        defaultValue - the default value if the property does not exist
        Returns:
        the int property
        Throws:
        IllegalStateException - if the entry is not a json object
      • getLongProperty

        public long getLongProperty​(String propertyName)
        Gets the value of the given property name as a long
        Parameters:
        propertyName - the property name
        Returns:
        the long property
        Throws:
        IllegalStateException - if the entry is not a json object
      • getLongProperty

        public long getLongProperty​(String propertyName,
                                    long defaultValue)
        Gets the value of the given property name as a long
        Parameters:
        propertyName - the property name
        defaultValue - the default value if the property does not exist
        Returns:
        the long property
        Throws:
        IllegalStateException - if the entry is not a json object
      • getNumberProperty

        public Number getNumberProperty​(String propertyName)
        Gets the value of the given property name as a Number
        Parameters:
        propertyName - the property name
        Returns:
        the Number property
        Throws:
        IllegalStateException - if the entry is not a json object
      • getNumberProperty

        public Number getNumberProperty​(String propertyName,
                                        Number defaultValue)
        Gets the value of the given property name as a Number
        Parameters:
        propertyName - the property name
        defaultValue - the default value if the property does not exist
        Returns:
        the Number property
        Throws:
        IllegalStateException - if the entry is not a json object
      • getProperty

        public JsonEntry getProperty​(String propertyName)
        Gets the value of the given property name as a JsonEntry
        Parameters:
        propertyName - the property name
        Returns:
        the JsonEntry property
        Throws:
        IllegalStateException - if the entry is not a json object
      • getProperty

        public JsonEntry getProperty​(String propertyName,
                                     Object defaultValue)
        Gets the value of the given property name as a JsonEntry
        Parameters:
        propertyName - the property name
        defaultValue - the default value if the property does not exist
        Returns:
        the JsonEntry property
        Throws:
        IllegalStateException - if the entry is not a json object
      • getProperty

        public <T> T getProperty​(@NonNull
                                 @NonNull String propertyName,
                                 @NonNull
                                 @NonNull Class<T> clazz)
        Gets the value of the given property name as the type of the given class
        Type Parameters:
        T - the type parameter
        Parameters:
        propertyName - the property name
        clazz - Class information for the desired type
        Returns:
        the property value
        Throws:
        IllegalStateException - if the entry is not a json object
      • getProperty

        public <T> T getProperty​(String propertyName,
                                 Class<T> clazz,
                                 T defaultValue)
        Gets the value of the given property name as the type of the given class
        Type Parameters:
        T - the type parameter
        Parameters:
        propertyName - the property name
        clazz - Class information for the desired type
        defaultValue - the default value if the property does not exist
        Returns:
        the property value
        Throws:
        IllegalStateException - if the entry is not a json object
      • getStringProperty

        public String getStringProperty​(String propertyName)
        Gets the value of the given property name as a String
        Parameters:
        propertyName - the property name
        Returns:
        the String property
        Throws:
        IllegalStateException - if the entry is not a json object
      • getStringProperty

        public String getStringProperty​(String propertyName,
                                        String defaultValue)
        Gets the value of the given property name as a String
        Parameters:
        propertyName - the property name
        defaultValue - the default value if the property does not exist
        Returns:
        the String property
        Throws:
        IllegalStateException - if the entry is not a json object
      • getValProperty

        public Val getValProperty​(String propertyName)
        Gets the value of the given property name as a Val
        Parameters:
        propertyName - the property name
        Returns:
        the Val property
        Throws:
        IllegalStateException - if the entry is not a json object
      • getValProperty

        public Val getValProperty​(String propertyName,
                                  Object defaultValue)
        Gets the value of the given property name as a Val
        Parameters:
        propertyName - the property name
        defaultValue - the default value if the property does not exist
        Returns:
        the Val property
        Throws:
        IllegalStateException - if the entry is not a json object
      • hasProperty

        public boolean hasProperty​(String propertyName)
        Checks if this entry has the given property
        Parameters:
        propertyName - the property name to check
        Returns:
        true if this is an object and has the property otherwise false
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • isArray

        public boolean isArray()
        Checks if this entry is a json array
        Returns:
        true if a json array, false otherwise
      • isBoolean

        public boolean isBoolean()
        Is boolean boolean.
        Returns:
        the boolean
      • isNull

        public boolean isNull()
        Checks if this entry is a json null value
        Returns:
        true if a json null value, false otherwise
      • isNumber

        public boolean isNumber()
        Is number boolean.
        Returns:
        the boolean
      • isObject

        public boolean isObject()
        Checks if this entry is a json object
        Returns:
        true if a json object, false otherwise
      • isPrimitive

        public boolean isPrimitive()
      • isString

        public boolean isString()
        Is string boolean.
        Returns:
        the boolean
      • keySet

        public Set<String> keySet()
        Gets the keys (property names).
        Returns:
        the set of property names (keys) or empty set if not an object
      • propertyIterator

        public Iterator<Map.Entry<String,​JsonEntry>> propertyIterator()
        Gets an iterator over the elements in this element.
        Returns:
        the iterator of properties if an object, empty iterator otherwise
      • size

        public int size()