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 aBigDecimalvalue from the underlying object.booleanExtract a boolean value from an underlyingBoolean.voidbooleancontainsKey(String key) Tests whether there is an object at the given key in the underlying map.doubleExtract a double value from an underlyingNumber.booleanget(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.inthashCode()booleanAnswers true iff the underlying value is a Boolean.booleanisList()Answers true iff the underlying value is aList.booleanisMap()Answers true iff the underlying value is aMap.booleanisNull()Answers true iff the underlying value is a JSON null (i.e.,JSONNull.INSTANCE).booleanisNumber()Answers true iff the underlying value is a Number.booleanisString()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 aListlistIterable(Collection<Object> xs) Cast the underlying value toList.longExtract a long value from an underlyingNumber.Iterate over JSONValue-wrapped entries in the underlying value, which is cast to aMapmapKeys()Iterate over keys of the underlying value, which is cast to aMapmapValue()Cast the underlying value toMap.Iterate over JSONValue-wrapped entries in the underlying value, which is cast to aMapstatic JSONValuenewList()Construct a newList, suitable for use as a JSON value.static JSONValuenewMap()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.intsize()Cast the underlying value toString.toString()static ObjectIf given a JSONValue, extracts the underlying object; otherwise, simply returns the value given.value()Extract the underlying value contained in this object.static JSONValueReturns 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 aBigDecimalvalue 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 aListJSONMissingIndexError- 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 aMapJSONMissingKeyError- 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:JSONSerializableCalled during serialization to JSON.- Specified by:
jsonSerializein interfaceJSONSerializable- Throws:
IOException
-
toString
-
equals
-
hashCode
-