The add-on package LaunchDarkly.CommonSdk.JsonNet
allows JSON-serializable data types from the LaunchDarkly .NET SDK and Xamarin SDK, such as User
and LdValue
, to be encoded and decoded correctly by the Json.NET library (Newtonsoft.Json
).
Earlier versions of the LaunchDarkly SDKs used Json.NET internally, so nothing additional was needed to make this work. However, starting with LaunchDarkly .NET SDK 6.0 and LaunchDarkly Xamarin SDK 2.0, the Json.NET dependency was removed and so these types do not contain the [JsonConverter]
annotation that would tell Json.NET how to encode and decode them.
It is always possible to encode or decode these types explicitly using the LaunchDarkly.Sdk.Json.LdJsonSerialization
class. But if you want them to be handled automatically by code that uses Json.NET, just do the following:
-
Install the package
LaunchDarkly.CommonSdk.JsonNet
. -
Define a
JsonSerializerSettings
object that includes the LaunchDarkly JSON converter:
var settings = new Newtonsoft.Json.JsonSerializerSettings
{
Converters = new List<Newtonsoft.Json.JsonConverter>
{
LaunchDarkly.Sdk.Json.LdJsonNet.Converter
// you may add any other custom converters you want here
}
};
- You can reference this configuration in any individual Json.NET operation:
var json = JsonConvert.SerializeObject(someObject, settings);
- Or, to make these settings the default for all Json.NET operations:
JsonConvert.DefaultSettings = () => settings;