Package com.vaadin.flow.internal
Class JacksonUtils
java.lang.Object
com.vaadin.flow.internal.JacksonUtils
Helpers for using
jackson.
For internal use only. May be renamed or removed in a future release.
- Since:
- 24.7
- Author:
- Vaadin Ltd
-
Method Summary
Modifier and TypeMethodDescriptionstatic Collector<com.fasterxml.jackson.databind.JsonNode,com.fasterxml.jackson.databind.node.ArrayNode, com.fasterxml.jackson.databind.node.ArrayNode> asArray()Creates a collector that collects values into a JSON array.static com.fasterxml.jackson.databind.node.ObjectNodebeanToJson(Object bean) Converts the given bean to JSON.static booleanbooleanEqual(com.fasterxml.jackson.databind.JsonNode a, com.fasterxml.jackson.databind.JsonNode b) Compare boolean value of two JsonNode values.static com.fasterxml.jackson.databind.node.ArrayNodecreateArray(com.fasterxml.jackson.databind.JsonNode... values) Creates a new JSON array with the given values.static com.fasterxml.jackson.databind.node.ArrayNodeCreate a new ArrayNode.static com.fasterxml.jackson.databind.JsonNodecreateNode(Object value) Create a JsonNode from value.static <T> com.fasterxml.jackson.databind.node.ObjectNodecreateObject(Map<String, T> map, Function<T, com.fasterxml.jackson.databind.JsonNode> itemToJson) Converts the given map into a JSON object by converting each map value to a JSON value.static com.fasterxml.jackson.databind.node.ObjectNodeCreate a new ObjectNode.getKeys(com.fasterxml.jackson.databind.JsonNode node) static com.fasterxml.jackson.databind.ObjectMapperstatic booleanjsonEquals(com.fasterxml.jackson.databind.JsonNode a, com.fasterxml.jackson.databind.JsonNode b) Compares two json values for deep equality.static com.fasterxml.jackson.databind.node.ArrayNodelistToJson(List<?> list) Converts the given list to JSON.static com.fasterxml.jackson.databind.node.ObjectNodeConverts the given map to JSON.static booleannumbersEqual(com.fasterxml.jackson.databind.JsonNode a, com.fasterxml.jackson.databind.JsonNode b) Compare number value of two JsonNode values.static DoubleStreamnumberStream(com.fasterxml.jackson.databind.node.ArrayNode array) Creates a double stream from a JSON array of numbers.static Stream<com.fasterxml.jackson.databind.JsonNode>objectStream(com.fasterxml.jackson.databind.node.ArrayNode array) Creates a stream from a JSON array of objects.static <T> TreadToObject(com.fasterxml.jackson.databind.node.ObjectNode jsonObject, Class<T> tClass) Converts JsonObject into Java object of given type.static com.fasterxml.jackson.databind.node.ObjectNodeRead Json string to JsonNode.static <T> TreadValue(com.fasterxml.jackson.databind.node.ObjectNode jsonValue, com.fasterxml.jackson.core.type.TypeReference<T> typeReference) Converts ObjectNode into Java object of given type.static <T> TConverts ObjectNode into Java object of given type.static <T extends com.fasterxml.jackson.databind.JsonNode>
Stream<T>stream(com.fasterxml.jackson.databind.node.ArrayNode array) Creates a stream from a JSON array.static booleanstringEqual(com.fasterxml.jackson.databind.JsonNode a, com.fasterxml.jackson.databind.JsonNode b) Compare String value of two JsonNode values.static StringtoFileJson(com.fasterxml.jackson.databind.JsonNode node) Converts the given node into JSON suitable for writing into a file such as package.json.static com.fasterxml.jackson.databind.node.ObjectNodewriteValue(Object object) Converts Java object into ObjectNode.
-
Method Details
-
getMapper
public static com.fasterxml.jackson.databind.ObjectMapper getMapper() -
createObjectNode
public static com.fasterxml.jackson.databind.node.ObjectNode createObjectNode()Create a new ObjectNode.- Returns:
- ObjectNode
-
createArrayNode
public static com.fasterxml.jackson.databind.node.ArrayNode createArrayNode()Create a new ArrayNode.- Returns:
- ArrayNode
-
readTree
Read Json string to JsonNode.- Returns:
- JsonNode representation of given json string
-
createNode
Create a JsonNode from value.- Returns:
- JsonNode for given value content
-
jsonEquals
public static boolean jsonEquals(com.fasterxml.jackson.databind.JsonNode a, com.fasterxml.jackson.databind.JsonNode b) Compares two json values for deep equality.This is a helper for overcoming the fact that
ObjectNodedoesn't overrideObject.equals(Object)and { ObjectNode#jsEquals(ObjectNode)} 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:
trueif both json values are the same;falseotherwise
-
stringEqual
public static boolean stringEqual(com.fasterxml.jackson.databind.JsonNode a, com.fasterxml.jackson.databind.JsonNode b) Compare String value of two JsonNode values.- Parameters:
a- Value oneb- Value two- Returns:
trueif text content equals
-
booleanEqual
public static boolean booleanEqual(com.fasterxml.jackson.databind.JsonNode a, com.fasterxml.jackson.databind.JsonNode b) Compare boolean value of two JsonNode values.- Parameters:
a- Value oneb- Value two- Returns:
trueif text boolean equals
-
numbersEqual
public static boolean numbersEqual(com.fasterxml.jackson.databind.JsonNode a, com.fasterxml.jackson.databind.JsonNode b) Compare number value of two JsonNode values.- Parameters:
a- Value oneb- Value two- Returns:
trueif number content equals
-
getKeys
-
stream
public static <T extends com.fasterxml.jackson.databind.JsonNode> Stream<T> stream(com.fasterxml.jackson.databind.node.ArrayNode array) 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
public static Stream<com.fasterxml.jackson.databind.JsonNode> objectStream(com.fasterxml.jackson.databind.node.ArrayNode array) 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<com.fasterxml.jackson.databind.JsonNode,com.fasterxml.jackson.databind.node.ArrayNode, asArray()com.fasterxml.jackson.databind.node.ArrayNode> Creates a collector that collects values into a JSON array.- Returns:
- the collector
-
createArray
public static com.fasterxml.jackson.databind.node.ArrayNode createArray(com.fasterxml.jackson.databind.JsonNode... 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> com.fasterxml.jackson.databind.node.ObjectNode createObject(Map<String, T> map, Function<T, com.fasterxml.jackson.databind.JsonNode> 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
public static <T> T readToObject(com.fasterxml.jackson.databind.node.ObjectNode jsonObject, Class<T> tClass) Converts JsonObject into Java object of given type.- Type Parameters:
T- type of result instance- Parameters:
jsonObject- JSON object to convert, notnulltClass- class of converted object instance- Returns:
- converted object instance
-
readValue
public static <T> T readValue(com.fasterxml.jackson.databind.node.ObjectNode jsonValue, Class<T> tClass) Converts ObjectNode into Java object of given type.- Type Parameters:
T- type of result instance- Parameters:
jsonValue- JSON value to convert, notnulltClass- class of converted object instance- Returns:
- converted object instance
-
readValue
public static <T> T readValue(com.fasterxml.jackson.databind.node.ObjectNode jsonValue, com.fasterxml.jackson.core.type.TypeReference<T> typeReference) Converts ObjectNode into Java object of given type.- Type Parameters:
T- type of result instance- Parameters:
jsonValue- JSON value to convert, notnulltypeReference- type reference of converted object instance- Returns:
- converted object instance
-
writeValue
Converts Java object into ObjectNode.- Parameters:
object- Java object to convert- Returns:
- converted JSON value
-
toFileJson
public static String toFileJson(com.fasterxml.jackson.databind.JsonNode node) throws com.fasterxml.jackson.core.JsonProcessingException Converts the given node into JSON suitable for writing into a file such as package.json.- Parameters:
node- the node to convert- Returns:
- the JSON string
- Throws:
com.fasterxml.jackson.core.JsonProcessingException- if the node cannot be converted
-