Class GsonPtr

java.lang.Object
org.jdrupes.vmoperator.util.GsonPtr

public class GsonPtr extends Object
Utility class for pointing to elements on a Gson (Json) tree.
  • Method Details

    • to

      public static GsonPtr to(com.google.gson.JsonElement root)
      Create a new instance pointing to the given element.
      Parameters:
      root - the root
      Returns:
      the Gson pointer
    • to

      public GsonPtr to(Object... selectors)
      Create a new instance pointing to the JsonElement selected by the given selectors.

      If a selector of type String denotes a non-existant member of a JsonObject, a new member (of type JsonObject is added.

      Parameters:
      selectors - the selectors
      Returns:
      the Gson pointer
    • get

      public com.google.gson.JsonElement get()
      Returns JsonElement that the pointer points to.
      Returns:
      the result
    • get

      public <T extends com.google.gson.JsonElement> T get(Class<T> cls)
      Returns JsonElement that the pointer points to, casted to the given type.
      Type Parameters:
      T - the generic type
      Parameters:
      cls - the cls
      Returns:
      the result
    • get

      public <T extends com.google.gson.JsonElement> Optional<T> get(Class<T> cls, Object... selectors)
      Returns the selected JsonElement, cast to the class specified.
      Type Parameters:
      T - the generic type
      Parameters:
      cls - the cls
      selectors - the selectors
      Returns:
      the optional
    • getAsString

      public Optional<String> getAsString(Object... selectors)
      Returns the String value of the selected JsonPrimitive.
      Parameters:
      selectors - the selectors
      Returns:
      the as string
    • getAsInt

      public Optional<Integer> getAsInt(Object... selectors)
      Returns the Integer value of the selected JsonPrimitive.
      Parameters:
      selectors - the selectors
      Returns:
      the as string
    • getAsBigInteger

      public Optional<BigInteger> getAsBigInteger(Object... selectors)
      Returns the Integer value of the selected JsonPrimitive.
      Parameters:
      selectors - the selectors
      Returns:
      the as string
    • getAsLong

      public Optional<Long> getAsLong(Object... selectors)
      Returns the Long value of the selected JsonPrimitive.
      Parameters:
      selectors - the selectors
      Returns:
      the as string
    • getAsBoolean

      public Optional<Boolean> getAsBoolean(Object... selectors)
      Returns the boolean value of the selected JsonPrimitive.
      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 selected JsonArray as list.
      Type Parameters:
      T - the generic type
      Parameters:
      cls - the cls
      selectors - the selectors
      Returns:
      the list
    • set

      public GsonPtr set(Object selector, com.google.gson.JsonElement value)
      Sets the selected value.

      This pointer must point to a JsonObject or JsonArray. The selector must be a String or an integer respectively.

      Parameters:
      selector - the selector
      value - the value
      Returns:
      the Gson pointer
    • set

      public GsonPtr set(Object selector, String value)
      Short for set(selector, new JsonPrimitive(value)).
      Parameters:
      selector - the selector
      value - the value
      Returns:
      the gson ptr
      See Also:
    • set

      public GsonPtr set(Object selector, Long value)
      Short for set(selector, new JsonPrimitive(value)).
      Parameters:
      selector - the selector
      value - the value
      Returns:
      the gson ptr
      See Also:
    • set

      public GsonPtr set(Object selector, BigInteger value)
      Short for set(selector, new JsonPrimitive(value)).
      Parameters:
      selector - the selector
      value - the value
      Returns:
      the gson ptr
      See Also:
    • computeIfAbsent

      public <T extends com.google.gson.JsonElement> T computeIfAbsent(Object selector, Supplier<T> supplier)
      Same as set(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 selector
      supplier - the supplier of the missing value
      Returns:
      the existing or supplied value
    • getOrSet

      public GsonPtr getOrSet(Object selector, String value)
      Short for computeIfAbsent(selector, () -> new JsonPrimitive(value)).
      Parameters:
      selector - the selector
      value - the value
      Returns:
      the Gson pointer