Class JacksonUtils

java.lang.Object
com.vaadin.flow.internal.JacksonUtils

public final class JacksonUtils extends Object
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 Type
    Method
    Description
    static Collector<com.fasterxml.jackson.databind.JsonNode,com.fasterxml.jackson.databind.node.ArrayNode,com.fasterxml.jackson.databind.node.ArrayNode>
    Creates a collector that collects values into a JSON array.
    static com.fasterxml.jackson.databind.node.ObjectNode
    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
    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.
    static List<String>
    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.
    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
    readValue(com.fasterxml.jackson.databind.node.ObjectNode jsonValue, Class<T> tClass)
    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
    Converts Java object into ObjectNode.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public static com.fasterxml.jackson.databind.node.ObjectNode readTree(String json)
      Read Json string to JsonNode.
      Returns:
      JsonNode representation of given json string
    • createNode

      public static com.fasterxml.jackson.databind.JsonNode createNode(Object value)
      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 override Object.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 null
      b - 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(com.fasterxml.jackson.databind.JsonNode a, com.fasterxml.jackson.databind.JsonNode b)
      Compare String value of two JsonNode values.
      Parameters:
      a - Value one
      b - Value two
      Returns:
      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 one
      b - Value two
      Returns:
      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 one
      b - Value two
      Returns:
      true if number content equals
    • getKeys

      public static List<String> getKeys(com.fasterxml.jackson.databind.JsonNode node)
    • 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

      public static DoubleStream numberStream(com.fasterxml.jackson.databind.node.ArrayNode array)
      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,com.fasterxml.jackson.databind.node.ArrayNode> asArray()
      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 object
      itemToJson - callback for converting map values to JSON
      Returns:
      the created object
    • beanToJson

      public static com.fasterxml.jackson.databind.node.ObjectNode beanToJson(Object bean)
      Converts the given bean to JSON.
      Parameters:
      bean - the bean to convert, not null
      Returns:
      a JSON representation of the bean
    • listToJson

      public static com.fasterxml.jackson.databind.node.ArrayNode listToJson(List<?> list)
      Converts the given list to JSON.
      Parameters:
      list - the list to convert, not null
      Returns:
      a JSON representation of the bean
    • mapToJson

      public static com.fasterxml.jackson.databind.node.ObjectNode mapToJson(Map<String,?> map)
      Converts the given map to JSON.
      Parameters:
      map - the map to convert, not null
      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, not null
      tClass - 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, not null
      tClass - 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, not null
      typeReference - type reference of converted object instance
      Returns:
      converted object instance
    • writeValue

      public static com.fasterxml.jackson.databind.node.ObjectNode writeValue(Object object)
      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