我正在使用Newtonsoft json.net序列化程序将我的一些模型序列化为json。
其中一些很复杂,包含循环引用。由于循环引用不可序列化,因此将使用设置ReferenceLoopHandling = ReferenceLoopHandling.Ignore进行处理。
使用ISupportInitialize在其他序列化之后,将还原引用。
ISupportInitialize还用于初始化数据库中的大量属性(图像)。一些类没有默认的ctor(需要[NotNull]参数),我知道我的繁重属性可能是惰性的,但是由于ISupportInitialize在以前的序列化程序中可以正常工作(并经过测试),因此我更喜欢使用该机制。

我正在尝试获取json.net序列化程序,以检测实现ISupportInitialize接口的类型并调用适当的接口方法。当实际的序列化/反序列化的对象实现该接口时,这很简单;而当对象图中深处的某些属性实现该接口时,这将变得更加困难(或者至少对我而言不直观)。我尝试编写一个自定义转换器,但是没有运气。以下是我要完成的简单示例

 public class SupportsInitialize : ISupportInitialize
    {
        public void BeginInit(){throw new NotImplementedException();}

        public void EndInit(){throw new NotImplementedException();}
    }

    [Test]
    public void MakeSerializerCallBeginAndEndInit()
    {
        var supportsInitialize = new ToBeSerialized() {SupportsInitialize = new SupportsInitialize()};
        // before serializing any property implementing ISupportInitialize (ToBeSerialized.SupportsInitialize in this case) its BeginInit should be called
        var json = JsonConvert.SerializeObject(supportsInitialize);
        // after deserializing any property implementing ISupportInitialize its EndInit should be called
        var deserialized = JsonConvert.DeserializeObject<ToBeSerialized>(json);
    }

    public class ToBeSerialized
    {
        public SupportsInitialize SupportsInitialize { get; set; }
    }


感谢您为我指明正确方向的任何帮助

最佳答案

如果您不想手动向每个[OnDeserializing] and [OnDeserialized]类型添加调用BeginInit()EndInit()ISupportInitialize回调,则可以创建DefaultContractResolver的自定义子类,该子类会自动调用适当的方法:

public class ISupportInitializeContractResolver : DefaultContractResolver
{
    // As of 7.0.1, Json.NET suggests using a static instance for "stateless" contract resolvers, for performance reasons.
    // http://www.newtonsoft.com/json/help/html/ContractResolver.htm
    // http://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Serialization_DefaultContractResolver__ctor_1.htm
    // "Use the parameterless constructor and cache instances of the contract resolver within your application for optimal performance."
    static ISupportInitializeContractResolver instance;

    // Using a static constructor enables fairly lazy initialization.  http://csharpindepth.com/Articles/General/Singleton.aspx
    static ISupportInitializeContractResolver() { instance = new ISupportInitializeContractResolver(); }

    public static ISupportInitializeContractResolver Instance { get { return instance; } }

    readonly SerializationCallback onDeserializing;
    readonly SerializationCallback onDeserialized;

    protected ISupportInitializeContractResolver()
        : base()
    {
        onDeserializing = (o, context) =>
            {
                var init = o as ISupportInitialize;
                if (init != null)
                    init.BeginInit();
            };
        onDeserialized = (o, context) =>
            {
                var init = o as ISupportInitialize;
                if (init != null)
                    init.EndInit();
            };
    }

    protected override JsonContract CreateContract(Type objectType)
    {
        var contract = base.CreateContract(objectType);
        if (typeof(ISupportInitialize).IsAssignableFrom(objectType))
        {
            contract.OnDeserializingCallbacks.Add(onDeserializing);
            contract.OnDeserializedCallbacks.Add(onDeserialized);
        }
        return contract;
    }
}


然后像这样使用它:

        var settings = new JsonSerializerSettings { ContractResolver = ISupportInitializeContractResolver.Instance };
        var root = JsonConvert.DeserializeObject<ToBeSerialized>(jsonString, settings);

09-13 08:57