Skip to content

Latest commit

 

History

History
34 lines (24 loc) · 1.61 KB

File metadata and controls

34 lines (24 loc) · 1.61 KB

LaunchDarkly SDK Json.NET Adapter

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:

  1. Install the package LaunchDarkly.CommonSdk.JsonNet.

  2. 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
        }
    };
  1. You can reference this configuration in any individual Json.NET operation:
    var json = JsonConvert.SerializeObject(someObject, settings);
  1. Or, to make these settings the default for all Json.NET operations:
    JsonConvert.DefaultSettings = () => settings;