Package io.openems.common.utils
Class JsonUtils
- java.lang.Object
-
- io.openems.common.utils.JsonUtils
-
public class JsonUtils extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
JsonUtils.JsonArrayBuilder
A temporary builder class for JsonArrays.static class
JsonUtils.JsonArrayCollector
static class
JsonUtils.JsonObjectBuilder
A temporary builder class for JsonObjects.
-
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 <T> com.google.gson.JsonArray
generateJsonArray(java.util.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 java.lang.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, java.lang.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, java.lang.String memberName)
Gets the member of theJsonElement
asDouble
.static <E extends java.lang.Enum<E>>
EgetAsEnum(java.lang.Class<E> enumType, com.google.gson.JsonElement jElement)
Gets theJsonElement
asEnum
.static <E extends java.lang.Enum<E>>
EgetAsEnum(java.lang.Class<E> enumType, com.google.gson.JsonElement jElement, java.lang.String memberName)
Gets the member of theJsonElement
asEnum
.static float
getAsFloat(com.google.gson.JsonElement jElement)
Gets theJsonElement
asFloat
.static float
getAsFloat(com.google.gson.JsonElement jElement, java.lang.String memberName)
Gets the member of theJsonElement
asFloat
.static java.net.Inet4Address
getAsInet4Address(com.google.gson.JsonElement jElement)
Gets theJsonElement
asInet4Address
.static java.net.Inet4Address
getAsInet4Address(com.google.gson.JsonElement jElement, java.lang.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
getAsInt(com.google.gson.JsonElement jElement, java.lang.String memberName)
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, java.lang.String memberName)
Gets the member of theJsonElement
asJsonArray
.static com.google.gson.JsonElement
getAsJsonElement(java.lang.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, java.lang.String memberName)
Gets the member of theJsonElement
asJsonObject
.static long
getAsLong(com.google.gson.JsonElement jElement)
Gets theJsonElement
as long.static long
getAsLong(com.google.gson.JsonElement jElement, java.lang.String memberName)
Gets the member of theJsonElement
as long.static java.util.Optional<java.lang.Boolean>
getAsOptionalBoolean(com.google.gson.JsonElement jElement)
Gets theJsonElement
as anOptional
Boolean
.static java.util.Optional<java.lang.Boolean>
getAsOptionalBoolean(com.google.gson.JsonElement jElement, java.lang.String memberName)
Gets the member of theJsonElement
as anOptional
Boolean
.static java.util.Optional<java.lang.Double>
getAsOptionalDouble(com.google.gson.JsonElement jElement)
Gets theJsonElement
asOptional
Double
.static java.util.Optional<java.lang.Double>
getAsOptionalDouble(com.google.gson.JsonElement jElement, java.lang.String memberName)
Gets the member of theJsonElement
asOptional
Double
.static <E extends java.lang.Enum<E>>
java.util.Optional<E>getAsOptionalEnum(java.lang.Class<E> enumType, com.google.gson.JsonElement jElement)
Gets theJsonElement
asOptional
Enum
.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 theJsonElement
asOptional
Enum
.static java.util.Optional<java.lang.Float>
getAsOptionalFloat(com.google.gson.JsonElement jElement)
Gets theJsonElement
asOptional
Float
.static java.util.Optional<java.lang.Float>
getAsOptionalFloat(com.google.gson.JsonElement jElement, java.lang.String memberName)
Gets the member of theJsonElement
asOptional
Float
.static java.util.Optional<java.net.Inet4Address>
getAsOptionalInet4Address(com.google.gson.JsonElement jElement)
Gets theJsonElement
asOptional
Inet4Address
.static java.util.Optional<java.net.Inet4Address>
getAsOptionalInet4Address(com.google.gson.JsonElement jElement, java.lang.String memberName)
Gets the member of theJsonElement
asOptional
Inet4Address
.static java.util.Optional<java.lang.Integer>
getAsOptionalInt(com.google.gson.JsonElement jElement)
Gets theJsonElement
asOptional
Integer
.static java.util.Optional<java.lang.Integer>
getAsOptionalInt(com.google.gson.JsonElement jElement, java.lang.String memberName)
Gets the member of theJsonElement
asOptional
Integer
.static java.util.Optional<com.google.gson.JsonArray>
getAsOptionalJsonArray(com.google.gson.JsonElement jElement)
Gets the member of theJsonElement
asOptional
JsonArray
.static java.util.Optional<com.google.gson.JsonArray>
getAsOptionalJsonArray(com.google.gson.JsonElement jElement, java.lang.String memberName)
Gets the member of theJsonElement
asOptional
JsonArray
.static java.util.Optional<com.google.gson.JsonObject>
getAsOptionalJsonObject(com.google.gson.JsonElement jElement)
Gets theJsonElement
asOptional
JsonObject
.static java.util.Optional<com.google.gson.JsonObject>
getAsOptionalJsonObject(com.google.gson.JsonElement jElement, java.lang.String memberName)
Gets the member of theJsonElement
asOptional
JsonObject
.static java.util.Optional<java.lang.Long>
getAsOptionalLong(com.google.gson.JsonElement jElement)
Gets theJsonElement
asOptional
Long
.static java.util.Optional<java.lang.Long>
getAsOptionalLong(com.google.gson.JsonElement jElement, java.lang.String memberName)
Gets the member of theJsonElement
asOptional
Long
.static java.util.Optional<java.lang.Short>
getAsOptionalShort(com.google.gson.JsonElement jElement)
Gets theJsonElement
as anOptional
Short
.static java.util.Optional<java.lang.Short>
getAsOptionalShort(com.google.gson.JsonElement jElement, java.lang.String memberName)
Gets the member of theJsonElement
as anOptional
Short
.static java.util.Optional<java.lang.String>
getAsOptionalString(com.google.gson.JsonElement jElement)
Gets theJsonElement
asOptional
String
.static java.util.Optional<java.lang.String>
getAsOptionalString(com.google.gson.JsonElement jElement, java.lang.String memberName)
Gets the member of theJsonElement
asOptional
String
.static java.util.Optional<java.util.UUID>
getAsOptionalUUID(com.google.gson.JsonElement jElement)
Gets theJsonElement
asOptional
UUID
.static java.util.Optional<java.util.UUID>
getAsOptionalUUID(com.google.gson.JsonElement jElement, java.lang.String memberName)
Gets the member of theJsonElement
asOptional
UUID
.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, java.lang.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, java.lang.String memberName)
Gets the member of theJsonElement
as short.static java.lang.String
getAsString(com.google.gson.JsonElement jElement)
Gets theJsonElement
asString
.static java.lang.String
getAsString(com.google.gson.JsonElement jElement, java.lang.String memberName)
Gets the member of theJsonElement
asString
.static java.lang.String[]
getAsStringArray(com.google.gson.JsonArray json)
Converts aJsonArray
to a String Array.static <T> T
getAsType(OpenemsType type, com.google.gson.JsonElement j)
Gets aJsonElement
as the givenOpenemsType
.static java.lang.Object
getAsType(java.lang.Class<?> type, com.google.gson.JsonElement j)
Gets aJsonElement
as the given type.static java.lang.Object
getAsType(java.util.Optional<java.lang.Class<?>> typeOptional, com.google.gson.JsonElement j)
Gets aJsonElement
as the given type.static java.util.UUID
getAsUUID(com.google.gson.JsonElement jElement)
Gets theJsonElement
asUUID
.static java.util.UUID
getAsUUID(com.google.gson.JsonElement jElement, java.lang.String memberName)
Gets the member of theJsonElement
asUUID
.static java.time.ZonedDateTime
getAsZonedDateWithZeroTime(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 aZonedDateTime
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 theJsonElement
asOptional
JsonElement
.static com.google.gson.JsonElement
getSubElement(com.google.gson.JsonElement jElement, java.lang.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 com.google.gson.JsonElement
parse(java.lang.String string)
Parses a string to aJsonElement
.static com.google.gson.JsonArray
parseToJsonArray(java.lang.String string)
Parses a string to aJsonArray
.static com.google.gson.JsonObject
parseToJsonObject(java.lang.String string)
Parses a string to aJsonObject
.static void
prettyPrint(com.google.gson.JsonElement j)
Pretty print aJsonElement
.static java.lang.String
prettyToString(com.google.gson.JsonElement j)
Pretty toString()-method for aJsonElement
.static java.util.stream.Stream<com.google.gson.JsonElement>
stream(com.google.gson.JsonArray jsonArray)
Returns a sequential stream of theJsonElements
in theJsonArray
.static java.util.stream.Collector<com.google.gson.JsonElement,JsonUtils.JsonArrayBuilder,com.google.gson.JsonArray>
toJsonArray()
Returns a Collector that accumulates the input elements into a new JsonArray.
-
-
-
Method Detail
-
generateJsonArray
public static <T> com.google.gson.JsonArray generateJsonArray(java.util.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
-
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) 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, java.lang.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
-
getSubElement
public static com.google.gson.JsonElement getSubElement(com.google.gson.JsonElement jElement, java.lang.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 java.util.Optional<com.google.gson.JsonElement> getOptionalSubElement(com.google.gson.JsonElement jElement, java.lang.String memberName)
Gets the member of theJsonElement
asOptional
JsonElement
.- 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, java.lang.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 java.util.Optional<com.google.gson.JsonObject> getAsOptionalJsonObject(com.google.gson.JsonElement jElement)
Gets theJsonElement
asOptional
JsonObject
.- Parameters:
jElement
- theJsonElement
- 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 theJsonElement
asOptional
JsonObject
.- 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, java.lang.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 java.util.Optional<com.google.gson.JsonArray> getAsOptionalJsonArray(com.google.gson.JsonElement jElement)
Gets the member of theJsonElement
asOptional
JsonArray
.- Parameters:
jElement
- theJsonElement
- Returns:
- the
Optional
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 theJsonElement
asOptional
JsonArray
.- Parameters:
jElement
- theJsonElement
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) throws OpenemsError.OpenemsNamedException
Gets theJsonElement
asString
.- Parameters:
jElement
- theJsonElement
- Returns:
- the
String
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 theJsonElement
asString
.- Parameters:
jElement
- theJsonElement
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 theJsonElement
asOptional
String
.- Parameters:
jElement
- theJsonElement
- 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 theJsonElement
asOptional
String
.- Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
Optional
String
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsStringArray
public static java.lang.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, java.lang.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
public static java.util.Optional<java.lang.Boolean> getAsOptionalBoolean(com.google.gson.JsonElement jElement)
Gets theJsonElement
as anOptional
Boolean
.- Parameters:
jElement
- theJsonElement
- Returns:
- the
Optional
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 theJsonElement
as anOptional
Boolean
.- 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, java.lang.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
public static java.util.Optional<java.lang.Short> getAsOptionalShort(com.google.gson.JsonElement jElement)
Gets theJsonElement
as anOptional
Short
.- Parameters:
jElement
- theJsonElement
- Returns:
- the
Optional
Short
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalShort
public static java.util.Optional<java.lang.Short> getAsOptionalShort(com.google.gson.JsonElement jElement, java.lang.String memberName)
Gets the member of theJsonElement
as anOptional
Short
.- 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, java.lang.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
public static java.util.Optional<java.lang.Integer> getAsOptionalInt(com.google.gson.JsonElement jElement)
Gets theJsonElement
asOptional
Integer
.- Parameters:
jElement
- theJsonElement
- 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 theJsonElement
asOptional
Integer
.- 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, java.lang.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
public static java.util.Optional<java.lang.Long> getAsOptionalLong(com.google.gson.JsonElement jElement)
Gets theJsonElement
asOptional
Long
.- Parameters:
jElement
- theJsonElement
- Returns:
- the
Optional
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 theJsonElement
asOptional
Long
.- 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, java.lang.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
public static java.util.Optional<java.lang.Float> getAsOptionalFloat(com.google.gson.JsonElement jElement)
Gets theJsonElement
asOptional
Float
.- Parameters:
jElement
- theJsonElement
- Returns:
- the
Optional
Float
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalFloat
public static java.util.Optional<java.lang.Float> getAsOptionalFloat(com.google.gson.JsonElement jElement, java.lang.String memberName)
Gets the member of theJsonElement
asOptional
Float
.- 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, java.lang.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
public static java.util.Optional<java.lang.Double> getAsOptionalDouble(com.google.gson.JsonElement jElement)
Gets theJsonElement
asOptional
Double
.- Parameters:
jElement
- theJsonElement
- Returns:
- the
Optional
Double
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsOptionalDouble
public static java.util.Optional<java.lang.Double> getAsOptionalDouble(com.google.gson.JsonElement jElement, java.lang.String memberName)
Gets the member of theJsonElement
asOptional
Double
.- Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
Optional
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 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 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 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 java.lang.Enum<E>> java.util.Optional<E> getAsOptionalEnum(java.lang.Class<E> enumType, com.google.gson.JsonElement jElement)
Gets theJsonElement
asOptional
Enum
.- 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 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 theJsonElement
asOptional
Enum
.- 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 java.net.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 java.net.Inet4Address getAsInet4Address(com.google.gson.JsonElement jElement, java.lang.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 java.util.Optional<java.net.Inet4Address> getAsOptionalInet4Address(com.google.gson.JsonElement jElement)
Gets theJsonElement
asOptional
Inet4Address
.- Parameters:
jElement
- theJsonElement
- Returns:
- the
Optional
Inet4Address
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 theJsonElement
asOptional
Inet4Address
.- Parameters:
jElement
- theJsonElement
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) throws OpenemsError.OpenemsNamedException
Gets theJsonElement
asUUID
.- Parameters:
jElement
- theJsonElement
- Returns:
- the
UUID
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 theJsonElement
asUUID
.- Parameters:
jElement
- theJsonElement
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)
Gets theJsonElement
asOptional
UUID
.- Parameters:
jElement
- theJsonElement
- Returns:
- the
Optional
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 theJsonElement
asOptional
UUID
.- Parameters:
jElement
- theJsonElement
memberName
- the name of the member- Returns:
- the
Optional
UUID
value - Throws:
OpenemsError.OpenemsNamedException
- on error
-
getAsBestType
public static java.lang.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
public static com.google.gson.JsonElement getAsJsonElement(java.lang.Object value)
Gets aJsonElement
representing the Object value.- Parameters:
value
- theObject
value- Returns:
- the
JsonElement
-
getAsType
public static java.lang.Object getAsType(java.lang.Class<?> type, com.google.gson.JsonElement j) throws NotImplementedException, java.lang.IllegalStateException
Gets aJsonElement
as the given type.- Parameters:
type
- the class of the typej
- theJsonElement
- Returns:
- an Object of the given type
- Throws:
NotImplementedException
java.lang.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 java.lang.Object getAsType(java.util.Optional<java.lang.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 java.time.ZonedDateTime getAsZonedDateWithZeroTime(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 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
-
parse
public static com.google.gson.JsonElement parse(java.lang.String string) throws OpenemsError.OpenemsNamedException
Parses a string to aJsonElement
.- Parameters:
string
- to be parsed- Returns:
- the
JsonElement
- Throws:
OpenemsError.OpenemsNamedException
- on error
-
parseToJsonObject
public static com.google.gson.JsonObject parseToJsonObject(java.lang.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(java.lang.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
public static java.lang.String prettyToString(com.google.gson.JsonElement j)
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
-
stream
public static java.util.stream.Stream<com.google.gson.JsonElement> stream(com.google.gson.JsonArray jsonArray)
Returns a sequential stream of theJsonElements
in theJsonArray
.- Parameters:
jsonArray
- TheJsonArray
, assumed to be unmodified during use- Returns:
- a Stream of the elements
-
toJsonArray
public static java.util.stream.Collector<com.google.gson.JsonElement,JsonUtils.JsonArrayBuilder,com.google.gson.JsonArray> toJsonArray()
Returns a Collector that accumulates the input elements into a new JsonArray.- Returns:
- a Collector which collects all the input elements into a JsonArray
-
-