public abstract class LDValue extends java.lang.Object implements JsonSerializable
An LDValue
instance can be a null (that is, an instance that represents a JSON null value,
rather than a Java null reference), a boolean, a number (always encoded internally as double-precision
floating-point, but can be treated as an integer), a string, an ordered list of LDValue
values (a JSON array), or a map of strings to LDValue
values (a JSON object). It is easily
convertible to standard Java types.
This can be used to represent complex data in a context attribute (see ContextBuilder.set(String, LDValue)
),
or to get a feature flag value that uses a complex type or that does not always use the same
type (see the client's jsonValueVariation
methods).
While the LaunchDarkly SDK uses Gson internally for JSON parsing, it uses LDValue
rather
than Gson's JsonElement
type for two reasons. First, this allows Gson types to be excluded
from the API, so the SDK does not expose this dependency and cannot cause version conflicts in
applications that use Gson themselves. Second, Gson's array and object types are mutable, which can
cause concurrency risks.
LDValue
can be converted to and from JSON in any of these ways:
LDValue
methods toJsonString()
and parse(String)
.
JsonSerialization
.
Gson
instance with
LDGson
.
ObjectMapper
instance with
LDJackson
.
Modifier and Type | Class and Description |
---|---|
static class |
LDValue.Convert
Predefined instances of
LDValue.Converter for commonly used types. |
static class |
LDValue.Converter<T>
Defines a conversion between
LDValue and some other type. |
Constructor and Description |
---|
LDValue() |
Modifier and Type | Method and Description |
---|---|
static LDValue |
arrayOf(LDValue... values)
Creates an array value from the specified values.
|
boolean |
booleanValue()
Returns this value as a boolean if it is explicitly a boolean.
|
static ArrayBuilder |
buildArray()
Starts building an array value.
|
static ObjectBuilder |
buildObject()
Starts building an object value.
|
double |
doubleValue()
Returns this value as a
double if it is numeric. |
boolean |
equals(java.lang.Object o)
Returns true if the other object is an
LDValue that is logically equal. |
float |
floatValue()
Returns this value as a
float if it is numeric. |
LDValue |
get(int index)
Returns an array element by index.
|
LDValue |
get(java.lang.String name)
Returns an object property by name.
|
abstract LDValueType |
getType()
Gets the JSON type for this value.
|
int |
hashCode() |
int |
intValue()
Returns this value as an
int if it is numeric. |
boolean |
isInt()
Tests whether this value is a number that is also an integer.
|
boolean |
isNull()
Tests whether this value is a null.
|
boolean |
isNumber()
Tests whether this value is a number (not a numeric string).
|
boolean |
isString()
Tests whether this value is a string.
|
java.lang.Iterable<java.lang.String> |
keys()
Enumerates the property names in an object.
|
long |
longValue()
Returns this value as a
long if it is numeric. |
static LDValue |
normalize(LDValue value)
Returns the same value if non-null, or
ofNull() if null. |
static LDValue |
of(boolean value)
Returns an instance for a boolean value.
|
static LDValue |
of(double value)
Returns an instance for a numeric value.
|
static LDValue |
of(float value)
Returns an instance for a numeric value.
|
static LDValue |
of(int value)
Returns an instance for a numeric value.
|
static LDValue |
of(long value)
Returns an instance for a numeric value.
|
static LDValue |
of(java.lang.String value)
Returns an instance for a string value (or a null).
|
static LDValue |
ofNull()
Returns an instance for a null value.
|
static LDValue |
parse(java.lang.String json)
Parses an LDValue from a JSON representation.
|
int |
size()
Returns the number of elements in an array or object.
|
java.lang.String |
stringValue()
Returns this value as a
String if it is a string. |
java.lang.String |
toJsonString()
Converts this value to its JSON serialization.
|
java.lang.String |
toString()
Returns a string representation of this value.
|
java.lang.Iterable<LDValue> |
values()
Enumerates the values in an array or object.
|
<T> java.lang.Iterable<T> |
valuesAs(LDValue.Converter<T> converter)
Enumerates the values in an array or object, converting them to a specific type.
|
public static LDValue normalize(LDValue value)
ofNull()
if null.public static LDValue ofNull()
public static LDValue of(boolean value)
true
and false
are always used.value
- a boolean valuepublic static LDValue of(int value)
value
- an integer numeric valuepublic static LDValue of(long value)
Note that the LaunchDarkly service, and most of the SDKs, represent numeric values internally
in 64-bit floating-point, which has slightly less precision than a signed 64-bit long
;
therefore, the full range of long
values cannot be accurately represented. If you need
to set a context attribute to a numeric value with more significant digits than will fit in a
double
, it is best to encode it as a string.
value
- a long integer numeric valuepublic static LDValue of(float value)
value
- a floating-point numeric valuepublic static LDValue of(double value)
value
- a floating-point numeric valuepublic static LDValue of(java.lang.String value)
value
- a nullable String referenceofNull()
if the value was null.public static ArrayBuilder buildArray()
LDValue arrayOfInts = LDValue.buildArray().add(2).add("three").build():
If the values are all of the same type, you may also use LDValue.Converter.arrayFrom(Iterable)
or LDValue.Converter.arrayOf(Object...)
.ArrayBuilder
public static LDValue arrayOf(LDValue... values)
LDValue arrayOfMixedValues = LDValue.arrayOf(LDValue.of(2), LDValue.of("three"));
If the values are all of the same type, you may also use LDValue.Converter.arrayFrom(Iterable)
or LDValue.Converter.arrayOf(Object...)
.values
- any number of valuespublic static ObjectBuilder buildObject()
LDValue objectVal = LDValue.buildObject().put("key", LDValue.int(1)).build():
If the values are all of the same type, you may also use LDValue.Converter.objectFrom(Map)
.ObjectBuilder
public static LDValue parse(java.lang.String json)
This convenience method is equivalent to using JsonSerialization.deserialize(String, Class)
with the LDValue
class, except for two things:
1. You do not have to provide the class parameter.
2. Parsing errors are thrown as an unchecked RuntimeException
that wraps the checked
SerializationException
, making this method somewhat more convenient in cases such as
test code where explicit error handling is less important.
json
- a JSON stringpublic abstract LDValueType getType()
LDValueType
public boolean isNull()
true
if this is a null valuepublic boolean booleanValue()
false
.public boolean isNumber()
true
if this is a numeric valuepublic boolean isInt()
JSON does not have separate types for integer and floating-point values; they are both just
numbers. This method returns true if and only if the actual numeric value has no fractional
component, so LDValue.of(2).isInt()
and LDValue.of(2.0f).isInt()
are both true.
true
if this is an integer valuepublic int intValue()
int
if it is numeric. Returns zero for all non-numeric values.
If the value is a number but not an integer, it will be rounded toward zero (truncated). This is consistent with Java casting behavior, and with most other LaunchDarkly SDKs.
int
valuepublic long longValue()
long
if it is numeric. Returns zero for all non-numeric values.
If the value is a number but not an integer, it will be rounded toward zero (truncated). This is consistent with Java casting behavior, and with most other LaunchDarkly SDKs.
long
valuepublic float floatValue()
float
if it is numeric. Returns zero for all non-numeric values.float
valuepublic double doubleValue()
double
if it is numeric. Returns zero for all non-numeric values.double
valuepublic boolean isString()
true
if this is a string valuepublic java.lang.String stringValue()
String
if it is a string. Returns null
for all non-string values.public int size()
public java.lang.Iterable<java.lang.String> keys()
public java.lang.Iterable<LDValue> values()
LDValue
valuespublic <T> java.lang.Iterable<T> valuesAs(LDValue.Converter<T> converter)
This is an efficient method because it does not copy values to a new list, but returns a view into the existing array.
Example:
LDValue anArrayOfInts = LDValue.Convert.Integer.arrayOf(1, 2, 3);
for (int i: anArrayOfInts.valuesAs(LDValue.Convert.Integer)) { println(i); }
For boolean and numeric types, even though the corresponding Java type is a nullable class like
Boolean
or Integer
, valuesAs
will never return a null element; instead,
it will use the appropriate default value for the primitive type (false or zero).
T
- the desired typeconverter
- the LDValue.Converter
for the specified typepublic LDValue get(int index)
ofNull()
if this is not an array or if the
index is out of range (will never throw an exception).index
- the array indexofNull()
public LDValue get(java.lang.String name)
ofNull()
if this is not an object or if the
key is not found (will never throw an exception).name
- the property nameofNull()
public java.lang.String toJsonString()
This method is equivalent to passing the LDValue
instance to
JsonSerialization.serialize(JsonSerializable)
.
public java.lang.String toString()
This method currently returns the same JSON serialization as toJsonString()
. However,
like most toString()
implementations, it is intended mainly for convenience in
debugging or other use cases where the goal is simply to have a human-readable format; it is
not guaranteed to always match toJsonString()
in the future. If you need to verify
the value type or other properties programmatically, use the getter methods of LDValue
.
toString
in class java.lang.Object
public boolean equals(java.lang.Object o)
LDValue
that is logically equal.
This is a deep equality comparison: for JSON arrays each element is compared recursively, and for JSON objects all property names and values must be deeply equal regardless of ordering.
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object