Package jodd.json
Class JsonObject
- java.lang.Object
-
- jodd.json.JsonObject
-
- All Implemented Interfaces:
java.lang.Iterable<java.util.Map.Entry<java.lang.String,java.lang.Object>>
public class JsonObject extends java.lang.Object implements java.lang.Iterable<java.util.Map.Entry<java.lang.String,java.lang.Object>>Representation of JSON object.- See Also:
JsonArray
-
-
Constructor Summary
Constructors Constructor Description JsonObject()Create a new, empty instance.JsonObject(java.util.Map<java.lang.String,java.lang.Object> map)Create an instance from a Map.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonObjectclear()Removes all the entries in this JSON object.booleancontainsKey(java.lang.String key)Returnstrueif the JSON object contain the specified key.booleanequals(java.lang.Object o)java.util.Set<java.lang.String>fieldNames()Return the set of field names in the JSON objects.byte[]getBinary(java.lang.String key)Returns the binary value with the specified key.byte[]getBinary(java.lang.String key, byte[] def)LikegetBinary(String)but specifies a default value to return if there is no entry.java.lang.BooleangetBoolean(java.lang.String key)Returns the boolean value with the specified key.java.lang.BooleangetBoolean(java.lang.String key, java.lang.Boolean def)LikegetBoolean(String)but specifies a default value to return if there is no entry.java.lang.DoublegetDouble(java.lang.String key)Returns the double value with the specified key.java.lang.DoublegetDouble(java.lang.String key, java.lang.Double def)LikegetDouble(String)but specifies a default value to return if there is no entry.java.lang.FloatgetFloat(java.lang.String key)Returns the float value with the specified key.java.lang.FloatgetFloat(java.lang.String key, java.lang.Float def)LikegetFloat(String)but specifies a default value to return if there is no entry.java.lang.IntegergetInteger(java.lang.String key)returns the integer value with the specified key.java.lang.IntegergetInteger(java.lang.String key, java.lang.Integer def)LikegetInteger(String)but specifies a default value to return if there is no entry.JsonArraygetJsonArray(java.lang.String key)Returns theJsonArrayvalue with the specified keyJsonArraygetJsonArray(java.lang.String key, JsonArray def)LikegetJsonArray(String)but specifies a default value to return if there is no entry.JsonObjectgetJsonObject(java.lang.String key)Returns theJsonObjectvalue with the specified key.JsonObjectgetJsonObject(java.lang.String key, JsonObject def)LikegetJsonObject(String)but specifies a default value to return if there is no entry.java.lang.LonggetLong(java.lang.String key)Returns the long value with the specified key.java.lang.LonggetLong(java.lang.String key, java.lang.Long def)LikegetLong(String)but specifies a default value to return if there is no entry.java.lang.StringgetString(java.lang.String key)Returns the string value with the specified key.java.lang.StringgetString(java.lang.String key, java.lang.String def)LikegetString(String)but specifies a default value to return if there is no entry.<T> TgetValue(java.lang.String key)Returns the value with the specified key, as an object.<T> TgetValue(java.lang.String key, T def)LikegetValue(String)but specifies a default value to return if there is no entry.inthashCode()booleanisEmpty()Returnstrueif JSON object is empty.java.util.Iterator<java.util.Map.Entry<java.lang.String,java.lang.Object>>iterator()Returns an iterator of the entries in the JSON object.java.util.Map<java.lang.String,java.lang.Object>map()Returns the underlyingMapas is.JsonObjectmergeIn(JsonObject other)Merges in another JSON object.JsonObjectmergeIn(JsonObject other, int depth)Merges in another JSON object.JsonObjectmergeInDeep(JsonObject other)Merges in another JSON object.JsonObjectput(java.lang.String key, byte[] value)Puts abyte[]into the JSON object with the specified key.JsonObjectput(java.lang.String key, java.lang.Boolean value)Puts a boolean into the JSON object with the specified key.JsonObjectput(java.lang.String key, java.lang.CharSequence value)Puts anCharSequenceinto the JSON object with the specified key.JsonObjectput(java.lang.String key, java.lang.Double value)Puts a double into the JSON object with the specified key.JsonObjectput(java.lang.String key, java.lang.Enum value)Puts an Enum into the JSON object with the specified key.JsonObjectput(java.lang.String key, java.lang.Float value)Puts a float into the JSON object with the specified key.JsonObjectput(java.lang.String key, java.lang.Integer value)Puts an integer into the JSON object with the specified key.JsonObjectput(java.lang.String key, java.lang.Long value)Puts a long into the JSON object with the specified key.JsonObjectput(java.lang.String key, java.lang.Object value)Puts an object into the JSON object with the specified key.JsonObjectput(java.lang.String key, java.lang.String value)Puts a string into the JSON object with the specified key.JsonObjectput(java.lang.String key, JsonArray value)Puts aJsonArrayinto the JSON object with the specified key.JsonObjectput(java.lang.String key, JsonObject value)Puts another JSON object into the JSON object with the specified key.JsonObjectputNull(java.lang.String key)Puts anullvalue into the JSON object with the specified key.java.lang.Objectremove(java.lang.String key)Removes an entry from this object.intsize()Returns the number of entries in the JSON object.java.util.stream.Stream<java.util.Map.Entry<java.lang.String,java.lang.Object>>stream()Returns a stream of the entries in the JSON object.java.lang.StringtoString()Returns JSON serialized by defaultJsonSerializer.
-
-
-
Method Detail
-
getString
public java.lang.String getString(java.lang.String key)
Returns the string value with the specified key.
-
getInteger
public java.lang.Integer getInteger(java.lang.String key)
returns the integer value with the specified key.
-
getLong
public java.lang.Long getLong(java.lang.String key)
Returns the long value with the specified key.
-
getDouble
public java.lang.Double getDouble(java.lang.String key)
Returns the double value with the specified key.
-
getFloat
public java.lang.Float getFloat(java.lang.String key)
Returns the float value with the specified key.
-
getBoolean
public java.lang.Boolean getBoolean(java.lang.String key)
Returns the boolean value with the specified key.
-
getJsonObject
public JsonObject getJsonObject(java.lang.String key)
Returns theJsonObjectvalue with the specified key.
-
getJsonArray
public JsonArray getJsonArray(java.lang.String key)
Returns theJsonArrayvalue with the specified key
-
getBinary
public byte[] getBinary(java.lang.String key)
Returns the binary value with the specified key.JSON itself has no notion of a binary. This extension complies to the RFC-7493. THe byte array is Base64 encoded binary.
-
getValue
public <T> T getValue(java.lang.String key)
Returns the value with the specified key, as an object.
-
getString
public java.lang.String getString(java.lang.String key, java.lang.String def)LikegetString(String)but specifies a default value to return if there is no entry.
-
getInteger
public java.lang.Integer getInteger(java.lang.String key, java.lang.Integer def)LikegetInteger(String)but specifies a default value to return if there is no entry.
-
getLong
public java.lang.Long getLong(java.lang.String key, java.lang.Long def)LikegetLong(String)but specifies a default value to return if there is no entry.
-
getDouble
public java.lang.Double getDouble(java.lang.String key, java.lang.Double def)LikegetDouble(String)but specifies a default value to return if there is no entry.
-
getFloat
public java.lang.Float getFloat(java.lang.String key, java.lang.Float def)LikegetFloat(String)but specifies a default value to return if there is no entry.
-
getBoolean
public java.lang.Boolean getBoolean(java.lang.String key, java.lang.Boolean def)LikegetBoolean(String)but specifies a default value to return if there is no entry.
-
getJsonObject
public JsonObject getJsonObject(java.lang.String key, JsonObject def)
LikegetJsonObject(String)but specifies a default value to return if there is no entry.
-
getJsonArray
public JsonArray getJsonArray(java.lang.String key, JsonArray def)
LikegetJsonArray(String)but specifies a default value to return if there is no entry.
-
getBinary
public byte[] getBinary(java.lang.String key, byte[] def)LikegetBinary(String)but specifies a default value to return if there is no entry.
-
getValue
public <T> T getValue(java.lang.String key, T def)LikegetValue(String)but specifies a default value to return if there is no entry.
-
containsKey
public boolean containsKey(java.lang.String key)
Returnstrueif the JSON object contain the specified key.
-
fieldNames
public java.util.Set<java.lang.String> fieldNames()
Return the set of field names in the JSON objects.
-
put
public JsonObject put(java.lang.String key, java.lang.Enum value)
Puts an Enum into the JSON object with the specified key.JSON has no concept of encoding Enums, so the Enum will be converted to a String using the
java.lang.Enum#namemethod and the value put as a String.
-
put
public JsonObject put(java.lang.String key, java.lang.CharSequence value)
Puts anCharSequenceinto the JSON object with the specified key.
-
put
public JsonObject put(java.lang.String key, java.lang.String value)
Puts a string into the JSON object with the specified key.
-
put
public JsonObject put(java.lang.String key, java.lang.Integer value)
Puts an integer into the JSON object with the specified key.
-
put
public JsonObject put(java.lang.String key, java.lang.Long value)
Puts a long into the JSON object with the specified key.
-
put
public JsonObject put(java.lang.String key, java.lang.Double value)
Puts a double into the JSON object with the specified key.
-
put
public JsonObject put(java.lang.String key, java.lang.Float value)
Puts a float into the JSON object with the specified key.
-
put
public JsonObject put(java.lang.String key, java.lang.Boolean value)
Puts a boolean into the JSON object with the specified key.
-
putNull
public JsonObject putNull(java.lang.String key)
Puts anullvalue into the JSON object with the specified key.
-
put
public JsonObject put(java.lang.String key, JsonObject value)
Puts another JSON object into the JSON object with the specified key.
-
put
public JsonObject put(java.lang.String key, JsonArray value)
Puts aJsonArrayinto the JSON object with the specified key.
-
put
public JsonObject put(java.lang.String key, byte[] value)
Puts abyte[]into the JSON object with the specified key.Follows JSON extension RFC7493, where binary will first be Base64 encoded before being put as a String.
-
put
public JsonObject put(java.lang.String key, java.lang.Object value)
Puts an object into the JSON object with the specified key.
-
remove
public java.lang.Object remove(java.lang.String key)
Removes an entry from this object.
-
mergeIn
public JsonObject mergeIn(JsonObject other)
Merges in another JSON object.This is the equivalent of putting all the entries of the other JSON object into this object. This is not a deep merge, entries containing (sub) JSON objects will be replaced entirely.
-
mergeInDeep
public JsonObject mergeInDeep(JsonObject other)
Merges in another JSON object. A deep merge (recursive) matches (sub) JSON objects in the existing tree and replaces all matching entries. JsonArrays are treated like any other entry, i.e. replaced entirely.
-
mergeIn
public JsonObject mergeIn(JsonObject other, int depth)
Merges in another JSON object. The merge is deep (recursive) to the specified level. If depth is 0, no merge is performed, if depth is greater than the depth of one of the objects, a full deep merge is performed.
-
map
public java.util.Map<java.lang.String,java.lang.Object> map()
Returns the underlyingMapas is.
-
stream
public java.util.stream.Stream<java.util.Map.Entry<java.lang.String,java.lang.Object>> stream()
Returns a stream of the entries in the JSON object.
-
iterator
public java.util.Iterator<java.util.Map.Entry<java.lang.String,java.lang.Object>> iterator()
Returns an iterator of the entries in the JSON object.- Specified by:
iteratorin interfacejava.lang.Iterable<java.util.Map.Entry<java.lang.String,java.lang.Object>>
-
size
public int size()
Returns the number of entries in the JSON object.
-
clear
public JsonObject clear()
Removes all the entries in this JSON object.
-
isEmpty
public boolean isEmpty()
Returnstrueif JSON object is empty.
-
toString
public java.lang.String toString()
Returns JSON serialized by defaultJsonSerializer.- Overrides:
toStringin classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
-