Package com.bfo.json

Class Json

java.lang.Object
com.bfo.json.Json
Direct Known Subclasses:
COSE, JWK

public class Json extends Object
JSON (and now CBOR/Msgpack) implementation that tries to be simple, complete, fast and follow the principle ofi least surprise. This class represents every type of JSON object; the type may vary, if for instance you call put(java.lang.Object, java.lang.Object) on this object. The various isNNN methods can be used to determine the current type, or you can just call stringValue(), intValue() etc to attempt to retrieve a typed value.

Keys and Paths

put, get, has and remove all accept a key, which is an index into this object (a normal "map" or "list" key). For lists it must be an Integer, and for maps it can theoretically be any type of object, but is currently limited in to String, Number or Boolean. Non-String keys are only preserved when serialising to CBOR or Msgpack, and will be converted to Strings when the object is serialised to JSON;

putPath, getPath, hasPath and removePath all accept a path, which is a String identifying a descendent. These may be compound paths, eg a.b, a.b[2]. If a part of the path contains a dot or square bracket it can be quoted and referenced inside square brackets, eg a.b["dotted.key"]. For speed, values supplied between two quotes simply have their quotes removed; they are not unescaped. So json.put("\"\"\", true) will create the structure {"\\"":true}. Paths that traverse lists are always converted to integers, and paths that traverse maps are converted to strings.

Note: prior to version 5, put, get, has and remove accepted a path, as non-String keys could not be stored.

Serialization

Object are read from the read(java.lang.CharSequence) methods and written with the write(java.lang.Appendable, com.bfo.json.JsonWriteOptions) method. The process can be controlled by specifying JsonReadOptions or JsonWriteOptions as appropriate, although the default will read/write as defined in RFC8259 or RFC7049 as appropriate. In all cases the Stream is not closed at the end of the read or write.

Since version 2, objects can also be serialized as CBOR (and since version 3, Msgpack), as defined in RFC7049 and the Msgpack spec. There are some differences between the JSON and CBOR/Msgpack object models which are significant, and to combine the two into one interface, some minor limitations to the datamodels are in place.

  1. Unlike JSON, CBOR/Msgpack supports binary data, which we read as a ByteBuffer - these can be identified with the isBuffer() method. When serializing a ByteBuffer to JSON, it will be Base-64 encoded with no padding, as recommended in RFC7049. By default the "URL- and filename-safe" variation of BAse64 will be used when writing (see JsonWriteOptions.setBase64Standard(boolean)). When reading, all Base64 variations can be parsed. The ByteBuffer position is ignored; it will be reset to zero before every read, write, or when it is returned from bufferValue().
  2. JSON does not support NaN or infinite floating point values. When serializing these values to JSON, they will be serialized as null - this matches the behavior of all web-browsrs.
  3. CBOR supports tags on JSON value, which can be accessed with the setTag() and getTag() methods. Tags can be set on any object, but will be ignored when serializing to JSON. CBOR supports positive value tags of any value, but these are limited to 63 bits by this API. If values higher than that are encountered when reading, readCbor() method will throw an IOException.
  4. Msgpack supports "ext" types, which are treated as ByteBuffer objects with the extension type set retrievable by calling getTag() - the tag is a number from 0..255. Msgpack does not support integer values greater than 64-bit; attempting to write these will throw an IOException.
  5. CBOR/Msgpack support complex key types in maps, such as lists or other maps. If these are encountered with this API when reading, they will be converted to strings by default, or (if JsonReadOptions.setFailOnComplexKeys(boolean) is set) throw an IOException. CBOR/Msgpack also supports duplicate keys in maps - this abomination is not allowed in this API, and the readCbor() and readMsgpack() methods will throw an IOException if found.
  6. CBOR supports the "undefined" value which is distinct from null. This can be created using the UNDEFINED constant, and tested for with isUndefined(java.lang.Object) (since version 5)
  7. CBOR allows for a number of undefined special types to be used without error. These will be loaded as undefined values, with a Tag set that identifies the type. There is no such conversion when writing; these values will be written as a tagged undefined, not the original special type.

Events

