public class JsonOps extends Object implements DynamicOps<JsonElement>
DynamicOps
adapter for JSON. Instances of this class serialize data to and from
JSON.Modifier and Type | Field and Description |
---|---|
static JsonOps |
COMPRESSED
The singleton
JsonOps instance for compressed JSON serialization. |
static JsonOps |
INSTANCE
The singleton
JsonOps instance for uncompressed JSON serialization. |
Modifier | Constructor and Description |
---|---|
protected |
JsonOps(boolean compressed)
Constructs a
JsonOps with the given settings. |
Modifier and Type | Method and Description |
---|---|
boolean |
compressMaps()
Whether the caller should serialize maps using a compressed representation.
|
<U> U |
convertTo(DynamicOps<U> outOps,
JsonElement input)
Converts a value of the serialization type to an equivalent value of another serialization type.
|
JsonElement |
createBoolean(boolean value)
Serializes a
boolean value to the serialized type. |
JsonElement |
createList(Stream<JsonElement> input)
Serializes a list of values, in the form of a
Stream to the serialized type. |
JsonElement |
createMap(Stream<(JsonElement,JsonElement)> map)
Serializes a
Stream of entries to a map serialized value. |
JsonElement |
createNumeric(Number i)
Serializes a
Number value to the serialized type. |
JsonElement |
createString(String value)
Serializes a
String value to the serialized type. |
JsonElement |
empty()
Returns the empty value of the serialization type.
|
DataResult<Boolean> |
getBooleanValue(JsonElement input)
Attempts to parse or coerce a
boolean value from the input. |
DataResult<((JsonElement) -> void) -> void> |
getList(JsonElement input)
Extracts a
Consumer from the given value that iterates over the elements of the serialized list. |
DataResult<MapLike<JsonElement>> |
getMap(JsonElement input)
Extracts the entries in the given value, returning them in a
MapLike object. |
DataResult<((JsonElement,JsonElement) -> void) -> void> |
getMapEntries(JsonElement input)
Extracts a
Consumer from the given value that iterates over the entries of the serialized map. |
DataResult<Stream<(JsonElement,JsonElement)>> |
getMapValues(JsonElement input)
Extracts a
Stream of map entries from the given serialized value. |
DataResult<Number> |
getNumberValue(JsonElement input)
Attempts to parse or coerce a
Number value from the input. |
DataResult<Stream<JsonElement>> |
getStream(JsonElement input)
Extracts a
Stream of list elements from the given serialized value. |
DataResult<String> |
getStringValue(JsonElement input)
Attempts to parse or coerce a
String value from the input. |
ListBuilder<JsonElement> |
listBuilder()
Returns a new
ListBuilder for creating lists of the serialized type. |
RecordBuilder<JsonElement> |
mapBuilder()
Returns a new
RecordBuilder for creating maps of the serialized type. |
DataResult<JsonElement> |
mergeToList(JsonElement list,
JsonElement value)
Creates a new serialized list from the given serialized list with the given value appended.
|
DataResult<JsonElement> |
mergeToList(JsonElement list,
List<JsonElement> values)
Creates a new serialized list from the given serialized list with the values from the given
List appended. |
DataResult<JsonElement> |
mergeToMap(JsonElement map,
JsonElement key,
JsonElement value)
Creates a new serialized map from the given serialized map with the given key-value mapping added.
|
DataResult<JsonElement> |
mergeToMap(JsonElement map,
MapLike<JsonElement> values)
Creates a new serialized map from the given serialized map with the entries in the given
MapLike
added. |
JsonElement |
remove(JsonElement input,
String key)
Returns an equivalent value with the entry associated with the given key removed.
|
String |
toString()
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
convertList, convertMap, createByte, createByteList, createDouble, createFloat, createInt, createIntList, createLong, createLongList, createMap, createShort, emptyList, emptyMap, get, getByteBuffer, getGeneric, getIntStream, getLongStream, getNumberValue, mergeToMap, mergeToPrimitive, set, update, updateGeneric, withDecoder, withEncoder, withParser
public static final JsonOps INSTANCE
JsonOps
instance for uncompressed JSON serialization.compressMaps()
public static final JsonOps COMPRESSED
JsonOps
instance for compressed JSON serialization.compressMaps()
public JsonElement empty()
DynamicOps
The returned value must be a singleton. That is, clients are guaranteed to be able to compare
instances to empty()
using reference equality (==
).
empty
in interface DynamicOps<JsonElement>
public <U> U convertTo(DynamicOps<U> outOps, JsonElement input)
DynamicOps
convertTo
in interface DynamicOps<JsonElement>
U
- The output serialization type.outOps
- The DynamicOps
object used to work with the output serialization type.input
- The value to convert to the output serialization type.public DataResult<Number> getNumberValue(JsonElement input)
DynamicOps
Number
value from the input.
This method may perform type coercions such as string-to-number and boolean-to-number conversion.
getNumberValue
in interface DynamicOps<JsonElement>
input
- The serialized value.DataResult
containing the parsed Number
, or else an error message.DynamicOps.getNumberValue(Object, Number)
public JsonElement createNumeric(Number i)
DynamicOps
Number
value to the serialized type.
There are no restrictions on the form of the serialized value, save that calling
DynamicOps.getNumberValue(Object)
on the returned value should result in a value equal to i
.
createNumeric
in interface DynamicOps<JsonElement>
public DataResult<Boolean> getBooleanValue(JsonElement input)
DynamicOps
boolean
value from the input.
This method may perform type coercions such as number-to-boolean conversion.
getBooleanValue
in interface DynamicOps<JsonElement>
input
- The serialized value.DataResult
containing the parsed boolean
, or else an error message.DynamicOps.createBoolean(boolean)
,
DynamicOps.getNumberValue(Object)
public JsonElement createBoolean(boolean value)
DynamicOps
boolean
value to the serialized type.
There are no restrictions on the form of the serialized value, save that calling
DynamicOps.getBooleanValue(Object)
on the returned value should result in a value whose value
is equal to value
.
createBoolean
in interface DynamicOps<JsonElement>
DynamicOps.getBooleanValue(Object)
,
DynamicOps.createByte(byte)
public DataResult<String> getStringValue(JsonElement input)
DynamicOps
String
value from the input.
This method may perform type coercions such as number-to-string and boolean-to-string conversion.
getStringValue
in interface DynamicOps<JsonElement>
input
- The serialized value.DataResult
containing the parsed String
value, or else an error message.DynamicOps.createString(String)
public JsonElement createString(String value)
DynamicOps
String
value to the serialized type.
There are no restrictions on the form of the serialized value, save that calling
DynamicOps.getStringValue(Object)
on the returned value should result in a value whose value
is equal to value
.
createString
in interface DynamicOps<JsonElement>
DynamicOps.getStringValue(Object)
public DataResult<JsonElement> mergeToList(JsonElement list, JsonElement value)
DynamicOps
mergeToList
in interface DynamicOps<JsonElement>
list
- The list that is appended to.value
- The value to append to the list.DataResult
containing the merged list, or an error message if the lefthand argument
was not a list.public DataResult<JsonElement> mergeToList(JsonElement list, List<JsonElement> values)
DynamicOps
List
appended. If list
does not represent a list, or values
could not
be appended, and error is returned.mergeToList
in interface DynamicOps<JsonElement>
list
- The list that is appended to.values
- The List
of values to append.DataResult
containing the merged list, or else an error message.public DataResult<JsonElement> mergeToMap(JsonElement map, JsonElement key, JsonElement value)
DynamicOps
String
.mergeToMap
in interface DynamicOps<JsonElement>
map
- The map to add to.key
- The key to add. The key must be convertible to String
.value
- The value to add.DataResult
containing the merged map, or an error message if either the map or
the key are of an incorrect type.DynamicOps.mergeToMap(Object, Map)
public DataResult<JsonElement> mergeToMap(JsonElement map, MapLike<JsonElement> values)
DynamicOps
MapLike
added. Only successful if the first argument is a map.mergeToMap
in interface DynamicOps<JsonElement>
map
- The map to add to.values
- A MapLike
containing the values to add.DataResult
containing the merged map, or an error message if the serialized map
is of an incorrect type.DynamicOps.mergeToMap(Object, Object, Object)
,
MapLike
public DataResult<Stream<(JsonElement,JsonElement)>> getMapValues(JsonElement input)
DynamicOps
Stream
of map entries from the given serialized value.getMapValues
in interface DynamicOps<JsonElement>
input
- The input value.DataResult
containing the extracted map entries, or an error message if input
is not a map.public DataResult<((JsonElement,JsonElement) -> void) -> void> getMapEntries(JsonElement input)
DynamicOps
Consumer
from the given value that iterates over the entries of the serialized map.
The returned value logically encapsulates a iteration over the entries in the given map,
performing some user-specified action on each entry.getMapEntries
in interface DynamicOps<JsonElement>
input
- The input value.DynamicOps.getMapValues(Object)
public DataResult<MapLike<JsonElement>> getMap(JsonElement input)
DynamicOps
MapLike
object.getMap
in interface DynamicOps<JsonElement>
input
- The serialized value.DataResult
containing the extracted entries, or an error message if the entries
could not be extracted.public JsonElement createMap(Stream<(JsonElement,JsonElement)> map)
DynamicOps
Stream
of entries to a map serialized value. The keys (first element in each entry)
must be convertible to String
.createMap
in interface DynamicOps<JsonElement>
map
- The stream of entries.DynamicOps.createMap(Map)
public DataResult<Stream<JsonElement>> getStream(JsonElement input)
DynamicOps
Stream
of list elements from the given serialized value.getStream
in interface DynamicOps<JsonElement>
input
- The serialized value.DataResult
containing the extracted elements, or an error message if the input
does not represent a list.DynamicOps.getIntStream(Object)
,
DynamicOps.getLongStream(Object)
public DataResult<((JsonElement) -> void) -> void> getList(JsonElement input)
DynamicOps
Consumer
from the given value that iterates over the elements of the serialized list.
The returned value logically encapsulates an iteration over the elements in the given list,
performing some user-specified action on each element.getList
in interface DynamicOps<JsonElement>
input
- The input value.DynamicOps.getStream(Object)
public JsonElement createList(Stream<JsonElement> input)
DynamicOps
Stream
to the serialized type.createList
in interface DynamicOps<JsonElement>
input
- The elements to serialize.public JsonElement remove(JsonElement input, String key)
DynamicOps
remove
in interface DynamicOps<JsonElement>
input
- The input value.key
- The key to remove.input
minus the given key. If input
does not contain that key,
or is not a map, then it is returned unchanged.public ListBuilder<JsonElement> listBuilder()
DynamicOps
ListBuilder
for creating lists of the serialized type.listBuilder
in interface DynamicOps<JsonElement>
public boolean compressMaps()
compressMaps
in interface DynamicOps<JsonElement>
INSTANCE
,
COMPRESSED
public RecordBuilder<JsonElement> mapBuilder()
DynamicOps
RecordBuilder
for creating maps of the serialized type.mapBuilder
in interface DynamicOps<JsonElement>