Package com.gengoai.json
Class JsonEntry
- java.lang.Object
-
- com.gengoai.json.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 Summary
Constructors Constructor Description JsonEntry(@NonNull com.fasterxml.jackson.databind.JsonNode element)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonEntry
addProperty(@NonNull String name, Object value)
Adds a property to the entry checking that it is a json objectJsonEntry
addValue(Object value)
Adds a value to the entry checking that it is a json arraystatic JsonEntry
array(Iterable<?> items)
Array json entry.static JsonEntry
array(Object... items)
Creates a new array<T> T
as(@NonNull Class<T> type)
Gets the value of this entry as the given class.<T> T
as(@NonNull Type type)
Gets the value of this entry as the given class.List<JsonEntry>
asArray()
Converts the entry into a list of elements checking if the underlying entry is a json array.<T> List<T>
asArray(@NonNull Class<? extends T> clazz)
Converts the entry into a list of elements checking if the underlying entry is a json array.<T,E extends Collection<T>>
EasArray(@NonNull Class<? extends T> clazz, E collection)
<T> List<T>
asArray(@NonNull Type clazz)
Converts the entry into a list of elements checking if the underlying entry is a json array.boolean
asBoolean()
Gets this entry as a boolean value.boolean[]
asBooleanArray()
byte
asByte()
Gets this entry as a byte value.Character
asCharacter()
Gets this entry as a Character value.double
asDouble()
Gets this entry as a double value.double[]
asDoubleArray()
float
asFloat()
Gets this entry as a float value.int
asInt()
Gets this entry as a int value.int[]
asIntArray()
long
asLong()
Gets this entry as a long value.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.<T> Map<String,T>
asMap(@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.Number
asNumber()
Gets this entry as a Number value.short
asShort()
Gets this entry as a short value.String
asString()
Gets this entry as a String value.Val
asVal()
Gets this entry as a Val value.Iterator<JsonEntry>
elementIterator()
Gets an iterator over the elements in this element checking if the underlying entry is a json array.Stream<JsonEntry>
elementStream()
boolean
equals(Object obj)
void
forEachElement(Consumer<JsonEntry> consumer)
Performs the given action for entry in this array.void
forEachProperty(BiConsumer<String,JsonEntry> consumer)
Performs the given action for property name and value in this object.static JsonEntry
from(Object v)
Creates an entry from the given object.Object
get()
boolean
getBooleanProperty(String propertyName)
Gets the value of the given property name as a booleanboolean
getBooleanProperty(String propertyName, boolean defaultValue)
Gets the value of the given property name as a booleanCharacter
getCharacterProperty(String propertyName)
Gets the value of the given property name as a CharacterCharacter
getCharacterProperty(String propertyName, Character defaultValue)
Gets the value of the given property name as a Characterdouble
getDoubleProperty(String propertyName)
Gets the value of the given property name as a doubledouble
getDoubleProperty(String propertyName, double defaultValue)
Gets the value of the given property name as a doublecom.fasterxml.jackson.databind.JsonNode
getElement()
Gets the underlying JsonElement.float
getFloatProperty(String propertyName)
Gets the value of the given property name as a floatfloat
getFloatProperty(String propertyName, float defaultValue)
Gets the value of the given property name as a floatint
getIntProperty(String propertyName)
Gets the value of the given property name as a intint
getIntProperty(String propertyName, int defaultValue)
Gets the value of the given property name as a intlong
getLongProperty(String propertyName)
Gets the value of the given property name as a longlong
getLongProperty(String propertyName, long defaultValue)
Gets the value of the given property name as a longNumber
getNumberProperty(String propertyName)
Gets the value of the given property name as a NumberNumber
getNumberProperty(String propertyName, Number defaultValue)
Gets the value of the given property name as a NumberOptional<JsonEntry>
getOptionalProperty(String propertyName)
<T> T
getProperty(@NonNull String propertyName, @NonNull Class<T> clazz)
Gets the value of the given property name as the type of the given classJsonEntry
getProperty(String propertyName)
Gets the value of the given property name as a JsonEntry<T> T
getProperty(String propertyName, Class<T> clazz, T defaultValue)
Gets the value of the given property name as the type of the given classJsonEntry
getProperty(String propertyName, Object defaultValue)
Gets the value of the given property name as a JsonEntryString
getStringProperty(String propertyName)
Gets the value of the given property name as a StringString
getStringProperty(String propertyName, String defaultValue)
Gets the value of the given property name as a StringVal
getValProperty(String propertyName)
Gets the value of the given property name as a ValVal
getValProperty(String propertyName, Object defaultValue)
Gets the value of the given property name as a Valint
hashCode()
boolean
hasProperty(String propertyName)
Checks if this entry has the given propertyboolean
isArray()
Checks if this entry is a json arrayboolean
isBoolean()
Is boolean boolean.boolean
isNull()
Checks if this entry is a json null valueboolean
isNumber()
Is number boolean.boolean
isObject()
Checks if this entry is a json objectboolean
isPrimitive()
boolean
isString()
Is string boolean.Set<String>
keySet()
Gets the keys (property names).JsonEntry
mergeObject(JsonEntry entry)
static JsonEntry
nullValue()
Creates a null valued entrystatic JsonEntry
object()
Creates a new empty objectIterator<Map.Entry<String,JsonEntry>>
propertyIterator()
Gets an iterator over the elements in this element.int
size()
String
toString()
-
-
-
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 namevalue
- 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
-
get
public Object get()
-
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 namedefaultValue
- 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 namedefaultValue
- 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 namedefaultValue
- 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 namedefaultValue
- 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 namedefaultValue
- 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 namedefaultValue
- 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 namedefaultValue
- 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 namedefaultValue
- 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 nameclazz
- 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 nameclazz
- Class information for the desired typedefaultValue
- 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 namedefaultValue
- 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 namedefaultValue
- 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
-
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()
-
-