Package org.jdrupes.vmoperator.util
Class GsonPtr
java.lang.Object
org.jdrupes.vmoperator.util.GsonPtr
Utility class for pointing to elements on a Gson (Json) tree.
-
Method Summary
Modifier and TypeMethodDescription<T extends com.google.gson.JsonElement>
TcomputeIfAbsent
(Object selector, Supplier<T> supplier) Same asset(Object, JsonElement)
, but sets the value only if it doesn’t exist yet, else returns the existing value.com.google.gson.JsonElement
get()
ReturnsJsonElement
that the pointer points to.<T extends com.google.gson.JsonElement>
TReturnsJsonElement
that the pointer points to, casted to the given type.<T extends com.google.gson.JsonElement>
Optional<T> Returns the selectedJsonElement
, cast to the class specified.getAsBigInteger
(Object... selectors) Returns the Integer value of the selectedJsonPrimitive
.getAsBoolean
(Object... selectors) Returns the boolean value of the selectedJsonPrimitive
.Returns the Integer value of the selectedJsonPrimitive
.<T extends com.google.gson.JsonElement>
List<T> getAsListOf
(Class<T> cls, Object... selectors) Returns the elements of the selectedJsonArray
as list.Returns the Long value of the selectedJsonPrimitive
.getAsString
(Object... selectors) Returns the String value of the selectedJsonPrimitive
.Short forcomputeIfAbsent(selector, () -> new JsonPrimitive(value))
.Sets the selected value.Short forset(selector, new JsonPrimitive(value))
.Short forset(selector, new JsonPrimitive(value))
.set
(Object selector, BigInteger value) Short forset(selector, new JsonPrimitive(value))
.static GsonPtr
to
(com.google.gson.JsonElement root) Create a new instance pointing to the given element.Create a new instance pointing to theJsonElement
selected by the given selectors.
-
Method Details
-
to
Create a new instance pointing to the given element.- Parameters:
root
- the root- Returns:
- the Gson pointer
-
to
Create a new instance pointing to theJsonElement
selected by the given selectors.If a selector of type
String
denotes a non-existant member of aJsonObject
, a new member (of typeJsonObject
is added.- Parameters:
selectors
- the selectors- Returns:
- the Gson pointer
-
get
ReturnsJsonElement
that the pointer points to.- Returns:
- the result
-
get
ReturnsJsonElement
that the pointer points to, casted to the given type.- Type Parameters:
T
- the generic type- Parameters:
cls
- the cls- Returns:
- the result
-
get
Returns the selectedJsonElement
, cast to the class specified.- Type Parameters:
T
- the generic type- Parameters:
cls
- the clsselectors
- the selectors- Returns:
- the optional
-
getAsString
Returns the String value of the selectedJsonPrimitive
.- Parameters:
selectors
- the selectors- Returns:
- the as string
-
getAsInt
Returns the Integer value of the selectedJsonPrimitive
.- Parameters:
selectors
- the selectors- Returns:
- the as string
-
getAsBigInteger
Returns the Integer value of the selectedJsonPrimitive
.- Parameters:
selectors
- the selectors- Returns:
- the as string
-
getAsLong
Returns the Long value of the selectedJsonPrimitive
.- Parameters:
selectors
- the selectors- Returns:
- the as string
-
getAsBoolean
Returns the boolean value of the selectedJsonPrimitive
.- Parameters:
selectors
- the selectors- Returns:
- the boolean
-
getAsListOf
public <T extends com.google.gson.JsonElement> List<T> getAsListOf(Class<T> cls, Object... selectors) Returns the elements of the selectedJsonArray
as list.- Type Parameters:
T
- the generic type- Parameters:
cls
- the clsselectors
- the selectors- Returns:
- the list
-
set
Sets the selected value.This pointer must point to a
JsonObject
orJsonArray
. The selector must be aString
or an integer respectively.- Parameters:
selector
- the selectorvalue
- the value- Returns:
- the Gson pointer
-
set
Short forset(selector, new JsonPrimitive(value))
.- Parameters:
selector
- the selectorvalue
- the value- Returns:
- the gson ptr
- See Also:
-
set
Short forset(selector, new JsonPrimitive(value))
.- Parameters:
selector
- the selectorvalue
- the value- Returns:
- the gson ptr
- See Also:
-
set
Short forset(selector, new JsonPrimitive(value))
.- Parameters:
selector
- the selectorvalue
- the value- Returns:
- the gson ptr
- See Also:
-
computeIfAbsent
public <T extends com.google.gson.JsonElement> T computeIfAbsent(Object selector, Supplier<T> supplier) Same asset(Object, JsonElement)
, but sets the value only if it doesn’t exist yet, else returns the existing value.If this pointer points to a
JsonArray
and the selector if larger than or equal to the size of the array, the supplied value will be appended.- Type Parameters:
T
- the generic type- Parameters:
selector
- the selectorsupplier
- the supplier of the missing value- Returns:
- the existing or supplied value
-
getOrSet
Short forcomputeIfAbsent(selector, () -> new JsonPrimitive(value))
.- Parameters:
selector
- the selectorvalue
- the value- Returns:
- the Gson pointer
-