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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classCustom Jackson serializer for Component that delegates to NodeSerializer.static classCustom Jackson serializer for Node types (Element, ShadowRoot) that serializes attached nodes as @v-node references for client-side DOM manipulation. -
Method Summary
Modifier and TypeMethodDescriptionstatic Collector<tools.jackson.databind.JsonNode, tools.jackson.databind.node.ArrayNode, tools.jackson.databind.node.ArrayNode> asArray()Creates a collector that collects values into a JSON array.static tools.jackson.databind.node.ObjectNodebeanToJson(Object bean) Converts the given bean to JSON.static booleanbooleanEqual(tools.jackson.databind.JsonNode a, tools.jackson.databind.JsonNode b) Compare boolean value of two JsonNode values.static voidChecks that the Jackson version on the classpath is compatible with this version of Vaadin by verifying that expected API methods are available.static tools.jackson.databind.node.ArrayNodecreateArray(tools.jackson.databind.JsonNode... values) Creates a new JSON array with the given values.static tools.jackson.databind.node.ArrayNodeCreate a new ArrayNode.static tools.jackson.databind.JsonNodecreateNode(Object value) Create a JsonNode from value.static <T> tools.jackson.databind.node.ObjectNodecreateObject(Map<String, T> map, Function<T, tools.jackson.databind.JsonNode> itemToJson) Converts the given map into a JSON object by converting each map value to a JSON value.static tools.jackson.databind.node.ObjectNodeCreate a new ObjectNode.getKeys(tools.jackson.databind.JsonNode node) static tools.jackson.databind.ObjectMapperstatic @Nullable tools.jackson.databind.JsonNodegetNestedKey(tools.jackson.databind.node.ObjectNode objectNode, List<String> keyPath) Retrieves a nested JSON key from an object node based on a provided path.static booleanjsonEquals(tools.jackson.databind.JsonNode a, tools.jackson.databind.JsonNode b) Compares two json values for deep equality.static tools.jackson.databind.node.ArrayNodelistToJson(List<?> list) Converts the given list to JSON.static tools.jackson.databind.node.ObjectNodeConverts the given map to JSON.static tools.jackson.databind.node.ValueNodenullNode()Create a nullNode for null value.static booleannumbersEqual(tools.jackson.databind.JsonNode a, tools.jackson.databind.JsonNode b) Compare number value of two JsonNode values.static DoubleStreamnumberStream(tools.jackson.databind.node.ArrayNode array) Creates a double stream from a JSON array of numbers.static Stream<tools.jackson.databind.JsonNode> objectStream(tools.jackson.databind.node.ArrayNode array) Creates a stream from a JSON array of objects.static <T> TreadToObject(tools.jackson.databind.JsonNode jsonObject, Class<T> tClass) Converts JsonObject into Java object of given type.static tools.jackson.databind.node.ObjectNodeRead Json string to JsonNode.static <T> TConverts ObjectNode into Java object of given type.static <T> TreadValue(tools.jackson.databind.JsonNode jsonValue, tools.jackson.core.type.TypeReference<T> typeReference) Converts ObjectNode into Java object of given type.static voidremoveNestedKey(tools.jackson.databind.node.ObjectNode objectNode, List<String> keyPath) Removes a nested key from anObjectNodebased on the provided path.static voidsetNestedKey(tools.jackson.databind.node.ObjectNode objectNode, List<String> keyPath, tools.jackson.databind.JsonNode valueNode, Function<? super tools.jackson.databind.JsonNode, ? extends tools.jackson.databind.node.ObjectNode> plainValueConverter) Sets a nested JSON key path in anObjectNodewith the provided value.static <T extends tools.jackson.databind.JsonNode>
Stream<T> stream(tools.jackson.databind.node.ArrayNode array) Creates a stream from a JSON array.static booleanstringEqual(tools.jackson.databind.JsonNode a, tools.jackson.databind.JsonNode b) Compare String value of two JsonNode values.static StringtoFileJson(tools.jackson.databind.JsonNode node) Converts the given node into JSON suitable for writing into a file such as package.json.static tools.jackson.databind.node.BaseJsonNodewriteValue(Object object) Converts Java object into ObjectNode.
-
Method Details
-
getMapper
public static tools.jackson.databind.ObjectMapper getMapper() -
checkJacksonCompatibility
public static void checkJacksonCompatibility()Checks that the Jackson version on the classpath is compatible with this version of Vaadin by verifying that expected API methods are available.- Throws:
IllegalStateException- if the Jackson version is not compatible
-
createObjectNode
public static tools.jackson.databind.node.ObjectNode createObjectNode()Create a new ObjectNode.- Returns:
- ObjectNode
-
createArrayNode
public static tools.jackson.databind.node.ArrayNode createArrayNode()Create a new ArrayNode.- Returns:
- ArrayNode
-
nullNode
public static tools.jackson.databind.node.ValueNode nullNode()Create a nullNode for null value.- Returns:
- NullNode
-
readTree
Read Json string to JsonNode.- Parameters:
json- the JSON string to parse- Returns:
- JsonNode representation of given json string
-
createNode
Create a JsonNode from value.- Parameters:
value- the value to convert to a JsonNode- Returns:
- JsonNode for given value content
-
jsonEquals
public static boolean jsonEquals(tools.jackson.databind.JsonNode a, tools.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(tools.jackson.databind.JsonNode a, tools.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(tools.jackson.databind.JsonNode a, tools.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(tools.jackson.databind.JsonNode a, tools.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 tools.jackson.databind.JsonNode> Stream<T> stream(tools.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<tools.jackson.databind.JsonNode> objectStream(tools.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<tools.jackson.databind.JsonNode,tools.jackson.databind.node.ArrayNode, asArray()tools.jackson.databind.node.ArrayNode> Creates a collector that collects values into a JSON array.- Returns:
- the collector
-
createArray
public static tools.jackson.databind.node.ArrayNode createArray(tools.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> tools.jackson.databind.node.ObjectNode createObject(Map<String, T> map, Function<T, tools.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
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
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(tools.jackson.databind.JsonNode jsonValue, tools.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(tools.jackson.databind.JsonNode node) throws tools.jackson.core.JacksonException 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:
tools.jackson.core.JacksonException- if the node cannot be converted
-
getNestedKey
public static @Nullable tools.jackson.databind.JsonNode getNestedKey(tools.jackson.databind.node.ObjectNode objectNode, List<String> keyPath) Retrieves a nested JSON key from an object node based on a provided path.- Parameters:
objectNode- the root object node to search withinkeyPath- the nested key path- Returns:
- the value of the last key found, or
null
-
setNestedKey
public static void setNestedKey(tools.jackson.databind.node.ObjectNode objectNode, List<String> keyPath, tools.jackson.databind.JsonNode valueNode, Function<? super tools.jackson.databind.JsonNode, ? extends tools.jackson.databind.node.ObjectNode> plainValueConverter) Sets a nested JSON key path in anObjectNodewith the provided value.This method handles both direct and nested key assignments. If the current node at the specified top-level key does not exist not an
ObjectNode, it converts it to one using the provided converter function, before proceeding with the remaining keys in the path.- Parameters:
objectNode- the root object where the key path will be setkeyPath- the nested key pathvalueNode- the value to be assignedplainValueConverter- a function that converts intermediatenulland non-ObjectNodevalues intoObjectNodes when necessary for recursive traversal
-
removeNestedKey
public static void removeNestedKey(tools.jackson.databind.node.ObjectNode objectNode, List<String> keyPath) Removes a nested key from anObjectNodebased on the provided path. Also removes nested objects, if they become empty after key removal.- Parameters:
objectNode- the root object containing to processkeyPath- the nested key path
-