Class Variant

java.lang.Object
com.massifmaps.core.Variant

public class Variant extends Object
JSON value. Can contain JSON-style structured data, including objects and arrays.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a null Variant object.
    Variant(boolean boolVal)
    Constructs Variant object from a boolean.
    Variant(double doubleVal)
    Constructs Variant object from a double.
    Variant(long longVal)
    Constructs Variant object from an integer.
    Constructs Variant object from a map of values.
    Constructs Variant object from a list of values.
    Variant(String string)
    Constructs Variant object from a string.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Returns true if object elements contains the specified key.
    boolean
    Checks if this object is equal to the specified object.
    static Variant
    Creates a Variant object from a JSON string representation.
    getArrayElement(int idx)
    Returns the element of array at specified position.
    int
    Returns the number of elements in the array.
    boolean
    Returns the boolean value of this variant.
    double
    Returns the floating point value of this variant.
    long
    Returns the integer value of this variant.
    Returns the element of object with the specified key.
    Returns all the keys in the object.
    Returns the string value of this variant.
    int
    Returns the type of this variant.
    int
    Returns the hash value of this object.
    Converts the variant to JSON string.

    Methods inherited from class java.lang.Object

    clone, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Variant

      public Variant()
      Constructs a null Variant object.
    • Variant

      public Variant(boolean boolVal)
      Constructs Variant object from a boolean.
      Parameters:
      boolVal - The boolean value.
    • Variant

      public Variant(long longVal)
      Constructs Variant object from an integer.
      Parameters:
      longVal - The integer value.
    • Variant

      public Variant(double doubleVal)
      Constructs Variant object from a double.
      Parameters:
      doubleVal - The double value.
    • Variant

      public Variant(String string)
      Constructs Variant object from a string.
      Parameters:
      string - The string value.
    • Variant

      public Variant(VariantVector array)
      Constructs Variant object from a list of values.
      Parameters:
      array - The array of JSON values.
    • Variant

      public Variant(StringVariantMap object)
      Constructs Variant object from a map of values.
      Parameters:
      object - The map of JSON values.
  • Method Details

    • equals

      public boolean equals(Object obj)
      Checks if this object is equal to the specified object.
      Overrides:
      equals in class Object
      Parameters:
      obj - The reference object.
      Returns:
      True when objects are equal, false otherwise.
    • hashCode

      public int hashCode()
      Returns the hash value of this object.
      Overrides:
      hashCode in class Object
      Returns:
      The hash value of this object.
    • getType

      public int getType()
      Returns the type of this variant.
      Returns:
      The type of this variant.
    • getString

      public String getString()
      Returns the string value of this variant.
      Returns:
      The string value of the variant. If the variant is not string, empty string will be returned.
    • getBool

      public boolean getBool()
      Returns the boolean value of this variant.
      Returns:
      The boolean value of the variant. If the variant is not boolean, false will be returned.
    • getLong

      public long getLong()
      Returns the integer value of this variant.
      Returns:
      The integer value of the variant. If the variant is not integer, 0 will be returned.
    • getDouble

      public double getDouble()
      Returns the floating point value of this variant.
      Returns:
      The floating point value of the variant. If the variant is integer, it will be converted to floating point. Otherwise 0.0 is be returned.
    • getArraySize

      public int getArraySize()
      Returns the number of elements in the array.
      Returns:
      The number of elements in the array if the variant is of array type. Otherwise 0 is returned.
    • getArrayElement

      public Variant getArrayElement(int idx)
      Returns the element of array at specified position.
      Parameters:
      idx - The index of the array element to return (starting from 0).
      Returns:
      The array element at specified position or null type if the element does not exist or the variant is not an array.
    • getObjectKeys

      public StringVector getObjectKeys()
      Returns all the keys in the object.
      Returns:
      The list containing all the keys of the object if the variant is of object type. Otherwise empty list is returned.
    • containsObjectKey

      public boolean containsObjectKey(String key)
      Returns true if object elements contains the specified key.
      Parameters:
      key - The key of the object element to check.
      Returns:
      True if the specified key exists in the object. If the element is not an object or the key does not exist, false is returned.
    • getObjectElement

      public Variant getObjectElement(String key)
      Returns the element of object with the specified key.
      Parameters:
      key - The key of the object element to return.
      Returns:
      The object element with the specified key or null type if the element does not exist or the variant is not an object.
    • toString

      public String toString()
      Converts the variant to JSON string.
      Overrides:
      toString in class Object
      Returns:
      The JSON string corresponding to the variant.
    • fromString

      public static Variant fromString(String str)
      Creates a Variant object from a JSON string representation.
      Parameters:
      str - The JSON string to use for the variant.
      Returns:
      The corresponding Variant object.
      Throws:
      Exception - If the string parsing fails.