com.vaadin.flow.internal.
Class 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.ObjectNode
beanToJson
(Object bean) Converts the given bean to JSON.
static boolean
booleanEqual
(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.ArrayNode
createArray
(com.fasterxml.jackson.databind.JsonNode... values) Creates a new JSON array with the given values.
static com.fasterxml.jackson.databind.node.ArrayNode
Create a new ArrayNode.
static com.fasterxml.jackson.databind.JsonNode
createNode
(Object value) Create a JsonNode from value.
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.
static com.fasterxml.jackson.databind.node.ObjectNode
Create a new ObjectNode.
getKeys
(com.fasterxml.jackson.databind.JsonNode node) static com.fasterxml.jackson.databind.ObjectMapper
static boolean
jsonEquals
(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.ArrayNode
listToJson
(List<?> list) Converts the given list to JSON.
static com.fasterxml.jackson.databind.node.ObjectNode
Converts the given map to JSON.
static boolean
numbersEqual
(com.fasterxml.jackson.databind.JsonNode a, com.fasterxml.jackson.databind.JsonNode b) Compare number value of two JsonNode values.
static DoubleStream
numberStream
(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> T
readToObject
(com.fasterxml.jackson.databind.node.ObjectNode jsonObject, Class<T> tClass) Converts JsonObject into Java object of given type.
static com.fasterxml.jackson.databind.node.ObjectNode
Read Json string to JsonNode.
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.
static <T> T
Converts 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 boolean
stringEqual
(com.fasterxml.jackson.databind.JsonNode a, com.fasterxml.jackson.databind.JsonNode b) Compare String value of two JsonNode values.
static String
toFileJson
(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.ObjectNode
writeValue
(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
ObjectNode
doesn'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 nullReturns:
true
if both json values are the same;false
otherwise -
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 twoReturns:
true
if 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 twoReturns:
true
if 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 twoReturns:
true
if 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 typeParameters:
array
- the JSON array to create a stream fromReturns:
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 fromReturns:
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 fromReturns:
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 arrayReturns:
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 valuesParameters:
map
- the map to convert into a JSON objectitemToJson
- callback for converting map values to JSONReturns:
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 instanceParameters:
jsonObject
- JSON object to convert, notnull
tClass
- class of converted object instanceReturns:
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 instanceParameters:
jsonValue
- JSON value to convert, notnull
tClass
- class of converted object instanceReturns:
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 instanceParameters:
jsonValue
- JSON value to convert, notnull
typeReference
- type reference of converted object instanceReturns:
converted object instance
-
writeValue
Converts Java object into ObjectNode.
Parameters:
object
- Java object to convertReturns:
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 convertReturns:
the JSON string
Throws:
com.fasterxml.jackson.core.JsonProcessingException
- if the node cannot be converted
-