Package com.massifmaps.core
Class Variant
java.lang.Object
com.massifmaps.core.Variant
JSON value. Can contain JSON-style structured data, including objects and arrays.
-
Constructor Summary
ConstructorsConstructorDescriptionVariant()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.Variant(StringVariantMap object) Constructs Variant object from a map of values.Variant(VariantVector array) Constructs Variant object from a list of values.Constructs Variant object from a string. -
Method Summary
Modifier and TypeMethodDescriptionbooleancontainsObjectKey(String key) Returns true if object elements contains the specified key.booleanChecks if this object is equal to the specified object.static VariantfromString(String str) Creates a Variant object from a JSON string representation.getArrayElement(int idx) Returns the element of array at specified position.intReturns the number of elements in the array.booleangetBool()Returns the boolean value of this variant.doubleReturns the floating point value of this variant.longgetLong()Returns the integer value of this variant.getObjectElement(String key) Returns the element of object with the specified key.Returns all the keys in the object.Returns the string value of this variant.intgetType()Returns the type of this variant.inthashCode()Returns the hash value of this object.toString()Converts the variant to JSON string.
-
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
Constructs Variant object from a string.- Parameters:
string- The string value.
-
Variant
Constructs Variant object from a list of values.- Parameters:
array- The array of JSON values.
-
Variant
Constructs Variant object from a map of values.- Parameters:
object- The map of JSON values.
-
-
Method Details
-
equals
Checks if this object is equal to the specified object. -
hashCode
public int hashCode()Returns the hash value of this object. -
getType
public int getType()Returns the type of this variant.- Returns:
- The type of this variant.
-
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
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
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
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
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
Converts the variant to JSON string. -
fromString
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.
-