Listeners can be attached to a JSON object, and will received JsonEvents when changes are made to this item or its descendants. See the JsonEvent API docs for an example of how to use this to record changes to an object.

Thread Safety

This object is not synchronized, and if it is being modified in one thread while being read in another, external locking should be put in place.

Examples

 Json json = Json.read("{}");
 json.putPath("a.b[0]", 0);
 assert json.get("a").get("b") == json.getPath("a.b");
 assert json.getPath("a.b[0]").type().equals("number");
 assert json.getPath("a.b[0]").isNumber();
 assert json.getPath("a.b[0]").intValue() == 0;
 assert json.get("a").type().equals("map");
 json.putPath("a.b[2]", 1);
 assert json.toString().equals("{\"a\":{\"b\":[0,null,2]}}");
 json.putPath("a.b", true);
 assert json.toString().equals("{\"a\":{\"b\":true}}");
 json.write(System.out, null);
 Json json2 = Json.read("[]");
 json2.put(0, 0);
 json2.put(2, 2);
 assert json2.toString().equals("[0,null,2]");
 json2.put("a", "a");
 assert json2.toString().equals("{\"0\":0,\"2\":2,\"a\":\"a"}");
 
  • Field Details

    • UNDEFINED

      public static final Object UNDEFINED
      A constant object that can be passed into the Json constructor to create a Cbor "undefined" value
      Since:
      5
  • Constructor Details

    • Json

      public Json(Object object)

      Create a new Json object that represents the specified object. The object should be a CharSequence, Boolean, Number, ByteBuffer, byte[], Map or Collection; if a Map or Collection, the collection is copied rather than referenced, and the values must also meet this criteria. A ByteBuffer (or byte[]) is not a native Json type, but is used for CBOR. The buffer is not copied, it is stored by reference.

      An fast alternative method for creating a Json object representing an empty map or list is to call Json.read("{}") or Json.read("[]")

      Parameters:
      object - the object
      Throws:
      ClassCastException - if these conditions are not met
    • Json

      public Json(Object object, JsonFactory factory)
      Create a new Json object that represents the specified object. As for Json(Object), but first attempts to convert the object to a Json object by calling JsonFactory.toJson(java.lang.Object).
      Parameters:
      object - the object
      factory - the factory for conversion, which may be null. Will be passed to setFactory(com.bfo.json.JsonFactory)
      Throws:
      ClassCastException - if the object cannot be converted to Json
  • Method Details

    • setFactory

      public Json setFactory(JsonFactory factory)
      Set the default JsonFactory for this object and its descendants. Any objects passed into put() will be converted using this factory. The default is null
      Parameters:
      factory - the factory
      Returns:
      this
      Since:
      2
    • getFactory

      public JsonFactory getFactory()
      Return the default JsonFactory, as set by setFactory(com.bfo.json.JsonFactory)
      Returns:
      the Factory set by setFactory(com.bfo.json.JsonFactory)
      Since:
      2
    • read

      public static Json read(CharSequence in)
      Read a Json object from the specified String. The object may be a structured type or a primitive value (boolean, number or string). The values "{}" and "[]" may be supplied to create a new Json map or array.
      Parameters:
      in - the String, which must not be null or empty.
      Returns:
      the Json object
      Throws:
      IllegalArgumentException - if the JSON is invalid
    • read

      public static Json read(InputStream in, JsonReadOptions options) throws IOException

      Read a Json object from the specified InputStream. If the stream begins with a valid byte-order mark, that will be used determine the encoding (which must be UTF-8 or UTF-16), otherwise the stream will be sniffed for UTF-16, and otherwise parsed as UTF-8.

      If you are sure the InputStream is in UTF-8 and has no byte-order mark, as recommended in RFC8259, then you're better off calling read(new InputStreamReader(in, "UTF-8"), options) as this will remove the possibility of guessing an incorrect encoding

      Parameters:
      in - the InputStream
      options - the options to use for reading, or null to use the default
      Returns:
      the Json object
      Throws:
      IOException - if an IO exception was encountered during reading
      IllegalArgumentException - if the JSON is invalid
    • read

      public static Json read(Reader in, JsonReadOptions options) throws IOException
      Read a Json object from the specified Reader.
      Parameters:
      in - the Reader
      options - the options to use for reading, or null to use the default
      Returns:
      the Json object
      Throws:
      IOException - if an IO exception was encountered during reading
      IllegalArgumentException - if the JSON is invalid
    • readCbor

      public static Json readCbor(InputStream in, JsonReadOptions options) throws IOException
      Read a CBOR formatted object from the specified InputStream.
      Parameters:
      in - the InputStream
      options - the options to use for reading, or null to use the default
      Returns:
      the Json object
      Throws:
      IOException - if an I/O exception was encountered during reading or the stream does not meet the CBOR format
      Since:
      2
    • readCbor

      public static Json readCbor(ByteBuffer in, JsonReadOptions options) throws IOException
      Read a CBOR formatted object from the specified ByteBuffer.
      Parameters:
      in - the ByteBuffer
      options - the options to use for reading, or null to use the default
      Returns:
      the Json object
      Throws:
      IOException - if an I/O exception was encountered during reading or the stream does not meet the CBOR format
      Since:
      2
    • readMsgpack

      public static Json readMsgpack(InputStream in, JsonReadOptions options) throws IOException
      Read a Msgpack formatted object from the specified InputStream.
      Parameters:
      in - the InputStream
      options - the options to use for reading, or null to use the default
      Returns:
      the Json object
      Throws:
      IOException - if an I/O exception was encountered during reading or the stream does not meet the MsgPack format
      Since:
      3
    • readMsgpack

      public static Json readMsgpack(ByteBuffer in, JsonReadOptions options) throws IOException
      Read a Msgpack formatted object from the specified ByteBuffer.
      Parameters:
      in - the ByteBuffer
      options - the options to use for reading, or null to use the default
      Returns:
      the Json object
      Throws:
      IOException - if an I/O exception was encountered during reading or the stream does not meet the CBOR format
      Since:
      3
    • writeCbor

      public OutputStream writeCbor(OutputStream out, JsonWriteOptions options) throws IOException
      Write the Json object in the CBOR format to the specified output
      Parameters:
      out - the output
      options - the JsonWriteOptions to use when writing, or null to use the default
      Returns:
      the "out" parameter
      Throws:
      IOException - if an IOException is thrown while writing
      Since:
      2
    • writeMsgpack

      public OutputStream writeMsgpack(OutputStream out, JsonWriteOptions options) throws IOException
      Write the Json object in the Msgpack format to the specified output
      Parameters:
      out - the output
      options - the JsonWriteOptions to use when writing, or null to use the default
      Returns:
      the "out" parameter
      Throws:
      IOException - if an IOException is thrown while writing
      Since:
      3
    • addListener

      public Json addListener(JsonListener listener)
      Add a JsonListener to this class, if it has not already been added. The listener will received events for any changes to this object or its descendants - the JsonEvent will reference which object the event relates to, and the find(com.bfo.json.Json) method can be used to construct the path to that object if of interest.
      Parameters:
      listener - listener to add, which may not be null
      Returns:
      this object
    • removeListener

      public Json removeListener(JsonListener listener)
      Remove a JsonListener from this class
      Parameters:
      listener - listener to remove
      Returns:
      this object
    • getListeners

      public Collection<JsonListener> getListeners()
      Return the read-only set of listeners on this class.
      Returns:
      a read-only collection of listeners, which will never be null
    • write

      public Appendable write(Appendable out, JsonWriteOptions options) throws IOException
      Write the Json object to the specified output
      Parameters:
      out - the output
      options - the JsonWriteOptions to use when writing, or null to use the default
      Returns:
      the "out" parameter
      Throws:
      IOException - if an IOException is thrown while writing
    • duplicate

      public Json duplicate()
      Create and return a deep copy of this Json tree. Note that ByteBuffers values will not be cloned, and the returned item will have no listeners
      Returns:
      a deep copy of this item
    • parent

      public Json parent()
      Get the parent of this node, if known
      Returns:
      the items parent, or null if it has not been added to another Json object
    • find

      public String find(Json descendant)

      Get the path from this node to the specified object, which should be a descendant of this node. If the parameter is null or not a descendant of this object, this method returns null.

      Specifically, if this method returns not null then it is the case that this.getPath(this.find(node)) == node.

      Implementation note: this method is implemented by traversing up from the descendant to this object; possible because a Json object can only have one parent. So the operation is O(n).

      Parameters:
      descendant - the presumed descendant of this object to find in the tree
      Returns:
      the path from this node to the descendant object
      Since:
      5 (was called "find" prior to that)
    • put

      public Json put(Object key, Object value)
      Put the specified value into this object with the specified key. Although the key can be any value, it will collapse to a String when serialised as JSON. If the object is not a Json, it will be converted with the factory set by setFactory(), if any.
      Parameters:
      key - the key - if this object is a list and the key is a non-negative integer, it will be used as the list index. Otherwise this object will be converted to a map. Must not be null.
      value - the value to insert, which must not be this or an ancestor of this
      Returns:
      the object that was previously found at that path, which may be null
    • get

      public Json get(Object key)
      Return the specified child of this object, or null if no value exists at the specified key
      Parameters:
      key - the key, which should be an integer (for lists) or any value for maps
      Returns:
      the Json object at that path or null if none exists
    • has

      public boolean has(Object object)
      If object is a Json object, return true if this object is a list or map, and contains that value. Otherwise return true if this object is a list or map and contains a non-null/non-undefined object at that entry.
      Parameters:
      object - the object
      Returns:
      true if this is a list or map and the object is either a Json and present in this object as a value, or is used as a key for a non-null/non-undefined value
      Since:
      5
    • remove

      public Json remove(Object object)
      Remove the item at the specified path from this object or one of its descendants. Or, if object is a Json, remove that value from this list or map. If called on an object that is not a list or map, this method has no effect.
      Parameters:
      object - if a Json object, the value to remove, otherwise the key to remove from this object.
      Returns:
      the object that was removed, or null if nothing was removed
      Since:
      5
    • putPath

      public Json putPath(String path, Object value)
      Put the specified value into this object or one of its descendants by parsing the specified path. If the path specifies a compound key than any intermediate descendants are created as required. If the path specifies an existing object then the old object (which may be a subtree) is removed and returned. The object will be converted with the factory set by setFactory(), if any.
      Parameters:
      path - the key, which may be a compound key (e.g "a.b" or "a.b[2]") and must not be null
      value - the value to insert, which must not be this or an ancestor of this
      Returns:
      the object that was previously found at that path, which may be null
      Since:
      5 - prior that that revision was called "put"
    • getPath

      public Json getPath(String path)
      Return the specified descendant of this object, or null if no value exists at the specified path.
      Parameters:
      path - the path, which must not be null
      Returns:
      the Json object at that path or null if none exists
    • hasPath

      public boolean hasPath(String path)
      Return true if this object has a non-null/non-undefined descendant at the specified path.
      Parameters:
      path - the path
      Returns:
      true if this object is a list or map, it has the specified descendant and the descendant is not null
    • removePath

      public Json removePath(String path)
      Remove the item at the specified path from this object or one of its descendants.
      Parameters:
      path - the key, which may be a compound key (e.g "a.b" or "a.b[2]") and must not be null
      Returns:
      the object that was removed, or null if nothing was removed
      Since:
      5 was called remove() prior to version 5
    • size

      public int size()
      Return the size of this object, or zero if this object is a number, string, buffer, boolean or null.
      Returns:
      the size of the object
    • getTag

      public long getTag()
      Return the tag for this item. Tags are only used with the CBOR serialization. Although CBOR alows positive tag values of any size, this implementation limits them to 63 bits. If no tag is set (the default) this method returns -1.
      Returns:
      the tag, or -1 if none is set.
      Since:
      2
    • setTag

      public Json setTag(long tag)
      Set the tag for this item. Tags are only used with the CBOR serialization, so they will be ignored when writing to Json. Although CBOR allows positive value tags of any size, this implementation limits them to 63 bits.
      Parameters:
      tag - the tag, or a negative number to remove the tag.
      Returns:
      this
      Since:
      2
    • isEmpty

      public boolean isEmpty()
      Return true if this node is a number, string, buffer, boolean, null or an empty map or list.
      Returns:
      true if the node is a leaf node.
    • isNull

      public boolean isNull(Object key)
      Return true if the specified child of this object is of type "null". Equivalent to get(key) != null && get(key).isNull(). It is not the same as the key being missing.
      Parameters:
      key - the key
      Returns:
      true if the child exists and is null
    • isUndefined

      public boolean isUndefined(Object key)
      Return true if the specified child of this object is of type "undefined". Equivalent to get(key) != null && get(key).isUndefined(). It is not the same as the key being missing.
      Parameters:
      key - the key
      Returns:
      true if the child exists and is undefined
    • isBuffer

      public boolean isBuffer(Object key)
      Return true if the specified child of this object is of type "buffer". Equivalent to get(key) != null && get(key).isBuffer().
      Parameters:
      key - the key
      Returns:
      true if the child exists and is a buffer
    • isString

      public boolean isString(Object key)
      Return true if the specified child of this object is of type "string". Equivalent to get(key) != null && get(key).isString().
      Parameters:
      key - the key
      Returns:
      true if the child exists and is a string
    • isNumber

      public boolean isNumber(Object key)
      Return true if the specified child of this object is of type "number". Equivalent to get(key) != null && get(key).isNumber()
      Parameters:
      key - the key
      Returns:
      true if the child exists and is a number
    • isBoolean

      public boolean isBoolean(Object key)
      Return true if the specified child of this object is of type "boolean". Equivalent to get(key) != null && get(key).isBoolean()
      Parameters:
      key - the key
      Returns:
      true if the child exists and is a boolean
    • isList

      public boolean isList(Object key)
      Return true if the specified child of this object is of type "list". Equivalent to get(key) != null && get(key).isList()
      Parameters:
      key - the key
      Returns:
      true if the child exists and is a list
    • isMap

      public boolean isMap(Object key)
      Return true if the specified wchild of this object is of type "map". Equivalent to get(key) != null && get(key).isMap()
      Parameters:
      key - the key
      Returns:
      true if the child exists and is a map
    • leafIterator

      public Iterator<Map.Entry<String,Json>> leafIterator()
      Return an Iterator that will descend through every leaf node under this object in a depth-first traveral. The returned keys are converter to Strings; they are relative to this node's path and will always return non-null if passed into getPath(java.lang.StringBuilder, com.bfo.json.Json). If this is called on a leaf nodes, it returns an empty iterator
      Returns:
      an Iterator as described
    • sort

      public Json sort()
      For Json objects that are maps, sort the map keys. For other types this is a no-op
      Returns:
      this
      Since:
      5
    • type

      public String type()
      Return the type of this node, which may be "number", "string", "boolean", "list", "map", "buffer", "null" or (since v5) "undefined"
      Returns:
      the object type
    • isNull

      public boolean isNull()
      Return true if this node is null
      Returns:
      true if the object is null
    • isUndefined

      public boolean isUndefined()
      Return true if this node is undefined. Undefined is a CBOR concept; in JSON serialization, this will collapse to null
      Returns:
      true if the object is null
      Since:
      5
    • isNumber

      public boolean isNumber()
      Return true if this node is a "number"
      Returns:
      true if the object is a number
    • isBoolean

      public boolean isBoolean()
      Return true if this node is a "boolean"
      Returns:
      true if the object is a boolean
    • isString

      public boolean isString()
      Return true if this node is a "string"
      Returns:
      true if the object is a string
    • isBuffer

      public boolean isBuffer()
      Return true if this node is a "buffer"
      Returns:
      true if the object is a buffer
    • isMap

      public boolean isMap()
      Return true if this node is a "map"
      Returns:
      true if the object is a map
    • isList

      public boolean isList()
      Return true if this node is a "list"
      Returns:
      true if the object is a list
    • value

      public Object value()
      Return the value of this Json as a plain Java object, which may be a String, Boolean, Number, List, Map, ByteBuffer or null.
      Returns:
      the object value
    • setValue

      public Json setValue(Json json)
      Copy the internal value from the specified Json object to this object. This can be useful to update the value of a Json object in a structure, without having to modify the structure. For example:
       Json a = new Json(999);
       Json b = new Json("string");
       a.setValue(b);
       System.out.println(a); // "string"
       
      Parameters:
      json - the json object that is the source of the intended value of this object
      Returns:
      this
    • stringValue

      public String stringValue()
      Return the value of this node as a String. This method will always succeed. It will return null if this object represents null, but it otherwise identical to toString()
      Returns:
      the string value of this object
    • stringValue

      public String stringValue(Object key)
      If the specified child of this object exists call the stringValue() method on it, otherwise return null
      Parameters:
      key - the key
      Returns:
      the string value of that object
      Since:
      4
    • bufferValue

      public ByteBuffer bufferValue()

      Return the value of this node as a ByteBuffer. ByteBuffers only exist natively in the CBOR and Msgpack serialization. Note that every ByteBuffer created by the API will be backed by an array of the same size; it is guaranteed that buffer.array().length == buffer.limit() && buffer.arrayOffset() == 0.

      • A string will be decoded using Base64 (no padding) and, if valid returned as a ByteBuffer. If invalid, a ClassCastException will be thrown
      • null will return null
      Returns:
      the buffer value of this object, which will always have position=0
      Throws:
      ClassCastException - if none of these conditions are met
      Since:
      2
      See Also:
    • bufferValue

      public ByteBuffer bufferValue(Object key)
      If the specified child of this object exists call the bufferValue() method on it, otherwise return null
      Parameters:
      key - the key
      Returns:
      the buffer value of that object
      Since:
      4
    • numberValue

      public Number numberValue()
      Return the value of this node as a Number.
      • A number will be returned as an Integer, Long, Double, BigInteger or BigDecimal as appropriate
      • A string in the format specified in RFC8259 will be parsed and returned as an Integer, Long or Double (if "strictTypes" was not specified)
      • An empty string will return 0 (if "looseEmptyStrings" was specified))
      • A boolean will return an Integer that is 1 or 0 (if "strictTypes" was not specified)
      • null will return null
      Returns:
      the number value of this object
      Throws:
      ClassCastException - if none of these conditions are met
      See Also:
    • numberValue

      public Number numberValue(Object key)
      If the specified child of this object exists call the intValue() method on it, otherwise return null
      Parameters:
      key - the key
      Returns:
      the number value of that object
      Since:
      4
    • intValue

      public int intValue()
      Return the value of this node as an integer.
      • A number will be converted to int
      • A string that can be parsed with Integer.parseInt will converted (if "strictTypes" was not specified)
      • An empty string will return 0 (if "looseEmptyStrings" was specified))
      • A boolean will return 1 or 0 (if "strictTypes" was not specified)
      Returns:
      the int value of this object
      Throws:
      ClassCastException - if none of these conditions are met
      See Also:
    • intValue

      public int intValue(Object key)
      If the specified child of this object exists call the intValue() method on it, otherwise return 0
      Parameters:
      key - the key
      Returns:
      the int value of that object
      Since:
      4
    • longValue

      public long longValue()
      Return the value of this node as a long.
      • A number will be converted to long
      • A string that can be parsed with Long.parseLong will converted (if "strictTypes" was not specified)
      • An empty string will return 0 (if "looseEmptyStrings" was specified))
      • A boolean will return 1 or 0 (if "strictTypes" was not specified)
      Returns:
      the long value of this object
      Throws:
      ClassCastException - if none of these conditions are met
      See Also:
    • longValue

      public long longValue(Object key)
      If the specified child of this object exists call the longValue() method on it, otherwise return 0
      Parameters:
      key - the key
      Returns:
      the long value of that object
      Since:
      4
    • floatValue

      public float floatValue()
      Return the value of this node as an float.
      • A number will be converted to float
      • A string in the format specified in RFC8259 will be parsed and returned (if "strictTypes" was not specified)
      • An empty string will return 0 (if "looseEmptyStrings" was specified)
      • A boolean will return 1 or 0 (if "strictTypes" was not specified)
      Returns:
      the float value of this object
      Throws:
      ClassCastException - if none of these conditions are met
      See Also:
    • floatValue

      public float floatValue(Object key)
      If the specified child of this object exists call the floatValue() method on it, otherwise return 0
      Parameters:
      key - the key
      Returns:
      the float value of that object
      Since:
      4
    • doubleValue

      public double doubleValue()
      Return the value of this node as a double.
      • A number will be converted to double
      • A string in the format specified in RFC8259 will be parsed and returned (if "strictTypes" was not specified)
      • An empty string will return 0 (if "looseEmptyStrings" was specified)
      • A boolean will return 1 or 0 (if "strictTypes" was not specified)
      Returns:
      the double value of this object
      Throws:
      ClassCastException - if none of these conditions are met
      See Also:
    • doubleValue

      public double doubleValue(Object key)
      If the specified child of this object exists call the doubleValue() method on it, otherwise return 0
      Parameters:
      key - the key
      Returns:
      the double value of that object
      Since:
      4
    • booleanValue

      public boolean booleanValue()
      Return the value of this node as a boolean.
      • A boolean will return its value
      • A number evaluating to 0 will return false, otherwise it will return true (if "strictTypes" was not specified)
      • The string "false", or one that represents a number evaluating to 0 will return false, otherwise it will return true (if "strictTypes" was not specified)
      • An empty string will return false (if "looseEmptyStrings" was specified)
      Returns:
      the boolean value of this object
      Throws:
      ClassCastException - if none of these conditions are met
      See Also:
    • booleanValue

      public boolean booleanValue(Object key)
      If the specified child of this object exists call the booleanValue() method on it, otherwise return false
      Parameters:
      key - the key
      Returns:
      the boolean value of that object
      Since:
      4
    • mapValue

      public Map<Object,Json> mapValue()
      Return the value of this node as a map. The returned Map is read-only
      Returns:
      the read-only map value of this object
      Throws:
      ClassCastException - if the node is not a map.
    • mapValue

      public Map<Object,Json> mapValue(Object key)
      If the specified descendant of this object exists call the mapValue() method on it, otherwise return null
      Parameters:
      key - the key
      Returns:
      the read-only map value of that object
      Since:
      4
    • listValue

      public List<Json> listValue()
      Return the value of this node as a list. The returned List is read-only
      Returns:
      the read-only list value of this object
      Throws:
      ClassCastException - if the node is not a list
    • listValue

      public List<Json> listValue(Object key)
      If the specified wchild of this object exists call the listValue() method on it, otherwise return null
      Parameters:
      key - the key
      Returns:
      the read-only list value of that object
      Since:
      4
    • objectValue

      public Object objectValue()
      Return this Json value as a plain object. Unlike value() this method will traverse through its descendants, converting Json objects to plain objects so that the returned object is guaranteed to have no reference to this package. The returned value is as follows
      • If this object is null, return null
      • If a factory is set and JsonFactory.fromJson() returns a non-null value, return that value
      • If this value is a string, buffer, number or boolean, return the value from value()
      • If this value is a list or map, populate the map values with the output of this method and return as a Map<String,Object> or List<Object>
      Returns:
      a String, Number, Boolean, Map<String,Object>, List<Object> or null as described
    • hashCode

      public int hashCode()
      Return a hashCode based on the value()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object o)
      Return true if the specified object is a Json object and has an equal value and tag
      Overrides:
      equals in class Object
      Parameters:
      o - the object to compare to
    • toString

      public String toString()
      Return a String representation of this Json object. Roughly equivalent to calling return write(new StringBuilder(), null).toString()
      Overrides:
      toString in class Object
      Returns:
      the serialized object
    • toString

      public String toString(JsonWriteOptions options)
      Return a String representation of this Json object with the specified serialization options. Equivalent to calling return write(new StringBuilder(), options).toString()
      Parameters:
      options - the JsonWriteOptions to use for serializing, or null to use the default
      Returns:
      the serialized object
      Since:
      5
    • toCbor

      public ByteBuffer toCbor()
      Return a Cbor representation of this Json object.
      Returns:
      the Cbor representation as an array-backed ByteBuffer
      Since:
      5
    • toCbor

      public ByteBuffer toCbor(JsonWriteOptions options)
      Return a Cbor representation of this Json object with the specified options
      Parameters:
      options - the JsonWriteOptions to use for serializing, or null to use the default
      Returns:
      the Cbor representation as an array-backed ByteBuffer
      Since:
      5