Package com.vaadin.flow.internal
Class JsonUtils
java.lang.Object
com.vaadin.flow.internal.JsonUtils
Helpers for using
elemental.json
.
For internal use only. May be renamed or removed in a future release.
- Since:
- 1.0
- Author:
- Vaadin Ltd
-
Method Summary
Modifier and TypeMethodDescriptionstatic Collector<elemental.json.JsonValue,
elemental.json.JsonArray, elemental.json.JsonArray> asArray()
Creates a collector that collects values into a JSON array.static elemental.json.JsonObject
beanToJson
(Object bean) Converts the given bean to JSON.static boolean
booleanEqual
(elemental.json.JsonValue a, elemental.json.JsonValue b) static elemental.json.JsonArray
createArray
(elemental.json.JsonValue... values) Creates a new JSON array with the given values.static <T> elemental.json.JsonObject
createObject
(Map<String, T> map, Function<T, elemental.json.JsonValue> itemToJson) Converts the given map into a JSON object by converting each map value to a JSON value.static boolean
jsonEquals
(elemental.json.JsonValue a, elemental.json.JsonValue b) Compares two json values for deep equality.static elemental.json.JsonArray
listToJson
(List<?> list) Converts the given list to JSON.static elemental.json.JsonObject
Converts the given map to JSON.static boolean
numbersEqual
(elemental.json.JsonValue a, elemental.json.JsonValue b) static DoubleStream
numberStream
(elemental.json.JsonArray array) Creates a double stream from a JSON array of numbers.static Stream<elemental.json.JsonObject>
objectStream
(elemental.json.JsonArray array) Creates a stream from a JSON array of objects.static <T> T
readToObject
(elemental.json.JsonObject jsonObject, Class<T> tClass) Converts JsonObject into Java object of given type.static <T> T
readValue
(elemental.json.JsonValue jsonValue, com.fasterxml.jackson.core.type.TypeReference<T> typeReference) Converts JsonValue into Java object of given type.static <T> T
Converts JsonValue into Java object of given type.static <T extends elemental.json.JsonValue>
Stream<T>stream
(elemental.json.JsonArray array) Creates a stream from a JSON array.static boolean
stringEqual
(elemental.json.JsonValue a, elemental.json.JsonValue b) static elemental.json.JsonValue
writeValue
(Object object) Converts Java object into JsonValue.
-
Method Details
-
jsonEquals
public static boolean jsonEquals(elemental.json.JsonValue a, elemental.json.JsonValue b) Compares two json values for deep equality.This is a helper for overcoming the fact that
JsonValue
doesn't overrideObject.equals(Object)
andJsonValue.jsEquals(JsonValue)
is defined to use JavaScript semantics where arrays and objects are equals only based on identity.- Parameters:
a
- the first json value to check, may not be nullb
- the second json value to check, may not be null- Returns:
true
if both json values are the same;false
otherwise
-
stringEqual
public static boolean stringEqual(elemental.json.JsonValue a, elemental.json.JsonValue b) -
booleanEqual
public static boolean booleanEqual(elemental.json.JsonValue a, elemental.json.JsonValue b) -
numbersEqual
public static boolean numbersEqual(elemental.json.JsonValue a, elemental.json.JsonValue b) -
stream
Creates a stream from a JSON array.- Type Parameters:
T
- the stream type- Parameters:
array
- the JSON array to create a stream from- Returns:
- a stream of JSON values
-
objectStream
Creates a stream from a JSON array of objects. This method does not verify that all items in the array are actually JSON objects instead of some other JSON type.- Parameters:
array
- the JSON array to create a stream from- Returns:
- a stream of JSON objects
-
numberStream
Creates a double stream from a JSON array of numbers. This method does not verify that all items in the array are actually JSON numbers instead of some other JSON type.- Parameters:
array
- the JSON array to create a stream from- Returns:
- a double stream of the values in the array
-
asArray
public static Collector<elemental.json.JsonValue,elemental.json.JsonArray, asArray()elemental.json.JsonArray> Creates a collector that collects values into a JSON array.- Returns:
- the collector
-
createArray
public static elemental.json.JsonArray createArray(elemental.json.JsonValue... values) Creates a new JSON array with the given values.- Parameters:
values
- the values that should be in the created array- Returns:
- the created array
-
createObject
public static <T> elemental.json.JsonObject createObject(Map<String, T> map, Function<T, elemental.json.JsonValue> itemToJson) Converts the given map into a JSON object by converting each map value to a JSON value.- Type Parameters:
T
- the type of the map values- Parameters:
map
- the map to convert into a JSON objectitemToJson
- callback for converting map values to JSON- Returns:
- the created object
-
beanToJson
Converts the given bean to JSON.- Parameters:
bean
- the bean to convert, notnull
- Returns:
- a JSON representation of the bean
-
listToJson
Converts the given list to JSON.- Parameters:
list
- the list to convert, notnull
- Returns:
- a JSON representation of the bean
-
mapToJson
Converts the given map to JSON.- Parameters:
map
- the map to convert, notnull
- Returns:
- a JSON representation of the bean
-
readToObject
Converts JsonObject into Java object of given type.- Type Parameters:
T
- type of result instance- Parameters:
jsonObject
- JSON object to convert, notnull
tClass
- class of converted object instance- Returns:
- converted object instance
-
readValue
Converts JsonValue into Java object of given type.- Type Parameters:
T
- type of result instance- Parameters:
jsonValue
- JSON value to convert, notnull
tClass
- class of converted object instance- Returns:
- converted object instance
-
readValue
public static <T> T readValue(elemental.json.JsonValue jsonValue, com.fasterxml.jackson.core.type.TypeReference<T> typeReference) Converts JsonValue into Java object of given type.- Type Parameters:
T
- type of result instance- Parameters:
jsonValue
- JSON value to convert, notnull
typeReference
- type reference of converted object instance- Returns:
- converted object instance
-
writeValue
Converts Java object into JsonValue.- Parameters:
object
- Java object to convert- Returns:
- converted JSON value
-