Class JsonUtils


  • public class JsonUtils
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Constructor Description
      JsonUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static JsonUtils.JsonArrayBuilder buildJsonArray()
      Creates a JsonArray using a Builder.
      static JsonUtils.JsonArrayBuilder buildJsonArray​(com.google.gson.JsonArray j)
      Creates a JsonArray using a Builder.
      static JsonUtils.JsonObjectBuilder buildJsonObject()
      Creates a JsonObject using a Builder.
      static JsonUtils.JsonObjectBuilder buildJsonObject​(com.google.gson.JsonObject j)
      Creates a JsonObject using a Builder.
      static java.lang.Object getAsBestType​(com.google.gson.JsonElement j)
      Tries to find the best matching Object representation of the given JsonElement.
      static boolean getAsBoolean​(com.google.gson.JsonElement jElement)
      Gets the JsonElement as Boolean.
      static boolean getAsBoolean​(com.google.gson.JsonElement jElement, java.lang.String memberName)
      Gets the member of the JsonElement as Boolean.
      static double getAsDouble​(com.google.gson.JsonElement jElement)
      Gets the JsonElement as Double.
      static double getAsDouble​(com.google.gson.JsonElement jElement, java.lang.String memberName)
      Gets the member of the JsonElement as Double.
      static <E extends java.lang.Enum<E>>
      E
      getAsEnum​(java.lang.Class<E> enumType, com.google.gson.JsonElement jElement)
      Gets the JsonElement as Enum.
      static <E extends java.lang.Enum<E>>
      E
      getAsEnum​(java.lang.Class<E> enumType, com.google.gson.JsonElement jElement, java.lang.String memberName)
      Gets the member of the JsonElement as Enum.
      static float getAsFloat​(com.google.gson.JsonElement jElement)
      Gets the JsonElement as Float.
      static float getAsFloat​(com.google.gson.JsonElement jElement, java.lang.String memberName)
      Gets the member of the JsonElement as Float.
      static java.net.Inet4Address getAsInet4Address​(com.google.gson.JsonElement jElement)
      Gets the JsonElement as Inet4Address.
      static int getAsInt​(com.google.gson.JsonArray jArray, int index)
      Gets the member with given index of the JsonArray as int.
      static int getAsInt​(com.google.gson.JsonElement jElement)
      Gets the JsonElement as int.
      static int getAsInt​(com.google.gson.JsonElement jElement, java.lang.String memberName)
      Gets the member of the JsonElement as int.
      static com.google.gson.JsonArray getAsJsonArray​(com.google.gson.JsonElement jElement)
      Gets the JsonElement as JsonArray.
      static com.google.gson.JsonArray getAsJsonArray​(com.google.gson.JsonElement jElement, java.lang.String memberName)
      Gets the member of the JsonElement as JsonArray.
      static com.google.gson.JsonElement getAsJsonElement​(java.lang.Object value)
      Gets a JsonElement representing the Object value.
      static com.google.gson.JsonObject getAsJsonObject​(com.google.gson.JsonElement jElement)
      Gets the JsonElement as JsonObject.
      static com.google.gson.JsonObject getAsJsonObject​(com.google.gson.JsonElement jElement, java.lang.String memberName)
      Gets the member of the JsonElement as JsonObject.
      static long getAsLong​(com.google.gson.JsonElement jElement)
      Gets the JsonElement as long.
      static long getAsLong​(com.google.gson.JsonElement jElement, java.lang.String memberName)
      Gets the member of the JsonElement as long.
      static java.util.Optional<java.lang.Boolean> getAsOptionalBoolean​(com.google.gson.JsonElement jElement, java.lang.String memberName)
      Gets the member of the JsonElement as an Optional Boolean.
      static <E extends java.lang.Enum<E>>
      java.util.Optional<E>
      getAsOptionalEnum​(java.lang.Class<E> enumType, com.google.gson.JsonElement jElement, java.lang.String memberName)
      Gets the member of the JsonElement as Optional Enum.
      static java.util.Optional<java.net.Inet4Address> getAsOptionalInet4Address​(com.google.gson.JsonElement jElement, java.lang.String memberName)
      Gets the member of the JsonElement as Optional Inet4Address.
      static java.util.Optional<java.lang.Integer> getAsOptionalInt​(com.google.gson.JsonElement jElement)
      Gets the JsonElement as Optional Integer.
      static java.util.Optional<java.lang.Integer> getAsOptionalInt​(com.google.gson.JsonElement jElement, java.lang.String memberName)
      Gets the member of the JsonElement as Optional Integer.
      static java.util.Optional<com.google.gson.JsonArray> getAsOptionalJsonArray​(com.google.gson.JsonElement jElement, java.lang.String memberName)
      Gets the member of the JsonElement as Optional JsonArray.
      static java.util.Optional<com.google.gson.JsonObject> getAsOptionalJsonObject​(com.google.gson.JsonElement jElement)
      Gets the JsonElement as Optional JsonObject.
      static java.util.Optional<com.google.gson.JsonObject> getAsOptionalJsonObject​(com.google.gson.JsonElement jElement, java.lang.String memberName)
      Gets the member of the JsonElement as Optional JsonObject.
      static java.util.Optional<java.lang.Long> getAsOptionalLong​(com.google.gson.JsonElement jElement, java.lang.String memberName)
      Gets the member of the JsonElement as Optional Long.
      static java.util.Optional<java.lang.String> getAsOptionalString​(com.google.gson.JsonElement jElement)
      Gets the JsonElement as Optional String.
      static java.util.Optional<java.lang.String> getAsOptionalString​(com.google.gson.JsonElement jElement, java.lang.String memberName)
      Gets the member of the JsonElement as Optional String.
      static java.util.Optional<java.util.UUID> getAsOptionalUUID​(com.google.gson.JsonElement jElement, java.lang.String memberName)
      Gets the member of the JsonElement as Optional UUID.
      static com.google.gson.JsonPrimitive getAsPrimitive​(com.google.gson.JsonElement jElement)
      Gets the JsonElement as JsonPrimitive.
      static com.google.gson.JsonPrimitive getAsPrimitive​(com.google.gson.JsonElement jElement, java.lang.String memberName)
      Gets the member of the JsonElement as JsonPrimitive.
      static short getAsShort​(com.google.gson.JsonElement jElement)
      Gets the JsonElement as short.
      static short getAsShort​(com.google.gson.JsonElement jElement, java.lang.String memberName)
      Gets the member of the JsonElement as short.
      static java.lang.String getAsString​(com.google.gson.JsonElement jElement)
      Gets the JsonElement as String.
      static java.lang.String getAsString​(com.google.gson.JsonElement jElement, java.lang.String memberName)
      Gets the member of the JsonElement as String.
      static java.lang.String[] getAsStringArray​(com.google.gson.JsonArray json)
      Converts a JsonArray to a String Array.
      static java.lang.Object getAsType​(OpenemsType type, com.google.gson.JsonElement j)
      Gets a JsonElement as the given OpenemsType.
      static java.lang.Object getAsType​(java.lang.Class<?> type, com.google.gson.JsonElement j)
      Gets a JsonElement as the given type.
      static java.lang.Object getAsType​(java.util.Optional<java.lang.Class<?>> typeOptional, com.google.gson.JsonElement j)
      Gets a JsonElement as the given type.
      static java.util.UUID getAsUUID​(com.google.gson.JsonElement jElement, java.lang.String memberName)
      Gets the member of the JsonElement as UUID.
      static java.time.ZonedDateTime getAsZonedDateTime​(com.google.gson.JsonElement element, java.lang.String memberName, java.time.ZoneId timezone)
      Takes a JSON in the form 'YYYY-MM-DD' and converts it to a ZonedDateTime with hour, minute and second set to zero.
      static java.util.Optional<com.google.gson.JsonElement> getOptionalSubElement​(com.google.gson.JsonElement jElement, java.lang.String memberName)
      Gets the member of the JsonElement as Optional JsonElement.
      static com.google.gson.JsonElement getSubElement​(com.google.gson.JsonElement jElement, java.lang.String memberName)
      Gets the member of the JsonElement as JsonElement.
      static boolean isEmptyJsonArray​(com.google.gson.JsonElement j)
      Check if the given JsonElement is an empty JsonArray [].
      static boolean isEmptyJsonObject​(com.google.gson.JsonElement j)
      Check if the given JsonElement is an empty JsonObject {}.
      static com.google.gson.JsonElement parse​(java.lang.String string)
      Parses a string to a JsonElement.
      static com.google.gson.JsonObject parseToJsonObject​(java.lang.String string)
      Parses a string to a JsonObject.
      static void prettyPrint​(com.google.gson.JsonElement j)
      Pretty print a JsonElement.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • JsonUtils

        public JsonUtils()
    • Method Detail

      • buildJsonArray

        public static JsonUtils.JsonArrayBuilder buildJsonArray()
        Creates a JsonArray using a Builder.
        Returns:
        the Builder
      • buildJsonArray

        public static JsonUtils.JsonArrayBuilder buildJsonArray​(com.google.gson.JsonArray j)
        Creates a JsonArray using a Builder. Initialized from an existing JsonArray.
        Parameters:
        j - the initial JsonArray
        Returns:
        the Builder
      • buildJsonObject

        public static JsonUtils.JsonObjectBuilder buildJsonObject()
        Creates a JsonObject using a Builder.
        Returns:
        the Builder
      • buildJsonObject

        public static JsonUtils.JsonObjectBuilder buildJsonObject​(com.google.gson.JsonObject j)
        Creates a JsonObject using a Builder. Initialized from an existing JsonObject.
        Parameters:
        j - the initial JsonObject
        Returns:
        the Builder
      • getAsPrimitive

        public static com.google.gson.JsonPrimitive getAsPrimitive​(com.google.gson.JsonElement jElement,
                                                                   java.lang.String memberName)
                                                            throws OpenemsError.OpenemsNamedException
        Gets the member of the JsonElement as JsonPrimitive.
        Parameters:
        jElement - the JsonElement
        memberName - the name of the member
        Returns:
        the JsonPrimitive value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getOptionalSubElement

        public static java.util.Optional<com.google.gson.JsonElement> getOptionalSubElement​(com.google.gson.JsonElement jElement,
                                                                                            java.lang.String memberName)
        Gets the member of the JsonElement as Optional JsonElement.
        Parameters:
        jElement - the JsonElement
        memberName - the name of the member
        Returns:
        the Optional JsonElement value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getSubElement

        public static com.google.gson.JsonElement getSubElement​(com.google.gson.JsonElement jElement,
                                                                java.lang.String memberName)
                                                         throws OpenemsError.OpenemsNamedException
        Gets the member of the JsonElement as JsonElement.
        Parameters:
        jElement - the JsonElement
        memberName - the name of the member
        Returns:
        the JsonElement value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsJsonObject

        public static com.google.gson.JsonObject getAsJsonObject​(com.google.gson.JsonElement jElement,
                                                                 java.lang.String memberName)
                                                          throws OpenemsError.OpenemsNamedException
        Gets the member of the JsonElement as JsonObject.
        Parameters:
        jElement - the JsonElement
        memberName - the name of the member
        Returns:
        the JsonObject value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsOptionalJsonObject

        public static java.util.Optional<com.google.gson.JsonObject> getAsOptionalJsonObject​(com.google.gson.JsonElement jElement)
        Gets the JsonElement as Optional JsonObject.
        Parameters:
        jElement - the JsonElement
        Returns:
        the Optional JsonObject value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsOptionalJsonObject

        public static java.util.Optional<com.google.gson.JsonObject> getAsOptionalJsonObject​(com.google.gson.JsonElement jElement,
                                                                                             java.lang.String memberName)
        Gets the member of the JsonElement as Optional JsonObject.
        Parameters:
        jElement - the JsonElement
        memberName - the name of the member
        Returns:
        the Optional JsonObject value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsJsonArray

        public static com.google.gson.JsonArray getAsJsonArray​(com.google.gson.JsonElement jElement,
                                                               java.lang.String memberName)
                                                        throws OpenemsError.OpenemsNamedException
        Gets the member of the JsonElement as JsonArray.
        Parameters:
        jElement - the JsonElement
        memberName - the name of the member
        Returns:
        the JsonArray value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsOptionalJsonArray

        public static java.util.Optional<com.google.gson.JsonArray> getAsOptionalJsonArray​(com.google.gson.JsonElement jElement,
                                                                                           java.lang.String memberName)
        Gets the member of the JsonElement as Optional JsonArray.
        Parameters:
        jElement - the JsonElement
        memberName - the name of the member
        Returns:
        the Optional JsonArray value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsString

        public static java.lang.String getAsString​(com.google.gson.JsonElement jElement,
                                                   java.lang.String memberName)
                                            throws OpenemsError.OpenemsNamedException
        Gets the member of the JsonElement as String.
        Parameters:
        jElement - the JsonElement
        memberName - the name of the member
        Returns:
        the String value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsOptionalString

        public static java.util.Optional<java.lang.String> getAsOptionalString​(com.google.gson.JsonElement jElement)
        Gets the JsonElement as Optional String.
        Parameters:
        jElement - the JsonElement
        Returns:
        the Optional String value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsOptionalString

        public static java.util.Optional<java.lang.String> getAsOptionalString​(com.google.gson.JsonElement jElement,
                                                                               java.lang.String memberName)
        Gets the member of the JsonElement as Optional String.
        Parameters:
        jElement - the JsonElement
        memberName - the name of the member
        Returns:
        the Optional String value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsBoolean

        public static boolean getAsBoolean​(com.google.gson.JsonElement jElement,
                                           java.lang.String memberName)
                                    throws OpenemsError.OpenemsNamedException
        Gets the member of the JsonElement as Boolean.
        Parameters:
        jElement - the JsonElement
        memberName - the name of the member
        Returns:
        the Boolean value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsOptionalBoolean

        public static java.util.Optional<java.lang.Boolean> getAsOptionalBoolean​(com.google.gson.JsonElement jElement,
                                                                                 java.lang.String memberName)
        Gets the member of the JsonElement as an Optional Boolean.
        Parameters:
        jElement - the JsonElement
        memberName - the name of the member
        Returns:
        the Optional Boolean value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsShort

        public static short getAsShort​(com.google.gson.JsonElement jElement,
                                       java.lang.String memberName)
                                throws OpenemsError.OpenemsNamedException
        Gets the member of the JsonElement as short.
        Parameters:
        jElement - the JsonElement
        memberName - the name of the member
        Returns:
        the short value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsInt

        public static int getAsInt​(com.google.gson.JsonElement jElement,
                                   java.lang.String memberName)
                            throws OpenemsError.OpenemsNamedException
        Gets the member of the JsonElement as int.
        Parameters:
        jElement - the JsonElement
        memberName - the name of the member
        Returns:
        the int value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsOptionalInt

        public static java.util.Optional<java.lang.Integer> getAsOptionalInt​(com.google.gson.JsonElement jElement)
        Gets the JsonElement as Optional Integer.
        Parameters:
        jElement - the JsonElement
        Returns:
        the Optional Integer value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsOptionalInt

        public static java.util.Optional<java.lang.Integer> getAsOptionalInt​(com.google.gson.JsonElement jElement,
                                                                             java.lang.String memberName)
        Gets the member of the JsonElement as Optional Integer.
        Parameters:
        jElement - the JsonElement
        memberName - the name of the member
        Returns:
        the Optional Integer value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsLong

        public static long getAsLong​(com.google.gson.JsonElement jElement,
                                     java.lang.String memberName)
                              throws OpenemsError.OpenemsNamedException
        Gets the member of the JsonElement as long.
        Parameters:
        jElement - the JsonElement
        memberName - the name of the member
        Returns:
        the long value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsOptionalLong

        public static java.util.Optional<java.lang.Long> getAsOptionalLong​(com.google.gson.JsonElement jElement,
                                                                           java.lang.String memberName)
        Gets the member of the JsonElement as Optional Long.
        Parameters:
        jElement - the JsonElement
        memberName - the name of the member
        Returns:
        the Optional Long value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsFloat

        public static float getAsFloat​(com.google.gson.JsonElement jElement,
                                       java.lang.String memberName)
                                throws OpenemsError.OpenemsNamedException
        Gets the member of the JsonElement as Float.
        Parameters:
        jElement - the JsonElement
        memberName - the name of the member
        Returns:
        the Float value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsDouble

        public static double getAsDouble​(com.google.gson.JsonElement jElement,
                                         java.lang.String memberName)
                                  throws OpenemsError.OpenemsNamedException
        Gets the member of the JsonElement as Double.
        Parameters:
        jElement - the JsonElement
        memberName - the name of the member
        Returns:
        the Double value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsEnum

        public static <E extends java.lang.Enum<E>> E getAsEnum​(java.lang.Class<E> enumType,
                                                                com.google.gson.JsonElement jElement)
                                                         throws OpenemsError.OpenemsNamedException
        Gets the JsonElement as Enum.
        Type Parameters:
        E - the Enum type
        Parameters:
        enumType - the class of the Enum
        jElement - the JsonElement
        Returns:
        the Enum value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsEnum

        public static <E extends java.lang.Enum<E>> E getAsEnum​(java.lang.Class<E> enumType,
                                                                com.google.gson.JsonElement jElement,
                                                                java.lang.String memberName)
                                                         throws OpenemsError.OpenemsNamedException
        Gets the member of the JsonElement as Enum.
        Type Parameters:
        E - the Enum type
        Parameters:
        enumType - the class of the Enum
        jElement - the JsonElement
        memberName - the name of the member
        Returns:
        the Enum value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsOptionalEnum

        public static <E extends java.lang.Enum<E>> java.util.Optional<E> getAsOptionalEnum​(java.lang.Class<E> enumType,
                                                                                            com.google.gson.JsonElement jElement,
                                                                                            java.lang.String memberName)
        Gets the member of the JsonElement as Optional Enum.
        Type Parameters:
        E - the Enum type
        Parameters:
        enumType - the class of the Enum
        jElement - the JsonElement
        memberName - the name of the member
        Returns:
        the Optional Enum value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsOptionalInet4Address

        public static java.util.Optional<java.net.Inet4Address> getAsOptionalInet4Address​(com.google.gson.JsonElement jElement,
                                                                                          java.lang.String memberName)
        Gets the member of the JsonElement as Optional Inet4Address.
        Parameters:
        jElement - the JsonElement
        memberName - the name of the member
        Returns:
        the Optional Inet4Address value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsUUID

        public static java.util.UUID getAsUUID​(com.google.gson.JsonElement jElement,
                                               java.lang.String memberName)
                                        throws OpenemsError.OpenemsNamedException
        Gets the member of the JsonElement as UUID.
        Parameters:
        jElement - the JsonElement
        memberName - the name of the member
        Returns:
        the UUID value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsOptionalUUID

        public static java.util.Optional<java.util.UUID> getAsOptionalUUID​(com.google.gson.JsonElement jElement,
                                                                           java.lang.String memberName)
        Gets the member of the JsonElement as Optional UUID.
        Parameters:
        jElement - the JsonElement
        memberName - the name of the member
        Returns:
        the Optional UUID value
        Throws:
        OpenemsError.OpenemsNamedException - on error
      • getAsJsonElement

        public static com.google.gson.JsonElement getAsJsonElement​(java.lang.Object value)
        Gets a JsonElement representing the Object value.
        Parameters:
        value - the Object value
        Returns:
        the JsonElement
      • getAsType

        public static java.lang.Object getAsType​(java.lang.Class<?> type,
                                                 com.google.gson.JsonElement j)
                                          throws NotImplementedException
        Gets a JsonElement as the given type.
        Parameters:
        type - the class of the type
        j - the JsonElement
        Returns:
        an Object of the given type
        Throws:
        NotImplementedException
      • getAsType

        public static java.lang.Object getAsType​(java.util.Optional<java.lang.Class<?>> typeOptional,
                                                 com.google.gson.JsonElement j)
                                          throws NotImplementedException
        Gets a JsonElement as the given type.
        Parameters:
        typeOptional - the class of the type
        j - the JsonElement
        Returns:
        an Object of the given type
        Throws:
        NotImplementedException
      • getAsZonedDateTime

        public static java.time.ZonedDateTime getAsZonedDateTime​(com.google.gson.JsonElement element,
                                                                 java.lang.String memberName,
                                                                 java.time.ZoneId timezone)
                                                          throws OpenemsError.OpenemsNamedException
        Takes a JSON in the form 'YYYY-MM-DD' and converts it to a ZonedDateTime with hour, minute and second set to zero.
        Parameters:
        element - the JsonElement
        memberName - the name of the member of the JsonObject
        timezone - the timezone as ZoneId
        Returns:
        the ZonedDateTime
        Throws:
        OpenemsError.OpenemsNamedException - on parse error
      • prettyPrint

        public static void prettyPrint​(com.google.gson.JsonElement j)
        Pretty print a JsonElement.
        Parameters:
        j - the JsonElement
      • isEmptyJsonObject

        public static boolean isEmptyJsonObject​(com.google.gson.JsonElement j)
        Check if the given JsonElement is an empty JsonObject {}.
        Parameters:
        j - the JsonElement to check
        Returns:
        true if is empty, otherwise false
      • isEmptyJsonArray

        public static boolean isEmptyJsonArray​(com.google.gson.JsonElement j)
        Check if the given JsonElement is an empty JsonArray [].
        Parameters:
        j - the JsonElement to check
        Returns:
        true if is empty, otherwise false