public class JsonEntity extends Object implements Iterable<JsonEntity>
EasyGson is a wrapper for Gson, the excellent Google Java Json library. The added value of EasyGson is that it will allow you to handle the Json in a more "native" manner (native to Json, not to Java). This library pays off especially if you do not intend to transform to and from a Java domain model, but instead chose to operate directly on the Json model itself.
Every node (array, object or primitive) in a JSON tree is a JsonEntity. JsonEntity offers all the methods available to every node type, though it checks whether the call can be made. If not, an exception is thrown, with details on the branch of the JSON tree where the failure took place.Some JSON terminology:
The interface of JsonEntity is fluent, meaning that if you create an array or object, it will return the newly created instance. If you create a primitive, it will return the parent (the primitive itself is not interesting to return).
When creating objects/arrays, it is also possible to "create or get" it. This operation is called "ensure". It will create the object or array if it does not exist, or otherwise return the existing version.
Constructor and Description |
---|
JsonEntity(com.google.gson.JsonElement json)
Constructor that takes a Gson JsonElement and transforms it into a JsonEntity
|
JsonEntity(String jsonString)
Constructor that takes a JSON string representation and transforms it into a JsonEntity
|
Modifier and Type | Method and Description |
---|---|
int |
arraySize()
Returns the size of the array
|
BigDecimal |
asBigDecimal()
Returns the current primitive element as a BigDecimal
|
BigDecimal |
asBigDecimal(int property)
Convenience method for returning the primitive at the position in the array as a BigDecimal
|
BigDecimal |
asBigDecimal(String property)
Convenience method for returning the primitive with property name from the object as a BigDecimal
|
BigInteger |
asBigInteger()
Returns the current primitive element as a BigInteger
|
BigInteger |
asBigInteger(int property)
Convenience method for returning the primitive at the position in the array as a BigDecimal
|
BigInteger |
asBigInteger(String property)
Convenience method for returning the primitive with property name from the object as a BigInteger
|
boolean |
asBoolean()
Returns the current primitive element as a boolean
|
boolean |
asBoolean(int index)
Convenience method for returning the primitive at the position in the array as a boolean
|
boolean |
asBoolean(String property)
Convenience method for returning the primitive with property name from the object as a boolean
|
byte |
asByte()
Returns the current primitive element as a byte
|
byte |
asByte(int property)
Convenience method for returning the primitive at the position in the array as a byte
|
byte |
asByte(String property)
Convenience method for returning the primitive with property name from the object as a byte
|
char |
asCharacter()
Returns the current primitive element as a Character
|
char |
asCharacter(int index)
Convenience method for returning the primitive at the position in the array as a Character
|
char |
asCharacter(String property)
Convenience method for returning the primitive with property name from the object as a Character
|
double |
asDouble()
Returns the current primitive element as a double
|
double |
asDouble(int property)
Convenience method for returning the primitive at the position in the array as a double
|
double |
asDouble(String property)
Convenience method for returning the primitive with property name from the object as a double
|
float |
asFloat()
Returns the current primitive element as a float
|
float |
asFloat(int property)
Convenience method for returning the primitive at the position in the array as a float
|
float |
asFloat(String property)
Convenience method for returning the primitive with property name from the object as a float
|
int |
asInt()
Returns the current primitive element as an int
|
int |
asInt(int property)
Convenience method for returning the primitive at the position in the array as an int
|
int |
asInt(String property)
Convenience method for returning the primitive with property name from the object as an int
|
short |
asShort()
Returns the current primitive element as a short
|
short |
asShort(int property)
Convenience method for returning the primitive at the position in the array as a short
|
short |
asShort(String property)
Convenience method for returning the primitive with property name from the object as a short
|
String |
asString()
Returns the current primitive element as a String
|
String |
asString(int property)
Convenience method for returning the primitive at the position in the array as a String
|
String |
asString(String property)
Convenience method for returning the primitive with property name from the object as a String
|
JsonEntity |
create(Boolean value)
Appends a Boolean value to the array.
|
JsonEntity |
create(Character value)
Appends a Character value to the array.
|
JsonEntity |
create(int index,
Boolean value)
Inserts a Boolean value at the position in the array.
|
JsonEntity |
create(int index,
Character value)
Inserts a Character value at the position in the array.
|
JsonEntity |
create(int index,
com.google.gson.JsonElement jsonEntity)
Inserts a JsonElement at the designated position in the array.
|
JsonEntity |
create(int index,
JsonEntity jsonEntity)
Inserts a JsonEntity at the designated position in the array.
|
JsonEntity |
create(int index,
Number value)
Inserts a Number value at the position in the array.
|
JsonEntity |
create(int index,
String value)
Inserts a String value at the position in the array.
|
JsonEntity |
create(com.google.gson.JsonElement jsonEntity)
Appends a JsonEntity to the end of the array.
|
JsonEntity |
create(JsonEntity jsonEntity)
Appends a JsonEntity to the end of the array.
|
JsonEntity |
create(Number value)
Appends a Number value to the array.
|
JsonEntity |
create(String value)
Appends a String value to the array.
|
JsonEntity |
create(String property,
Boolean value)
Creates a Boolean value under property name in the object.
|
JsonEntity |
create(String property,
Character value)
Creates a Character value under property name in the object.
|
JsonEntity |
create(String property,
com.google.gson.JsonElement jsonEntity)
Creates a JsonElement under property name in the object.
|
JsonEntity |
create(String property,
JsonEntity jsonEntity)
Creates a JsonEntity under property name in the object.
|
JsonEntity |
create(String property,
Number value)
Creates a Number value under property name in the object.
|
JsonEntity |
create(String property,
String value)
Creates a String value under property name in the object.
|
JsonEntity |
createArray()
Creates an array within an array and adds it as the last element.
|
JsonEntity |
createArray(int index)
Creates an array within an array and adds it at the designated position.
|
JsonEntity |
createArray(String property)
Creates an array within an object.
|
JsonEntity |
createObject()
Creates an object within an array and adds it as the last element.
|
JsonEntity |
createObject(int index)
Creates an object within an array and adds it at the designated position.
|
JsonEntity |
createObject(String property)
Creates an object within an object.
|
JsonEntity |
detachedCopy()
Copies the current node without the reference to the parent and property.
|
static JsonEntity |
emptyArray()
Provides a starting point, in this case an empty array
|
static JsonEntity |
emptyObject()
Provides a starting point, in this case an empty object
|
JsonEntity |
ensureArray(int index)
Returns the array in an array with a designated position.
|
JsonEntity |
ensureArray(String property)
Returns the array in an object with the designated name.
|
JsonEntity |
ensureObject(int index)
Returns the object in an array with a designated position.
|
JsonEntity |
ensureObject(String property)
Returns the object in an object with the designated name.
|
JsonEntity |
get(int index)
Returns an element on the basis of an index.
|
JsonEntity |
get(String property)
Returns an element on the basis of a property name.
|
boolean |
isArray()
If the current element is an array, this method will return true
|
boolean |
isNull()
If the current element is empty (ie, 'null'), this method will return true
|
boolean |
isObject()
If the current element is an object, this method will return true
|
boolean |
isPrimitive()
If the current element is a primitive, this method will return true
|
Iterator<JsonEntity> |
iterator()
Returns the children in the current array as an iterator.
|
String |
name()
Returns the property name the current element has within the parent.
|
JsonEntity |
nullify(int index)
Sets the element at index in the array to a null value.
|
JsonEntity |
nullify(String property)
Sets the element with name property to a null value.
|
JsonEntity |
parent()
Returns the parent of the current element.
|
com.google.gson.JsonElement |
raw()
Returns the wrapped JsonElement of Gson.
|
JsonEntity |
remove(int index)
Removes the element at index from the array.
|
JsonEntity |
remove(String property)
Removes the element with name property from the object.
|
String |
toString()
Uses the JsonElement.toString() to show the JSON string representation
|
public JsonEntity(String jsonString)
jsonString
- JSON string representationpublic JsonEntity(com.google.gson.JsonElement json)
json
- Gson JsonElementpublic JsonEntity createArray(String property)
property
- name of the property to create the array underpublic JsonEntity createArray()
public JsonEntity createArray(int index)
index
- the position within the array where the array must be createdpublic JsonEntity ensureArray(String property)
property
- name of the property to get the array from (if it exists) or to create the array under (if
it does not exist)public JsonEntity ensureArray(int index)
index
- position of the property to get the array from (if it exists) or to create the array under
(if it does not exist)public JsonEntity createObject(String property)
property
- name of the property to create the object underpublic JsonEntity createObject()
public JsonEntity createObject(int index)
index
- the position within the array where the object must be createdpublic JsonEntity ensureObject(String property)
property
- name of the property to get the object from (if it exists) or to create the object under
(if it does not exist)public JsonEntity ensureObject(int index)
index
- position of the property to get the object from (if it exists) or to create the object under
(if it does not exist)public JsonEntity create(String property, JsonEntity jsonEntity)
property
- name of the propertyjsonEntity
- JsonEntity to create in the objectpublic JsonEntity create(JsonEntity jsonEntity)
jsonEntity
- JsonEntity to create in the arraypublic JsonEntity create(int index, JsonEntity jsonEntity)
index
- position in the array to store the JsonEntityjsonEntity
- JsonEntity to create in the arraypublic JsonEntity create(String property, com.google.gson.JsonElement jsonEntity)
property
- name of the propertyjsonEntity
- JsonElement to create in the objectpublic JsonEntity create(com.google.gson.JsonElement jsonEntity)
jsonEntity
- JsonElement to create in the arraypublic JsonEntity create(int index, com.google.gson.JsonElement jsonEntity)
index
- position in the array to store the JsonEntityjsonEntity
- JsonEntity to create in the arraypublic JsonEntity create(String property, String value)
property
- name of the propertyvalue
- value to storepublic JsonEntity create(String value)
value
- value to storepublic JsonEntity create(int index, String value)
value
- value to storepublic JsonEntity create(String property, Number value)
property
- name of the propertyvalue
- value to storepublic JsonEntity create(Number value)
value
- value to storepublic JsonEntity create(int index, Number value)
value
- value to storepublic JsonEntity create(String property, Boolean value)
property
- name of the propertyvalue
- value to storepublic JsonEntity create(Boolean value)
value
- value to storepublic JsonEntity create(int index, Boolean value)
value
- value to storepublic JsonEntity create(String property, Character value)
property
- name of the propertyvalue
- value to storepublic JsonEntity create(Character value)
value
- value to storepublic JsonEntity create(int index, Character value)
value
- value to storepublic JsonEntity nullify(String property)
property
- name of the element to nullify within the objectpublic JsonEntity nullify(int index)
index
- position of the element within the array to nullifypublic JsonEntity remove(String property)
property
- name of the element to remove from the objectpublic JsonEntity remove(int index)
index
- position of the element within the array to removepublic int arraySize()
public char asCharacter()
public char asCharacter(int index)
public char asCharacter(String property)
public boolean asBoolean()
public boolean asBoolean(int index)
public boolean asBoolean(String property)
public String asString()
public String asString(int property)
public String asString(String property)
public double asDouble()
public double asDouble(int property)
public double asDouble(String property)
public float asFloat()
public float asFloat(int property)
public float asFloat(String property)
public short asShort()
public short asShort(int property)
public short asShort(String property)
public int asInt()
public int asInt(int property)
public int asInt(String property)
public byte asByte()
public byte asByte(int property)
public byte asByte(String property)
public BigDecimal asBigDecimal()
public BigDecimal asBigDecimal(int property)
public BigDecimal asBigDecimal(String property)
public BigInteger asBigInteger()
public BigInteger asBigInteger(int property)
public BigInteger asBigInteger(String property)
public JsonEntity get(int index)
index
- index of the element within an arraypublic JsonEntity get(String property)
property
- property name of the element to returnpublic boolean isArray()
public boolean isPrimitive()
public boolean isObject()
public boolean isNull()
public JsonEntity parent()
public com.google.gson.JsonElement raw()
public String name()
public static JsonEntity emptyObject()
public static JsonEntity emptyArray()
public Iterator<JsonEntity> iterator()
iterator
in interface Iterable<JsonEntity>
public JsonEntity detachedCopy()
Copyright © 2013 42 BV. All Rights Reserved.