Package jodd.json

Class 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
      JsonObject clear()
      Removes all the entries in this JSON object.
      boolean containsKey​(java.lang.String key)
      Returns true if the JSON object contain the specified key.
      boolean equals​(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)
      Like getBinary(String) but specifies a default value to return if there is no entry.
      java.lang.Boolean getBoolean​(java.lang.String key)
      Returns the boolean value with the specified key.
      java.lang.Boolean getBoolean​(java.lang.String key, java.lang.Boolean def)
      Like getBoolean(String) but specifies a default value to return if there is no entry.
      java.lang.Double getDouble​(java.lang.String key)
      Returns the double value with the specified key.
      java.lang.Double getDouble​(java.lang.String key, java.lang.Double def)
      Like getDouble(String) but specifies a default value to return if there is no entry.
      java.lang.Float getFloat​(java.lang.String key)
      Returns the float value with the specified key.
      java.lang.Float getFloat​(java.lang.String key, java.lang.Float def)
      Like getFloat(String) but specifies a default value to return if there is no entry.
      java.lang.Integer getInteger​(java.lang.String key)
      returns the integer value with the specified key.
      java.lang.Integer getInteger​(java.lang.String key, java.lang.Integer def)
      Like getInteger(String) but specifies a default value to return if there is no entry.
      JsonArray getJsonArray​(java.lang.String key)
      Returns the JsonArray value with the specified key
      JsonArray getJsonArray​(java.lang.String key, JsonArray def)
      Like getJsonArray(String) but specifies a default value to return if there is no entry.
      JsonObject getJsonObject​(java.lang.String key)
      Returns the JsonObject value with the specified key.
      JsonObject getJsonObject​(java.lang.String key, JsonObject def)
      Like getJsonObject(String) but specifies a default value to return if there is no entry.
      java.lang.Long getLong​(java.lang.String key)
      Returns the long value with the specified key.
      java.lang.Long getLong​(java.lang.String key, java.lang.Long def)
      Like getLong(String) but specifies a default value to return if there is no entry.
      java.lang.String getString​(java.lang.String key)
      Returns the string value with the specified key.
      java.lang.String getString​(java.lang.String key, java.lang.String def)
      Like getString(String) but specifies a default value to return if there is no entry.
      <T> T getValue​(java.lang.String key)
      Returns the value with the specified key, as an object.
      <T> T getValue​(java.lang.String key, T def)
      Like getValue(String) but specifies a default value to return if there is no entry.
      int hashCode()  
      boolean isEmpty()
      Returns true if 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 underlying Map as is.
      JsonObject mergeIn​(JsonObject other)
      Merges in another JSON object.
      JsonObject mergeIn​(JsonObject other, int depth)
      Merges in another JSON object.
      JsonObject mergeInDeep​(JsonObject other)
      Merges in another JSON object.
      JsonObject put​(java.lang.String key, byte[] value)
      Puts a byte[] into the JSON object with the specified key.
      JsonObject put​(java.lang.String key, java.lang.Boolean value)
      Puts a boolean into the JSON object with the specified key.
      JsonObject put​(java.lang.String key, java.lang.CharSequence value)
      Puts an CharSequence into the JSON object with the specified key.
      JsonObject put​(java.lang.String key, java.lang.Double value)
      Puts a double into the JSON object with the specified key.
      JsonObject put​(java.lang.String key, java.lang.Enum value)
      Puts an Enum into the JSON object with the specified key.
      JsonObject put​(java.lang.String key, java.lang.Float value)
      Puts a float into the JSON object with the specified key.
      JsonObject put​(java.lang.String key, java.lang.Integer value)
      Puts an integer into the JSON object with the specified key.
      JsonObject put​(java.lang.String key, java.lang.Long value)
      Puts a long into the JSON object with the specified key.
      JsonObject put​(java.lang.String key, java.lang.Object value)
      Puts an object into the JSON object with the specified key.
      JsonObject put​(java.lang.String key, java.lang.String value)
      Puts a string into the JSON object with the specified key.
      JsonObject put​(java.lang.String key, JsonArray value)
      Puts a JsonArray into the JSON object with the specified key.
      JsonObject put​(java.lang.String key, JsonObject value)
      Puts another JSON object into the JSON object with the specified key.
      JsonObject putNull​(java.lang.String key)
      Puts a null value into the JSON object with the specified key.
      java.lang.Object remove​(java.lang.String key)
      Removes an entry from this object.
      int size()
      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.String toString()
      Returns JSON serialized by default JsonSerializer.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Constructor Detail

      • JsonObject

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

        public JsonObject​(java.util.Map<java.lang.String,​java.lang.Object> map)
        Create an instance from a Map. The Map is not copied.
    • 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 the JsonObject value with the specified key.
      • getJsonArray

        public JsonArray getJsonArray​(java.lang.String key)
        Returns the JsonArray value 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)
        Like getString(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)
        Like getInteger(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)
        Like getLong(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)
        Like getDouble(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)
        Like getFloat(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)
        Like getBoolean(String) but specifies a default value to return if there is no entry.
      • getBinary

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

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

        public boolean containsKey​(java.lang.String key)
        Returns true if 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#name method and the value put as a String.

      • put

        public JsonObject put​(java.lang.String key,
                              java.lang.CharSequence value)
        Puts an CharSequence into 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 a null value 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,
                              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​(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 underlying Map as 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:
        iterator in interface java.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()
        Returns true if JSON object is empty.
      • toString

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

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object