using System; using System.Collections.Generic; using System.Runtime.Serialization; using Newtonsoft.Json; namespace MLEM.Data.Json { /// /// A json type-safe wrapper can be used to wrap any objects of any type before submitting them to a non-specifically typed or that will be serialized using a in cases where is not set to . /// If any object is not wrapped in this manner, and it has a custom , the value deserialized from it might not have the same type as the originally serialized object. This behavior can be observed, for example, when serializing a of entries, one of which is a : The will be serialized as a and, upon deserialization, will remain a . /// In general, wrapping objects in this manner is only useful in rare cases, where custom data of an unexpected or unknown type is stored. /// See for an example of how this class and can be used, and see this stackoverflow answer for more information on the problem that this class solves: https://stackoverflow.com/a/38798114. /// public abstract class JsonTypeSafeWrapper { /// /// Returns this json type-safe wrapper's value as an . /// public abstract object Value { get; } } /// [DataContract] public class JsonTypeSafeWrapper : JsonTypeSafeWrapper { /// public override object Value => this.value; [DataMember] private readonly T value; /// /// Creates a new json type-safe wrapper instance that wraps the given . /// /// The value to wrap public JsonTypeSafeWrapper(T value) { this.value = value; } } }