Package jodd.json

Class JsonObject

java.lang.Object
jodd.json.JsonObject
All Implemented Interfaces:
Iterable<Map.Entry<String,Object>>

public class JsonObject extends Object implements Iterable<Map.Entry<String,Object>>
Representation of JSON object.
See Also:
  • Field Details

  • Constructor Details

    • JsonObject

      public JsonObject()
      Create a new, empty instance.
    • JsonObject

      public JsonObject(Map<String,Object> map)
      Create an instance from a Map. The Map is not copied.
  • Method Details

    • getString

      public String getString(String key)
      Returns the string value with the specified key.
    • getInteger

      public Integer getInteger(String key)
      returns the integer value with the specified key.
    • getLong

      public Long getLong(String key)
      Returns the long value with the specified key.
    • getDouble

      public Double getDouble(String key)
      Returns the double value with the specified key.
    • getFloat

      public Float getFloat(String key)
      Returns the float value with the specified key.
    • getBoolean

      public Boolean getBoolean(String key)
      Returns the boolean value with the specified key.
    • getJsonObject

      public JsonObject getJsonObject(String key)
      Returns the JsonObject value with the specified key.
    • getJsonArray

      public JsonArray getJsonArray(String key)
      Returns the JsonArray value with the specified key
    • getBinary

      public byte[] getBinary(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(String key)
      Returns the value with the specified key, as an object.
    • getString

      public String getString(String key, String def)
      Like getString(String) but specifies a default value to return if there is no entry.
    • getInteger

      public Integer getInteger(String key, Integer def)
      Like getInteger(String) but specifies a default value to return if there is no entry.
    • getLong

      public Long getLong(String key, Long def)
      Like getLong(String) but specifies a default value to return if there is no entry.
    • getDouble

      public Double getDouble(String key, Double def)
      Like getDouble(String) but specifies a default value to return if there is no entry.
    • getFloat

      public Float getFloat(String key, Float def)
      Like getFloat(String) but specifies a default value to return if there is no entry.
    • getBoolean

      public Boolean getBoolean(String key, Boolean def)
      Like getBoolean(String) but specifies a default value to return if there is no entry.
    • getJsonObject

      public JsonObject getJsonObject(String key, JsonObject def)
      Like getJsonObject(String) but specifies a default value to return if there is no entry.
    • getJsonArray

      public JsonArray getJsonArray(String key, JsonArray def)
      Like getJsonArray(String) but specifies a default value to return if there is no entry.
    • getBinary

      public byte[] getBinary(String key, byte[] def)
      Like getBinary(String) but specifies a default value to return if there is no entry.
    • getValue

      public <T> T getValue(String key, T def)
      Like getValue(String) but specifies a default value to return if there is no entry.
    • containsKey

      public boolean containsKey(String key)
      Returns true if the JSON object contain the specified key.
    • fieldNames

      public Set<String> fieldNames()
      Return the set of field names in the JSON objects.
    • put

      public JsonObject put(String key, 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#name method and the value put as a String.

    • put

      public JsonObject put(String key, CharSequence value)
      Puts an CharSequence into the JSON object with the specified key.
    • put

      public JsonObject put(String key, String value)
      Puts a string into the JSON object with the specified key.
    • put

      public JsonObject put(String key, Integer value)
      Puts an integer into the JSON object with the specified key.
    • put

      public JsonObject put(String key, Long value)
      Puts a long into the JSON object with the specified key.
    • put

      public JsonObject put(String key, Double value)
      Puts a double into the JSON object with the specified key.
    • put

      public JsonObject put(String key, Float value)
      Puts a float into the JSON object with the specified key.
    • put

      public JsonObject put(String key, Boolean value)
      Puts a boolean into the JSON object with the specified key.
    • putNull

      public JsonObject putNull(String key)
      Puts a null value into the JSON object with the specified key.
    • put

      public JsonObject put(String key, JsonObject value)
      Puts another JSON object into the JSON object with the specified key.
    • put

      public JsonObject put(String key, JsonArray value)
      Puts a JsonArray into the JSON object with the specified key.
    • put

      public JsonObject put(String key, byte[] value)
      Puts a byte[] 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(String key, Object value)
      Puts an object into the JSON object with the specified key.
    • resolveValue

      static Object resolveValue(Object value)
    • remove

      public Object remove(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 Map<String,Object> map()
      Returns the underlying Map as is.
    • stream

      public Stream<Map.Entry<String,Object>> stream()
      Returns a stream of the entries in the JSON object.
    • iterator

      public Iterator<Map.Entry<String,Object>> iterator()
      Returns an iterator of the entries in the JSON object.
      Specified by:
      iterator in interface Iterable<Map.Entry<String,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()
      Returns true if JSON object is empty.
    • toString

      public String toString()
      Returns JSON serialized by default JsonSerializer.
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • objectEquals

      static boolean objectEquals(Map<?,?> m1, Object o2)
    • elementEquals

      static boolean elementEquals(Object o1, Object o2)
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object