Programmer sitting in front of computer screen.

Changes to serialization of PropertyList items in Adaptive Images for Optimizely


If you use the AdaptiveImageConverter or SingleImageConverter JSON converters for PropertyList properties, you need to make some minor changes to avoid errors when adding or editing items.

Estimated read time : 3 minutes

Jump to

Key takeaways

  • New JSON converters replace previous Newtonsoft converters
  • Use converters from the AdaptiveImages.Shell.Json.Serialization namespace
  • Make sure to use JsonConverter attribute from System.Text.Json.Serialization namespace

Note: As of version 2.0.6.23 there is no longer any need for explicitly specifying JSON converters. Default JSON converters are now automatically registered supporting both Newtonsoft and System.Text.Json.

TL;DR summary

If you use the SingleImageConverter or AdaptiveImageConverter classes for PropertyList item types in Optimizely CMS 12, you need to ensure you are using the ones from the AdaptiveImages.Shell.Json.Serialization namespace. This applies from version 2.0.6.22 of Adaptive Images.

Since the new JSON converters are based on the System.Text.Json namespace, i.e. built-in .NET serialization, you also need to make sure you use the JsonConverter attribute from the System.Text.Json.Serialization namespace.

Refer to the Adaptive Images SDK for a code sample with correct namespaces.

Same names, different namespaces

Potentially confusing, there are now two different JSON converter implementations for AdaptiveImage and SingleImage instances, called AdaptiveImageConverter and SingleImageConverter, respectively.

One implementation is based on Newtonsoft and resides in the AdaptiveImages.Shell.Json namespace. This one is now obsolete.

The other implementation is based on .NET and resides in the AdaptiveImages.Shell.Json.Serialization namespace.

Since both Newtonsoft and .NET have their own JsonConverter attributes, it's important to pair the right attribute type with the right converter type.

In other words, the JsonConverter attribute from the System.Text.Json namespace must be used together with the applicable JSON converter type from the AdaptiveImages.Shell.Json.Serialization namespace.

If this gets mixed up, you will receive serialization errors when using the collection editor to add or edit PropertyList items.

What to change in your code

You might have something like the following for your PropertyList, or IList<T>, properties:

using AdaptiveImages.Models;
using AdaptiveImages.Shell.Json;
using Newtonsoft.Json;

public class MyListItem
{
    [JsonConverter(typeof(AdaptiveImageConverter))]
    public virtual AdaptiveImage MyAdaptiveImage { get; set; }

    [JsonConverter(typeof(SingleImageConverter))]
    public virtual SingleImage MySingleImage { get; set; }
}

This should be changed into:

using AdaptiveImages.Models;
using AdaptiveImages.Shell.Json.Serialization;
using System.Text.Json.Serialization;

public class MyListItem
{
    [JsonConverter(typeof(AdaptiveImageConverter))]
    public virtual AdaptiveImage MyAdaptiveImage { get; set; }

    [JsonConverter(typeof(SingleImageConverter))]
    public virtual SingleImage MySingleImage { get; set; }
}

Notice how it's really only the using statements that change.