Class JsonObject

java.lang.Object
blue.endless.jankson.JsonElement
blue.endless.jankson.JsonObject
All Implemented Interfaces:
Cloneable, Map<String,JsonElement>

public class JsonObject extends JsonElement implements Map<String,JsonElement>
  • Field Details

  • Constructor Details

    • JsonObject

      public JsonObject()
  • Method Details

    • getObject

      @Nullable public JsonObject getObject(@Nonnull String name)
      If there is an entry at this key, and that entry is a json object, return it. Otherwise returns null.
    • put

      public JsonElement put(@Nonnull String key, @Nonnull JsonElement elem, @Nullable String comment)
      Replaces a key-value mapping in this object if it exists, or adds the mapping to the end of the object if it doesn't. Returns the old value mapped to this key if there was one.
    • putDefault

      @Nonnull public JsonElement putDefault(@Nonnull String key, @Nonnull JsonElement elem, @Nullable String comment)
    • putDefault

      @Nullable public <T> T putDefault(@Nonnull String key, @Nonnull T elem, @Nullable String comment)
      May return null if the existing object can't be marshalled to elem's class
    • putDefault

      @Nullable public <T> T putDefault(@Nonnull String key, @Nonnull T elem, Class<? extends T> clazz, @Nullable String comment)
      May return null if the existing object can't be marshalled to the target class
    • getDelta

      @Nonnull public JsonObject getDelta(@Nonnull JsonObject defaults)
      Gets a minimal set of key-value-comment settings which, if added to the supplied JsonObject, would produce this JsonObject. See BasicTests::testDiffAgainstDefaults() for more details on this comparison.
      • If a key is present in the default and not in the object, it's skipped
      • If a key is an object, a deep (recursive) comparison occurs. Comments are ignored in this comparison.
      • All other types, including lists, receive a shallow comparison of its value. The comment is ignored in this comparison.
      • Whether deep or shallow, if the key is found to be identical in value to its default, it is skipped.
      • If the key is found to be different than its default, the key, value, and comment are represented in the output.
    • getComment

      @Nullable public String getComment(@Nonnull String name)
      Returns the comment "attached to" a given key-value mapping, which is to say, the comment appearing immediately before it or the single-line comment to the right of it.
    • setComment

      public void setComment(@Nonnull String name, @Nullable String comment)
    • toJson

      public String toJson(boolean comments, boolean newlines, int depth)
      Specified by:
      toJson in class JsonElement
    • toJson

      public void toJson(Writer w, JsonGrammar grammar, int depth) throws IOException
      Specified by:
      toJson in class JsonElement
      Throws:
      IOException
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object other)
      Specified by:
      equals in interface Map<String,JsonElement>
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Map<String,JsonElement>
      Overrides:
      hashCode in class Object
    • setMarshaller

      public void setMarshaller(Marshaller marshaller)
    • getMarshaller

      public Marshaller getMarshaller()
    • get

      @Nullable public <E> E get(@Nonnull Class<E> clazz, @Nonnull String key)
    • getBoolean

      public boolean getBoolean(@Nonnull String key, boolean defaultValue)
    • getByte

      public byte getByte(@Nonnull String key, byte defaultValue)
    • getChar

      public char getChar(@Nonnull String key, char defaultValue)
    • getShort

      public short getShort(@Nonnull String key, short defaultValue)
    • getInt

      public int getInt(@Nonnull String key, int defaultValue)
    • getLong

      public long getLong(@Nonnull String key, long defaultValue)
    • getFloat

      public float getFloat(@Nonnull String key, float defaultValue)
    • getDouble

      public double getDouble(@Nonnull String key, double defaultValue)
    • recursiveGet

      @Nullable public <E> E recursiveGet(@Nonnull Class<E> clazz, @Nonnull String key)
      Gets a (potentially nested) element from this object if it exists.
      Parameters:
      clazz - The expected class of the element
      key - The keys of the nested elements, separated by periods, such as "foo.bar.baz"
      Returns:
      The element at that location, if it exists and is of the proper type, otherwise null.
    • recursiveGetOrCreate

      public <E extends JsonElement> E recursiveGetOrCreate(@Nonnull Class<E> clazz, @Nonnull String key, @Nonnull E fallback, @Nullable String comment)
      Gets a (potentially nested) element from this object if it exists, or creates it and any intermediate objects needed to put it at the indicated location in the hierarchy.
      Parameters:
      clazz - The expected class of the element
      key - The keys of the nested elements, separated by periods, such as "foo.bar.baz"
      Returns:
      The element at that location if it exists, or the newly-created element if it did not previously exist.
    • clone

      public JsonObject clone()
      Specified by:
      clone in class JsonElement
    • put

      @Nullable public JsonElement put(@Nonnull String key, @Nonnull JsonElement elem)
      Replaces a key-value mapping in this object if it exists, or adds the mapping to the end of the object if it doesn't. Returns the old value mapped to this key if there was one.
      Specified by:
      put in interface Map<String,JsonElement>
    • clear

      public void clear()
      Specified by:
      clear in interface Map<String,JsonElement>
    • containsKey

      public boolean containsKey(@Nullable Object key)
      Specified by:
      containsKey in interface Map<String,JsonElement>
    • containsValue

      public boolean containsValue(@Nullable Object val)
      Specified by:
      containsValue in interface Map<String,JsonElement>
    • entrySet

      public Set<Map.Entry<String,JsonElement>> entrySet()
      Creates a semi-live shallow copy instead of a live view
      Specified by:
      entrySet in interface Map<String,JsonElement>
    • get

      @Nullable public JsonElement get(@Nullable Object key)
      Specified by:
      get in interface Map<String,JsonElement>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<String,JsonElement>
    • keySet

      @Nonnull public Set<String> keySet()
      Returns a defensive copy instead of a live view
      Specified by:
      keySet in interface Map<String,JsonElement>
    • putAll

      public void putAll(Map<? extends String,? extends JsonElement> map)
      Specified by:
      putAll in interface Map<String,JsonElement>
    • remove

      @Nullable public JsonElement remove(@Nullable Object key)
      Specified by:
      remove in interface Map<String,JsonElement>
    • size

      public int size()
      Specified by:
      size in interface Map<String,JsonElement>
    • values

      public Collection<JsonElement> values()
      Specified by:
      values in interface Map<String,JsonElement>