com.vaadin.flow.internal.

Class JsonUtils

java.lang.Object
com.vaadin.flow.internal.JsonUtils
public final class JsonUtils extends Object

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 Type
    Method
    Description
    static Collector<elemental.json.JsonValue,elemental.json.JsonArray,elemental.json.JsonArray>

    Creates a collector that collects values into a JSON array.

    static elemental.json.JsonObject

    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)
     
    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
    readValue(elemental.json.JsonValue jsonValue, Class<T> tClass)

    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

    Converts Java object into JsonValue.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 override Object.equals(Object) and JsonValue.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 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(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

      public static <T extends elemental.json.JsonValue> Stream<T> stream(elemental.json.JsonArray 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<elemental.json.JsonObject> objectStream(elemental.json.JsonArray 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(elemental.json.JsonArray 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<elemental.json.JsonValue,elemental.json.JsonArray,elemental.json.JsonArray> asArray()

      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 object

      itemToJson - callback for converting map values to JSON

      Returns:

      the created object

    • beanToJson

      public static elemental.json.JsonObject 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 elemental.json.JsonArray 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 elemental.json.JsonObject 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(elemental.json.JsonObject 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(elemental.json.JsonValue jsonValue, Class<T> tClass)

      Converts JsonValue 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(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, not null

      typeReference - type reference of converted object instance

      Returns:

      converted object instance

    • writeValue

      public static elemental.json.JsonValue writeValue(Object object)

      Converts Java object into JsonValue.

      Parameters:

      object - Java object to convert

      Returns:

      converted JSON value