Package io.openems.common.utils
Class JsonUtils
java.lang.Object
io.openems.common.utils.JsonUtils
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
A temporary builder class for JsonArrays.static class
static class
A temporary builder class for JsonObjects. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic JsonUtils.JsonArrayBuilder
Creates a JsonArray using a Builder.static JsonUtils.JsonArrayBuilder
buildJsonArray
(com.google.gson.JsonArray j) Creates a JsonArray using a Builder.static JsonUtils.JsonObjectBuilder
Creates a JsonObject using a Builder.static JsonUtils.JsonObjectBuilder
buildJsonObject
(com.google.gson.JsonObject j) Creates a JsonObject using a Builder.static <T extends com.google.gson.JsonElement>
com.google.gson.JsonArraygenerateJsonArray
(Collection<T> list) Provide a easy way to generate a JsonArray from a collection of JsonElements.static <T> com.google.gson.JsonArray
generateJsonArray
(Collection<T> list, com.google.common.base.Function<T, com.google.gson.JsonElement> convert) Provide a easy way to generate a JsonArray from a list using the given convert function to add each element.static Object
getAsBestType
(com.google.gson.JsonElement j) Tries to find the best matching Object representation of the givenJsonElement
.static boolean
getAsBoolean
(com.google.gson.JsonElement jElement) Gets theJsonElement
asBoolean
.static boolean
getAsBoolean
(com.google.gson.JsonElement jElement, String memberName) Gets the member of theJsonElement
asBoolean
.static double
getAsDouble
(com.google.gson.JsonElement jElement) Gets theJsonElement
asDouble
.static double
getAsDouble
(com.google.gson.JsonElement jElement, String memberName) Gets the member of theJsonElement
asDouble
.static <E extends Enum<E>>
EGets theJsonElement
asEnum
.static <E extends Enum<E>>
EGets the member of theJsonElement
asEnum
.static float
getAsFloat
(com.google.gson.JsonElement jElement) Gets theJsonElement
asFloat
.static float
getAsFloat
(com.google.gson.JsonElement jElement, String memberName) Gets the member of theJsonElement
asFloat
.static Inet4Address
getAsInet4Address
(com.google.gson.JsonElement jElement) Gets theJsonElement
asInet4Address
.static Inet4Address
getAsInet4Address
(com.google.gson.JsonElement jElement, String memberName) Gets the member of theJsonElement
asInet4Address
.static int
getAsInt
(com.google.gson.JsonArray jArray, int index) Gets the member with given index of theJsonArray
as int.static int
getAsInt
(com.google.gson.JsonElement jElement) Gets theJsonElement
as int.static int
Gets the member of theJsonElement
as int.static com.google.gson.JsonArray
getAsJsonArray
(com.google.gson.JsonElement jElement) Gets theJsonElement
asJsonArray
.static com.google.gson.JsonArray
getAsJsonArray
(com.google.gson.JsonElement jElement, String memberName) Gets the member of theJsonElement
asJsonArray
.static com.google.gson.JsonElement
getAsJsonElement
(Object value) Gets aJsonElement
representing the Object value.static com.google.gson.JsonObject
getAsJsonObject
(com.google.gson.JsonElement jElement) Gets theJsonElement
asJsonObject
.static com.google.gson.JsonObject
getAsJsonObject
(com.google.gson.JsonElement jElement, String memberName) Gets the member of theJsonElement
asJsonObject
.static long
getAsLong
(com.google.gson.JsonElement jElement) Gets theJsonElement
as long.static long
Gets the member of theJsonElement
as long.getAsOptionalBoolean
(com.google.gson.JsonElement jElement) getAsOptionalBoolean
(com.google.gson.JsonElement jElement, String memberName) getAsOptionalDouble
(com.google.gson.JsonElement jElement) getAsOptionalDouble
(com.google.gson.JsonElement jElement, String memberName) getAsOptionalEnum
(Class<E> enumType, com.google.gson.JsonElement jElement) getAsOptionalEnum
(Class<E> enumType, com.google.gson.JsonElement jElement, String memberName) getAsOptionalFloat
(com.google.gson.JsonElement jElement) getAsOptionalFloat
(com.google.gson.JsonElement jElement, String memberName) static Optional
<Inet4Address> getAsOptionalInet4Address
(com.google.gson.JsonElement jElement) static Optional
<Inet4Address> getAsOptionalInet4Address
(com.google.gson.JsonElement jElement, String memberName) getAsOptionalInt
(com.google.gson.JsonElement jElement) getAsOptionalInt
(com.google.gson.JsonElement jElement, String memberName) static Optional
<com.google.gson.JsonArray> getAsOptionalJsonArray
(com.google.gson.JsonElement jElement) static Optional
<com.google.gson.JsonArray> getAsOptionalJsonArray
(com.google.gson.JsonElement jElement, String memberName) static Optional
<com.google.gson.JsonObject> getAsOptionalJsonObject
(com.google.gson.JsonElement jElement) static Optional
<com.google.gson.JsonObject> getAsOptionalJsonObject
(com.google.gson.JsonElement jElement, String memberName) getAsOptionalLong
(com.google.gson.JsonElement jElement) getAsOptionalLong
(com.google.gson.JsonElement jElement, String memberName) static Optional
<com.google.gson.JsonPrimitive> getAsOptionalPrimitive
(com.google.gson.JsonElement jElement, String memberName) getAsOptionalShort
(com.google.gson.JsonElement jElement) getAsOptionalShort
(com.google.gson.JsonElement jElement, String memberName) getAsOptionalString
(com.google.gson.JsonElement jElement) getAsOptionalString
(com.google.gson.JsonElement jElement, String memberName) getAsOptionalUUID
(com.google.gson.JsonElement jElement) getAsOptionalUUID
(com.google.gson.JsonElement jElement, String memberName) static Optional
<ZonedDateTime> getAsOptionalZonedDateTime
(com.google.gson.JsonElement element, String memberName) Takes a JSON in the form 'YYYY-MM-DD' and converts it to aZonedDateTime
.static com.google.gson.JsonPrimitive
getAsPrimitive
(com.google.gson.JsonElement jElement) Gets theJsonElement
asJsonPrimitive
.static com.google.gson.JsonPrimitive
getAsPrimitive
(com.google.gson.JsonElement jElement, String memberName) Gets the member of theJsonElement
asJsonPrimitive
.static short
getAsShort
(com.google.gson.JsonElement jElement) Gets theJsonElement
as short.static short
getAsShort
(com.google.gson.JsonElement jElement, String memberName) Gets the member of theJsonElement
as short.static String
getAsString
(com.google.gson.JsonElement jElement) Gets theJsonElement
asString
.static String
getAsString
(com.google.gson.JsonElement jElement, String memberName) Gets the member of theJsonElement
asString
.static String[]
getAsStringArray
(com.google.gson.JsonArray json) Converts aJsonArray
to a String Array.static String
getAsStringOrElse
(com.google.gson.JsonElement jElement, String memberName, String alternative) Gets the member of theJsonElement
asString
if it exists, and the alternative value otherwise.static <T> T
getAsType
(OpenemsType type, com.google.gson.JsonElement j) Gets aJsonElement
as the givenOpenemsType
.static Object
Gets aJsonElement
as the given type.static Object
Gets aJsonElement
as the given type.static UUID
getAsUUID
(com.google.gson.JsonElement jElement) Gets theJsonElement
asUUID
.static UUID
Gets the member of theJsonElement
asUUID
.static ZonedDateTime
getAsZonedDateWithZeroTime
(com.google.gson.JsonElement element, String memberName, ZoneId timezone) Takes a JSON in the form 'YYYY-MM-DD' and converts it to aZonedDateTime
with hour, minute and second set to zero.static Optional
<com.google.gson.JsonElement> getOptionalSubElement
(com.google.gson.JsonElement jElement, String memberName) static com.google.gson.JsonElement
getSubElement
(com.google.gson.JsonElement jElement, String memberName) Gets the member of theJsonElement
asJsonElement
.static boolean
isEmptyJsonArray
(com.google.gson.JsonElement j) Check if the givenJsonElement
is an empty JsonArray [].static boolean
isEmptyJsonObject
(com.google.gson.JsonElement j) Check if the givenJsonElement
is an empty JsonObject {}.static boolean
isNumber
(com.google.gson.JsonElement j) Check if the givenJsonElement
is aNumber
.static com.google.gson.JsonElement
Parses a string to aJsonElement
.static Optional
<com.google.gson.JsonElement> parseOptional
(String string) Parses a string to aOptional
JsonElement
.static com.google.gson.JsonArray
parseToJsonArray
(String string) Parses a string to aJsonArray
.static com.google.gson.JsonObject
parseToJsonObject
(String string) Parses a string to aJsonObject
.static void
prettyPrint
(com.google.gson.JsonElement j) Pretty print aJsonElement
.static String
prettyToString
(com.google.gson.JsonElement j) Pretty toString()-method for aJsonElement
.static Stream
<com.google.gson.JsonElement> stream
(com.google.gson.JsonArray jsonArray) Returns a sequential stream of theJsonElements
in theJsonArray
.static com.google.gson.JsonElement
Creates a JsonElement from theBoolean
value.static com.google.gson.JsonElement
Creates a JsonElement from theBoolean
value.static com.google.gson.JsonElement
Creates a JsonElement from theNumber
value.static com.google.gson.JsonElement
Creates a JsonElement from theBoolean
value.static Collector
<com.google.gson.JsonElement, JsonUtils.JsonArrayBuilder, com.google.gson.JsonArray> Returns a Collector that accumulates the input elements into a new JsonArray.static <T> Collector
<T, ?, com.google.gson.JsonObject> toJsonObject
(com.google.common.base.Function<T, ? extends String> keyMapper, com.google.common.base.Function<T, ? extends com.google.gson.JsonElement> valueMapper) Returns aCollector
that accumulates the input elements into a newJsonObject
.
-
Constructor Details
-
JsonUtils
public JsonUtils()
-
-
Method Details
-
generateJsonArray
public static <T> com.google.gson.JsonArray generateJsonArray(Collection<T> list, com.google.common.base.Function<T, com.google.gson.JsonElement> convert) Provide a easy way to generate a JsonArray from a list using the given convert function to add each element.- Type Parameters:
T
- type of an element from list- Parameters:
list
- to convertconvert
- function to convert elements- Returns:
- list as JsonArray
-
generateJsonArray
public static <T extends com.google.gson.JsonElement> com.google.gson.JsonArray generateJsonArray(Collection<T> list) Provide a easy way to generate a JsonArray from a collection of JsonElements.- Type Parameters:
T
- type of element from list- Parameters:
list
- to convert- Returns:
- list as JsonArray
-
buildJsonArray
Creates a JsonArray using a Builder.- Returns:
- the Builder
-
buildJsonArray
Creates a JsonArray using a Builder. Initialized from an existing JsonArray.- Parameters:
j
- the initial JsonArray- Returns:
- the Builder
-
buildJsonObject
Creates a JsonObject using a Builder.- Returns:
- the Builder
-
buildJsonObject
Creates a JsonObject using a Builder. Initialized from an existing JsonObject.- Parameters:
j
- the initial JsonObject- Returns:
- the Builder
-
toJson
Creates a JsonElement from theNumber
value.- Parameters:
number
- theNumber
- Returns:
- a
JsonPrimitive
orJsonNull
-
toJson
Creates a JsonElement from theBoolean
value.- Parameters:
bool
- theBoolean
- Returns:
- a
JsonPrimitive
orJsonNull
-
toJson
Creates a JsonElement from theBoolean
value.- Parameters:
c
- theCharacter
- Returns:
- a
JsonPrimitive
orJsonNull
-
toJson
Creates a JsonElement from theBoolean
value.- Parameters:
string
- theString
- Returns:
- a
JsonPrimitive
orJsonNull
-
getAsPrimitive
public static com.google.gson.JsonPrimitive getAsPrimitive(com.google.gson.JsonElement jElement) throws OpenemsError.OpenemsNamedException Gets theJsonElement
asJsonPrimitive
.- Parameters:
jElement
- theJsonElement
- Returns:
- the
JsonPrimitive
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsPrimitive
public static com.google.gson.JsonPrimitive getAsPrimitive(com.google.gson.JsonElement jElement, String memberName) throws OpenemsError.OpenemsNamedException Gets the member of theJsonElement
asJsonPrimitive
.- Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
JsonPrimitive
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalPrimitive
public static Optional<com.google.gson.JsonPrimitive> getAsOptionalPrimitive(com.google.gson.JsonElement jElement, String memberName) - Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
Optional
JsonPrimitive
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getSubElement
public static com.google.gson.JsonElement getSubElement(com.google.gson.JsonElement jElement, String memberName) throws OpenemsError.OpenemsNamedException Gets the member of theJsonElement
asJsonElement
.- Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
JsonElement
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getOptionalSubElement
public static Optional<com.google.gson.JsonElement> getOptionalSubElement(com.google.gson.JsonElement jElement, String memberName) - Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
Optional
JsonElement
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsJsonObject
public static com.google.gson.JsonObject getAsJsonObject(com.google.gson.JsonElement jElement) throws OpenemsError.OpenemsNamedException Gets theJsonElement
asJsonObject
.- Parameters:
jElement
- theJsonElement
- Returns:
- the
JsonObject
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsJsonObject
public static com.google.gson.JsonObject getAsJsonObject(com.google.gson.JsonElement jElement, String memberName) throws OpenemsError.OpenemsNamedException Gets the member of theJsonElement
asJsonObject
.- Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
JsonObject
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalJsonObject
public static Optional<com.google.gson.JsonObject> getAsOptionalJsonObject(com.google.gson.JsonElement jElement) - Parameters:
jElement
- theJsonElement
- Returns:
- the
Optional
JsonObject
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalJsonObject
public static Optional<com.google.gson.JsonObject> getAsOptionalJsonObject(com.google.gson.JsonElement jElement, String memberName) - Parameters:
jElement
- theJsonElement
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) throws OpenemsError.OpenemsNamedException Gets theJsonElement
asJsonArray
.- Parameters:
jElement
- theJsonElement
- Returns:
- the
JsonArray
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsJsonArray
public static com.google.gson.JsonArray getAsJsonArray(com.google.gson.JsonElement jElement, String memberName) throws OpenemsError.OpenemsNamedException Gets the member of theJsonElement
asJsonArray
.- Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
JsonArray
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalJsonArray
public static Optional<com.google.gson.JsonArray> getAsOptionalJsonArray(com.google.gson.JsonElement jElement) - Parameters:
jElement
- theJsonElement
- Returns:
- the
Optional
JsonArray
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalJsonArray
public static Optional<com.google.gson.JsonArray> getAsOptionalJsonArray(com.google.gson.JsonElement jElement, String memberName) - Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
Optional
JsonArray
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsString
public static String getAsString(com.google.gson.JsonElement jElement) throws OpenemsError.OpenemsNamedException Gets theJsonElement
asString
.- Parameters:
jElement
- theJsonElement
- Returns:
- the
String
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsString
public static String getAsString(com.google.gson.JsonElement jElement, String memberName) throws OpenemsError.OpenemsNamedException Gets the member of theJsonElement
asString
.- Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
String
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalString
- Parameters:
jElement
- theJsonElement
- Returns:
- the
Optional
String
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalString
public static Optional<String> getAsOptionalString(com.google.gson.JsonElement jElement, String memberName) - Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
Optional
String
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsStringOrElse
public static String getAsStringOrElse(com.google.gson.JsonElement jElement, String memberName, String alternative) Gets the member of theJsonElement
asString
if it exists, and the alternative value otherwise.- Parameters:
jElement
- theJsonElement
memberName
- the name of the memberalternative
- the alternative value- Returns:
- the
String
value or the alternative value
-
getAsStringArray
public static String[] getAsStringArray(com.google.gson.JsonArray json) throws OpenemsError.OpenemsNamedException Converts aJsonArray
to a String Array.- Parameters:
json
- theJsonArray
- Returns:
- a String Array
- Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsBoolean
public static boolean getAsBoolean(com.google.gson.JsonElement jElement) throws OpenemsError.OpenemsNamedException Gets theJsonElement
asBoolean
.- Parameters:
jElement
- theJsonElement
- Returns:
- the
Boolean
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsBoolean
public static boolean getAsBoolean(com.google.gson.JsonElement jElement, String memberName) throws OpenemsError.OpenemsNamedException Gets the member of theJsonElement
asBoolean
.- Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
Boolean
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalBoolean
- Parameters:
jElement
- theJsonElement
- Returns:
- the
Optional
Boolean
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalBoolean
public static Optional<Boolean> getAsOptionalBoolean(com.google.gson.JsonElement jElement, String memberName) - Parameters:
jElement
- theJsonElement
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) throws OpenemsError.OpenemsNamedException Gets theJsonElement
as short.- Parameters:
jElement
- theJsonElement
- Returns:
- the short value
- Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsShort
public static short getAsShort(com.google.gson.JsonElement jElement, String memberName) throws OpenemsError.OpenemsNamedException Gets the member of theJsonElement
as short.- Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the short value
- Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalShort
- Parameters:
jElement
- theJsonElement
- Returns:
- the
Optional
Short
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalShort
public static Optional<Short> getAsOptionalShort(com.google.gson.JsonElement jElement, String memberName) - Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
Optional
Boolean
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsInt
public static int getAsInt(com.google.gson.JsonElement jElement) throws OpenemsError.OpenemsNamedException Gets theJsonElement
as int.- Parameters:
jElement
- theJsonElement
- Returns:
- the int value
- Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsInt
public static int getAsInt(com.google.gson.JsonElement jElement, String memberName) throws OpenemsError.OpenemsNamedException Gets the member of theJsonElement
as int.- Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the int value
- Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsInt
public static int getAsInt(com.google.gson.JsonArray jArray, int index) throws OpenemsError.OpenemsNamedException Gets the member with given index of theJsonArray
as int.- Parameters:
jArray
- theJsonArray
index
- the index of the member- Returns:
- the int value
- Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalInt
- Parameters:
jElement
- theJsonElement
- Returns:
- the
Optional
Integer
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalInt
public static Optional<Integer> getAsOptionalInt(com.google.gson.JsonElement jElement, String memberName) - Parameters:
jElement
- theJsonElement
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) throws OpenemsError.OpenemsNamedException Gets theJsonElement
as long.- Parameters:
jElement
- theJsonElement
- Returns:
- the long value
- Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsLong
public static long getAsLong(com.google.gson.JsonElement jElement, String memberName) throws OpenemsError.OpenemsNamedException Gets the member of theJsonElement
as long.- Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the long value
- Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalLong
- Parameters:
jElement
- theJsonElement
- Returns:
- the
Optional
Long
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalLong
public static Optional<Long> getAsOptionalLong(com.google.gson.JsonElement jElement, String memberName) - Parameters:
jElement
- theJsonElement
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) throws OpenemsError.OpenemsNamedException Gets theJsonElement
asFloat
.- Parameters:
jElement
- theJsonElement
- Returns:
- the
Float
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsFloat
public static float getAsFloat(com.google.gson.JsonElement jElement, String memberName) throws OpenemsError.OpenemsNamedException Gets the member of theJsonElement
asFloat
.- Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
Float
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalFloat
- Parameters:
jElement
- theJsonElement
- Returns:
- the
Optional
Float
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalFloat
public static Optional<Float> getAsOptionalFloat(com.google.gson.JsonElement jElement, String memberName) - Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
Optional
Float
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsDouble
public static double getAsDouble(com.google.gson.JsonElement jElement) throws OpenemsError.OpenemsNamedException Gets theJsonElement
asDouble
.- Parameters:
jElement
- theJsonElement
- Returns:
- the
Double
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsDouble
public static double getAsDouble(com.google.gson.JsonElement jElement, String memberName) throws OpenemsError.OpenemsNamedException Gets the member of theJsonElement
asDouble
.- Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
Double
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalDouble
- Parameters:
jElement
- theJsonElement
- Returns:
- the
Optional
Double
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalDouble
public static Optional<Double> getAsOptionalDouble(com.google.gson.JsonElement jElement, String memberName) - Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
Optional
Double
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsEnum
public static <E extends Enum<E>> E getAsEnum(Class<E> enumType, com.google.gson.JsonElement jElement) throws OpenemsError.OpenemsNamedException Gets theJsonElement
asEnum
.- Type Parameters:
E
- theEnum
type- Parameters:
enumType
- the class of theEnum
jElement
- theJsonElement
- Returns:
- the
Enum
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsEnum
public static <E extends Enum<E>> E getAsEnum(Class<E> enumType, com.google.gson.JsonElement jElement, String memberName) throws OpenemsError.OpenemsNamedException Gets the member of theJsonElement
asEnum
.- Type Parameters:
E
- theEnum
type- Parameters:
enumType
- the class of theEnum
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
Enum
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalEnum
public static <E extends Enum<E>> Optional<E> getAsOptionalEnum(Class<E> enumType, com.google.gson.JsonElement jElement) - Type Parameters:
E
- theEnum
type- Parameters:
enumType
- the class of theEnum
jElement
- theJsonElement
- Returns:
- the
Optional
Enum
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalEnum
public static <E extends Enum<E>> Optional<E> getAsOptionalEnum(Class<E> enumType, com.google.gson.JsonElement jElement, String memberName) - Type Parameters:
E
- theEnum
type- Parameters:
enumType
- the class of theEnum
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
Optional
Enum
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsInet4Address
public static Inet4Address getAsInet4Address(com.google.gson.JsonElement jElement) throws OpenemsError.OpenemsNamedException Gets theJsonElement
asInet4Address
.- Parameters:
jElement
- theJsonElement
- Returns:
- the
Inet4Address
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsInet4Address
public static Inet4Address getAsInet4Address(com.google.gson.JsonElement jElement, String memberName) throws OpenemsError.OpenemsNamedException Gets the member of theJsonElement
asInet4Address
.- Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
Inet4Address
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalInet4Address
public static Optional<Inet4Address> getAsOptionalInet4Address(com.google.gson.JsonElement jElement) - Parameters:
jElement
- theJsonElement
- Returns:
- the
Optional
Inet4Address
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalInet4Address
public static Optional<Inet4Address> getAsOptionalInet4Address(com.google.gson.JsonElement jElement, String memberName) - Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
Optional
Inet4Address
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsUUID
public static UUID getAsUUID(com.google.gson.JsonElement jElement) throws OpenemsError.OpenemsNamedException Gets theJsonElement
asUUID
.- Parameters:
jElement
- theJsonElement
- Returns:
- the
UUID
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsUUID
public static UUID getAsUUID(com.google.gson.JsonElement jElement, String memberName) throws OpenemsError.OpenemsNamedException Gets the member of theJsonElement
asUUID
.- Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
UUID
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalUUID
- Parameters:
jElement
- theJsonElement
- Returns:
- the
Optional
UUID
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalUUID
public static Optional<UUID> getAsOptionalUUID(com.google.gson.JsonElement jElement, String memberName) - Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
Optional
UUID
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsBestType
public static Object getAsBestType(com.google.gson.JsonElement j) throws OpenemsError.OpenemsNamedException Tries to find the best matching Object representation of the givenJsonElement
.- Parameters:
j
- theJsonElement
- Returns:
- the Object
- Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsJsonElement
Gets aJsonElement
representing the Object value.- Parameters:
value
- theObject
value- Returns:
- the
JsonElement
-
getAsType
public static Object getAsType(Class<?> type, com.google.gson.JsonElement j) throws NotImplementedException, IllegalStateException Gets aJsonElement
as the given type.- Parameters:
type
- the class of the typej
- theJsonElement
- Returns:
- an Object of the given type
- Throws:
NotImplementedException
IllegalStateException
-
getAsType
public static <T> T getAsType(OpenemsType type, com.google.gson.JsonElement j) throws OpenemsError.OpenemsNamedException Gets aJsonElement
as the givenOpenemsType
.- Type Parameters:
T
- the Type for implicit casting of the result- Parameters:
type
- theOpenemsType
j
- theJsonElement
- Returns:
- an Object of the given type
- Throws:
OpenemsError.OpenemsNamedException
-
getAsType
public static Object getAsType(Optional<Class<?>> typeOptional, com.google.gson.JsonElement j) throws NotImplementedException Gets aJsonElement
as the given type.- Parameters:
typeOptional
- the class of the typej
- theJsonElement
- Returns:
- an Object of the given type
- Throws:
NotImplementedException
-
getAsZonedDateWithZeroTime
public static ZonedDateTime getAsZonedDateWithZeroTime(com.google.gson.JsonElement element, String memberName, ZoneId timezone) throws OpenemsError.OpenemsNamedException Takes a JSON in the form 'YYYY-MM-DD' and converts it to aZonedDateTime
with hour, minute and second set to zero.- Parameters:
element
- theJsonElement
memberName
- the name of the member of the JsonObjecttimezone
- the timezone asZoneId
- Returns:
- the
ZonedDateTime
- Throws:
OpenemsError.OpenemsNamedException
- on parse error
-
getAsOptionalZonedDateTime
public static Optional<ZonedDateTime> getAsOptionalZonedDateTime(com.google.gson.JsonElement element, String memberName) throws OpenemsError.OpenemsNamedException Takes a JSON in the form 'YYYY-MM-DD' and converts it to aZonedDateTime
.- Parameters:
element
- theJsonElement
memberName
- the name of the member of the JsonObject- Returns:
- the
ZonedDateTime
- Throws:
OpenemsError.OpenemsNamedException
-
parse
public static com.google.gson.JsonElement parse(String string) throws OpenemsError.OpenemsNamedException Parses a string to aJsonElement
.- Parameters:
string
- to be parsed- Returns:
- the
JsonElement
- Throws:
OpenemsError.OpenemsNamedException
- on error
-
parseOptional
Parses a string to aOptional
JsonElement
.- Parameters:
string
- to be parsed- Returns:
- the
Optional
of the result;Optional.empty()
if the value can not be parsed
-
parseToJsonObject
public static com.google.gson.JsonObject parseToJsonObject(String string) throws OpenemsError.OpenemsNamedException Parses a string to aJsonObject
.- Parameters:
string
- the String- Returns:
- the
JsonObject
- Throws:
OpenemsError.OpenemsNamedException
- on error
-
parseToJsonArray
public static com.google.gson.JsonArray parseToJsonArray(String string) throws OpenemsError.OpenemsNamedException Parses a string to aJsonArray
.- Parameters:
string
- the String- Returns:
- the
JsonArray
- Throws:
OpenemsError.OpenemsNamedException
- on error
-
prettyPrint
public static void prettyPrint(com.google.gson.JsonElement j) Pretty print aJsonElement
.- Parameters:
j
- theJsonElement
-
prettyToString
Pretty toString()-method for aJsonElement
.- Parameters:
j
- theJsonElement
- Returns:
- a pretty string representing the
JsonElement
usingGsonBuilder.setPrettyPrinting()
-
isEmptyJsonObject
public static boolean isEmptyJsonObject(com.google.gson.JsonElement j) Check if the givenJsonElement
is an empty JsonObject {}.- Parameters:
j
- theJsonElement
to check- Returns:
- true if is empty, otherwise false
-
isEmptyJsonArray
public static boolean isEmptyJsonArray(com.google.gson.JsonElement j) Check if the givenJsonElement
is an empty JsonArray [].- Parameters:
j
- theJsonElement
to check- Returns:
- true if is empty, otherwise false
-
isNumber
public static boolean isNumber(com.google.gson.JsonElement j) Check if the givenJsonElement
is aNumber
.- Parameters:
j
- theJsonElement
to check- Returns:
- true if the element is a
Number
, otherwise false
-
stream
Returns a sequential stream of theJsonElements
in theJsonArray
.- Parameters:
jsonArray
- TheJsonArray
, assumed to be unmodified during use- Returns:
- a Stream of the elements
-
toJsonObject
public static <T> Collector<T,?, toJsonObjectcom.google.gson.JsonObject> (com.google.common.base.Function<T, ? extends String> keyMapper, com.google.common.base.Function<T, ? extends com.google.gson.JsonElement> valueMapper) Returns aCollector
that accumulates the input elements into a newJsonObject
.- Type Parameters:
T
- the type of the input- Parameters:
keyMapper
- the key mappervalueMapper
- the value mapper- Returns:
- the
Collector
-
toJsonArray
public static Collector<com.google.gson.JsonElement,JsonUtils.JsonArrayBuilder, toJsonArray()com.google.gson.JsonArray> Returns a Collector that accumulates the input elements into a new JsonArray.- Returns:
- a Collector which collects all the input elements into a JsonArray
-