Class LDValue
- All Implemented Interfaces:
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:
- With the
LDValuemethodstoJsonString()andparse(String). - With
JsonSerialization. - With Gson, if and only if you configure your
Gsoninstance withLDGson. - With Jackson, if and only if you configure your
ObjectMapperinstance withLDJackson.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classPredefined instances ofLDValue.Converterfor commonly used types.static classDefines a conversion betweenLDValueand some other type. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic LDValueCreates an array value from the specified values.booleanReturns this value as a boolean if it is explicitly a boolean.static ArrayBuilderStarts building an array value.static ObjectBuilderStarts building an object value.doubleReturns this value as adoubleif it is numeric.booleanReturns true if the other object is anLDValuethat is logically equal.floatReturns this value as afloatif it is numeric.get(int index) Returns an array element by index.Returns an object property by name.abstract LDValueTypegetType()Gets the JSON type for this value.inthashCode()intintValue()Returns this value as anintif it is numeric.booleanisInt()Tests whether this value is a number that is also an integer.booleanisNull()Tests whether this value is a null.booleanisNumber()Tests whether this value is a number (not a numeric string).booleanisString()Tests whether this value is a string.keys()Enumerates the property names in an object.longReturns this value as alongif it is numeric.static LDValueReturns the same value if non-null, orofNull()if null.static LDValueof(boolean value) Returns an instance for a boolean value.static LDValueof(double value) Returns an instance for a numeric value.static LDValueof(float value) Returns an instance for a numeric value.static LDValueof(int value) Returns an instance for a numeric value.static LDValueof(long value) Returns an instance for a numeric value.static LDValueReturns an instance for a string value (or a null).static LDValueofNull()Returns an instance for a null value.static LDValueParses an LDValue from a JSON representation.intsize()Returns the number of elements in an array or object.Returns this value as aStringif it is a string.Converts this value to its JSON serialization.toString()Returns a string representation of this value.values()Enumerates the values in an array or object.<T> Iterable<T>valuesAs(LDValue.Converter<T> converter) Enumerates the values in an array or object, converting them to a specific type.
-
Constructor Details
-
LDValue
public LDValue()
-
-
Method Details
-
normalize
Returns the same value if non-null, orofNull()if null. -
ofNull
Returns an instance for a null value. The same instance is always used.- Returns:
- an LDValue containing null
-
of
Returns an instance for a boolean value. The same instances fortrueandfalseare always used.- Parameters:
value- a boolean value- Returns:
- an LDValue containing that value
-
of
Returns an instance for a numeric value.- Parameters:
value- an integer numeric value- Returns:
- an LDValue containing that value
-
of
Returns an instance for a numeric 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 oflongvalues cannot be accurately represented. If you need to set a context attribute to a numeric value with more significant digits than will fit in adouble, it is best to encode it as a string.- Parameters:
value- a long integer numeric value- Returns:
- an LDValue containing that value
-
of
Returns an instance for a numeric value.- Parameters:
value- a floating-point numeric value- Returns:
- an LDValue containing that value
-
of
Returns an instance for a numeric value.- Parameters:
value- a floating-point numeric value- Returns:
- an LDValue containing that value
-
of
Returns an instance for a string value (or a null).- Parameters:
value- a nullable String reference- Returns:
- an LDValue containing a string, or
ofNull()if the value was null.
-
buildArray
Starts building an array value. The elements can be of any type supported by LDValue.
If the values are all of the same type, you may also useLDValue arrayOfInts = LDValue.buildArray().add(2).add("three").build():LDValue.Converter.arrayFrom(Iterable)orLDValue.Converter.arrayOf(Object...).- Returns:
- an
ArrayBuilder
-
arrayOf
Creates an array value from the specified values. The elements can be of any type supported by LDValue.
If the values are all of the same type, you may also useLDValue arrayOfMixedValues = LDValue.arrayOf(LDValue.of(2), LDValue.of("three"));LDValue.Converter.arrayFrom(Iterable)orLDValue.Converter.arrayOf(Object...).- Parameters:
values- any number of values- Returns:
- an immutable array value
-
buildObject
Starts building an object value.
If the values are all of the same type, you may also useLDValue objectVal = LDValue.buildObject().put("key", LDValue.int(1)).build():LDValue.Converter.objectFrom(Map).- Returns:
- an
ObjectBuilder
-
parse
Parses an LDValue from a JSON representation.This convenience method is equivalent to using
JsonSerialization.deserialize(String, Class)with theLDValueclass, except for two things:1. You do not have to provide the class parameter.
2. Parsing errors are thrown as an unchecked
RuntimeExceptionthat wraps the checkedSerializationException, making this method somewhat more convenient in cases such as test code where explicit error handling is less important.- Parameters:
json- a JSON string- Returns:
- an LDValue
-
getType
Gets the JSON type for this value.- Returns:
- the appropriate
LDValueType
-
isNull
public boolean isNull()Tests whether this value is a null.- Returns:
trueif this is a null value
-
booleanValue
public boolean booleanValue()Returns this value as a boolean if it is explicitly a boolean. Otherwise returnsfalse.- Returns:
- a boolean
-
isNumber
public boolean isNumber()Tests whether this value is a number (not a numeric string).- Returns:
trueif this is a numeric value
-
isInt
public boolean isInt()Tests whether this value is a number that is also an integer.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()andLDValue.of(2.0f).isInt()are both true.- Returns:
trueif this is an integer value
-
intValue
public int intValue()Returns this value as anintif 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.
- Returns:
- an
intvalue
-
longValue
public long longValue()Returns this value as alongif 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.
- Returns:
- a
longvalue
-
floatValue
public float floatValue()Returns this value as afloatif it is numeric. Returns zero for all non-numeric values.- Returns:
- a
floatvalue
-
doubleValue
public double doubleValue()Returns this value as adoubleif it is numeric. Returns zero for all non-numeric values.- Returns:
- a
doublevalue
-
isString
public boolean isString()Tests whether this value is a string.- Returns:
trueif this is a string value
-
stringValue
Returns this value as aStringif it is a string. Returnsnullfor all non-string values.- Returns:
- a nullable string value
-
size
public int size()Returns the number of elements in an array or object. Returns zero for all other types.- Returns:
- the number of array elements or object properties
-
keys
Enumerates the property names in an object. Returns an empty iterable for all other types.- Returns:
- the property names
-
values
Enumerates the values in an array or object. Returns an empty iterable for all other types.- Returns:
- an iterable of
LDValuevalues
-
valuesAs
Enumerates the values in an array or object, converting them to a specific type. Returns an empty iterable for all other types.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
BooleanorInteger,valuesAswill never return a null element; instead, it will use the appropriate default value for the primitive type (false or zero).- Type Parameters:
T- the desired type- Parameters:
converter- theLDValue.Converterfor the specified type- Returns:
- an iterable of values of the specified type
-
get
Returns an array element by index. ReturnsofNull()if this is not an array or if the index is out of range (will never throw an exception).- Parameters:
index- the array index- Returns:
- the element value or
ofNull()
-
get
Returns an object property by name. ReturnsofNull()if this is not an object or if the key is not found (will never throw an exception).- Parameters:
name- the property name- Returns:
- the property value or
ofNull()
-
toJsonString
Converts this value to its JSON serialization.This method is equivalent to passing the
LDValueinstance toJsonSerialization.serialize(JsonSerializable).- Returns:
- a JSON string
-
toString
Returns a string representation of this value.This method currently returns the same JSON serialization as
toJsonString(). However, like mosttoString()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 matchtoJsonString()in the future. If you need to verify the value type or other properties programmatically, use the getter methods ofLDValue. -
equals
Returns true if the other object is anLDValuethat 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.
-
hashCode
public int hashCode()
-