Class JSONValue
java.lang.Object
com.leastfixedpoint.json.JSONValue
- All Implemented Interfaces:
JSONSerializable
Helper class for concise traversal and construction of JSON values in the representation used by
JSONReader
and JSONWriter
. In general, will throw JSONTypeError
when the shape of a given blob of JSON doesn't
match expectations.-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAppend an object to the underlying list.Extract aBigDecimal
value from the underlying object.boolean
Extract a boolean value from an underlyingBoolean
.void
boolean
containsKey
(String key) Tests whether there is an object at the given key in the underlying map.double
Extract a double value from an underlyingNumber
.boolean
get
(int index) Retrieve the object at the index'th position in the underlying list.Retrieve the object at the index'th position in the underlying list, or the given default value if there is no such index.Retrieve the object at the given key in the underlying map.Retrieve the object at the given key in the underlying map, or the given default value if there is no such key.getRequired
(int index) Retrieve the object at the index'th position in the underlying list, throwing JSONMissingIndexError if there is no such index.getRequired
(String key) Retrieve the object at the given key in the underlying map, throwing JSONMissingKeyError if there is no such key.int
hashCode()
boolean
Answers true iff the underlying value is a Boolean.boolean
isList()
Answers true iff the underlying value is aList
.boolean
isMap()
Answers true iff the underlying value is aMap
.boolean
isNull()
Answers true iff the underlying value is a JSON null (i.e.,JSONNull.INSTANCE
).boolean
isNumber()
Answers true iff the underlying value is a Number.boolean
isString()
Answers true iff the underlying value is a String.<W extends Writer>
voidjsonSerialize
(JSONWriter<W> w) Called during serialization to JSON.list()
Iterate over JSONValue-wrapped items in the underlying value, which is cast to aList
listIterable
(Collection<Object> xs) Cast the underlying value toList
.long
Extract a long value from an underlyingNumber
.Iterate over JSONValue-wrapped entries in the underlying value, which is cast to aMap
mapKeys()
Iterate over keys of the underlying value, which is cast to aMap
mapValue()
Cast the underlying value toMap
.Iterate over JSONValue-wrapped entries in the underlying value, which is cast to aMap
static JSONValue
newList()
Construct a newList
, suitable for use as a JSON value.static JSONValue
newMap()
Construct a newMap
, suitable for use as a JSON value.Replace the object at the given key in the underlying map.Remove the object at the given key in the underlying map.Replace the object at the index'th position in the underlying list.int
size()
Cast the underlying value toString
.toString()
static Object
If given a JSONValue, extracts the underlying object; otherwise, simply returns the value given.value()
Extract the underlying value contained in this object.static JSONValue
Returns a JSONValue wrapping the given object, unless the argument is null or already a JSONValue, in which case it is returned directly.
-
Field Details
-
blob
-
-
Constructor Details
-
JSONValue
-
-
Method Details
-
wrap
-
unwrap
-
newList
-
newMap
-
value
Extract the underlying value contained in this object. -
isString
public boolean isString()Answers true iff the underlying value is a String. -
stringValue
Cast the underlying value toString
.- Throws:
JSONTypeError
- if it is not a string.
-
isNumber
public boolean isNumber()Answers true iff the underlying value is a Number. -
longValue
Extract a long value from an underlyingNumber
.- Throws:
JSONTypeError
- if it is not a number.
-
doubleValue
Extract a double value from an underlyingNumber
.- Throws:
JSONTypeError
- if it is not a number.
-
bigDecimalValue
Extract aBigDecimal
value from the underlying object.- Throws:
JSONTypeError
- if it is not aBigDecimal
.
-
isBoolean
public boolean isBoolean()Answers true iff the underlying value is a Boolean. -
booleanValue
Extract a boolean value from an underlyingBoolean
.- Throws:
JSONTypeError
- if it is not a boolean.
-
isNull
public boolean isNull()Answers true iff the underlying value is a JSON null (i.e.,JSONNull.INSTANCE
). -
checkNull
- Throws:
JSONTypeError
- if the underlying object is not a JSON null (i.e.,JSONNull.INSTANCE
).
-
isList
public boolean isList()Answers true iff the underlying value is aList
. -
listValue
Cast the underlying value toList
.- Throws:
JSONTypeError
- if it is not a list.
-
list
Iterate over JSONValue-wrapped items in the underlying value, which is cast to aList
- Throws:
JSONTypeError
- if the underlying value is not a list.
-
listIterable
- Throws:
JSONTypeError
-
isMap
public boolean isMap()Answers true iff the underlying value is aMap
. -
mapValue
Cast the underlying value toMap
.- Throws:
JSONTypeError
- if it is not a map.
-
mapKeys
Iterate over keys of the underlying value, which is cast to aMap
- Throws:
JSONTypeError
- if the underlying value is not a map.
-
mapValues
Iterate over JSONValue-wrapped entries in the underlying value, which is cast to aMap
- Throws:
JSONTypeError
- if the underlying value is not a map.
-
mapEntries
Iterate over JSONValue-wrapped entries in the underlying value, which is cast to aMap
- Throws:
JSONTypeError
- if the underlying value is not a map.
-
get
Retrieve the object at the index'th position in the underlying list.- Throws:
JSONTypeError
- if the underlying object is not aList
.
-
get
Retrieve the object at the index'th position in the underlying list, or the given default value if there is no such index.- Throws:
JSONTypeError
- if the underlying object is not aList
.
-
getRequired
Retrieve the object at the index'th position in the underlying list, throwing JSONMissingIndexError if there is no such index.- Throws:
JSONTypeError
- if the underlying object is not aList
JSONMissingIndexError
- if the list is missing the requested indexJSONSchemaError
-
set
Replace the object at the index'th position in the underlying list. Returns 'this' to allow for chaining-style building of complex values.- Throws:
JSONTypeError
- if the underlying object is not aList
.
-
add
Append an object to the underlying list. Returns 'this' to allow for chaining-style building of complex values.- Throws:
JSONTypeError
- if the underlying object is not aList
.
-
get
Retrieve the object at the given key in the underlying map.- Throws:
JSONTypeError
- if the underlying object is not aMap
.
-
get
Retrieve the object at the given key in the underlying map, or the given default value if there is no such key.- Throws:
JSONTypeError
- if the underlying object is not aMap
.
-
getRequired
Retrieve the object at the given key in the underlying map, throwing JSONMissingKeyError if there is no such key.- Throws:
JSONTypeError
- if the underlying object is not aMap
JSONMissingKeyError
- if the map is missing the requested keyJSONSchemaError
-
put
Replace the object at the given key in the underlying map. Returns 'this' to allow for chaining-style building of complex values.- Throws:
JSONTypeError
- if the underlying object is not aMap
.
-
remove
Remove the object at the given key in the underlying map. Returns 'this' to allow for chaining-style building of complex values.- Throws:
JSONTypeError
- if the underlying object is not aMap
.
-
containsKey
Tests whether there is an object at the given key in the underlying map.- Throws:
JSONTypeError
- if the underlying object is not aMap
.
-
size
- Throws:
JSONTypeError
- if the underlying object is not any of these.
-
jsonSerialize
Description copied from interface:JSONSerializable
Called during serialization to JSON.- Specified by:
jsonSerialize
in interfaceJSONSerializable
- Throws:
IOException
-
toString
-
equals
-
hashCode
-