Class LdValue.Converter<T>
Defines a conversion between LdValue and some other type.
Inherited Members
Namespace: LaunchDarkly.Sdk
Assembly: LaunchDarkly.CommonSdk.dll
Syntax
public abstract class LdValue.Converter<T>
Type Parameters
Name | Description |
---|---|
T | the type to convert from/to |
Remarks
Besides converting individual values, LdValue.Converter<T> provides factory methods like ArrayOf(params T[]) which transform a collection of the specified type to the corresponding LdValue complex type.
There are type-specific instances of this class for commonly used types in LdValue.Convert, but you can also implement your own.
Constructors
Converter()
Declaration
protected Converter()
Methods
ArrayFrom(IEnumerable<T>)
Initializes an LdValue as an array, from a sequence of this type.
Declaration
public LdValue ArrayFrom(IEnumerable<T> values)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | values | a sequence of elements of the specified type |
Returns
Type | Description |
---|---|
LdValue | a struct representing a JSON array, or Null if the parameter was null |
Remarks
Values are copied, so subsequent changes to the source values do not affect the array.
Examples
var listOfInts = new List<int> { 1, 2, 3 };
var arrayValue = LdValue.Convert.Int.ArrayFrom(arrayOfInts);
ArrayOf(params T[])
Initializes an LdValue as an array, from a sequence of this type.
Declaration
public LdValue ArrayOf(params T[] values)
Parameters
Type | Name | Description |
---|---|---|
T[] | values | any number of elements of the specified type |
Returns
Type | Description |
---|---|
LdValue | a struct representing a JSON array |
Remarks
Values are copied, so subsequent changes to the source values do not affect the array.
Examples
var arrayValue = LdValue.Convert.Int.ArrayOf(1, 2, 3);
FromType(T)
Converts a value of the specified type to an LdValue.
Declaration
public abstract LdValue FromType(T valueOfType)
Parameters
Type | Name | Description |
---|---|---|
T | valueOfType | a value of this type |
Returns
Type | Description |
---|---|
LdValue | an LdValue |
Remarks
This method should never throw an exception; if for some reason the value is invalid, it should return Null.
ObjectFrom(IReadOnlyDictionary<string, T>)
Initializes an LdValue as a JSON object, from a dictionary containing values of this type.
Declaration
public LdValue ObjectFrom(IReadOnlyDictionary<string, T> dictionary)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyDictionary<string, T> | dictionary | a dictionary with string keys and values of the specified type |
Returns
Type | Description |
---|---|
LdValue | a struct representing a JSON object, or Null if the parameter was null |
Remarks
Values are copied, so subsequent changes to the source values do not affect the array.
Examples
var dictionaryOfInts = new Dictionary<string, int> { { "a", 1 }, { "b", 2 } };
var objectValue = LdValue.Convert.Int.ObjectFrom(dictionaryOfInts);
ToType(LdValue)
Converts an LdValue to a value of the specified type.
Declaration
public abstract T ToType(LdValue jsonValue)
Parameters
Type | Name | Description |
---|---|---|
LdValue | jsonValue | an LdValue |
Returns
Type | Description |
---|---|
T | a value of this type |
Remarks
This method should never throw an exception; if the conversion cannot be done, it
should return default(T)
.