Class 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

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Collector<elemental.json.JsonValue,​elemental.json.JsonArray,​elemental.json.JsonArray> asArray()
      Creates a collector that collects values into a JSON array.
      static elemental.json.JsonObject beanToJson​(Object bean)
      Converts the given bean to JSON.
      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 mapToJson​(Map<String,​?> map)
      Converts the given map to JSON.
      static DoubleStream 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 extends elemental.json.JsonValue>
      Stream<T>
      stream​(elemental.json.JsonArray array)
      Creates a stream from a JSON array.
    • Method Detail

      • 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
      • 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