Json.Net 自定义序列化



我正在序列化一个对象集合,其中包含一个名为动态属性的字典。

发出的默认 Json 如下所示:

[{"dynamicProperties":{"WatchId":7771,"Issues":0,"WatchType":"x","Location":"Equinix Source","Name":"PI_5570_5580"}},
{"dynamicProperties":{"WatchId":7769,"Issues":0,"WatchType":"x","Location":"Equinix Source","Name":"PI_5570_5574"}},
{"dynamicProperties":{"WatchId":7767,"Issues":0,"WatchType":"x","Location":"Equinix Source","Name":"PI_5570_5572"}},
{"dynamicProperties":{"WatchId":7765,"Issues":0,"WatchType":"y","Location":"Equinix Source","Name":"highlight_SM"}},
{"dynamicProperties":{"WatchId":8432,"Issues":0,"WatchType":"y","Location":"Test Devices","Name":"Cisco1700PI"}}]

我想生成如下所示的 Json:

[{"WatchId":7771,"Issues":0,"WatchType":"x","Location":"Equinix Source","Name":"PI_5570_5580"},
{"WatchId":7769,"Issues":0,"WatchType":"x","Location":"Equinix Source","Name":"PI_5570_5574"},
{"WatchId":7767,"Issues":0,"WatchType":"x","Location":"Equinix Source","Name":"PI_5570_5572"},
{"WatchId":7765,"Issues":0,"WatchType":"y","Location":"Equinix Source","Name":"highlight_SM"},
{"WatchId":8432,"Issues":0,"WatchType":"y","Location":"Test Devices","Name":"Cisco1700PI"}]

通过阅读 Json.Net 文档,看起来我可以为我的类构建一个 CustomContractResolver,但我找不到有关如何执行此操作的任何详细信息......谁能告诉我应该看的方向?

我正在尝试序列化的类如下。

我需要一个允许动态样式行为的类,并且还可以通过 WCF 进行序列化和反序列化。

 [DataContract]
public class SerializableDynamicObject : IDynamicMetaObjectProvider
{
    [DataMember]
    private IDictionary<string, object> dynamicProperties = new Dictionary<string, object>();
    #region IDynamicMetaObjectProvider implementation
    public DynamicMetaObject GetMetaObject(Expression expression)
    {
        return new SerializableDynamicMetaObject(expression,
            BindingRestrictions.GetInstanceRestriction(expression, this), this);
    }
    #endregion

    #region Helper methods for dynamic meta object support
    internal object setValue(string name, object value)
    {
        dynamicProperties.Add(name, value);
        return value;
    }
    internal object getValue(string name)
    {
        object value;
        if (!dynamicProperties.TryGetValue(name, out value))
        {
            value = null;
        }
        return value;
    }
    internal IEnumerable<string> getDynamicMemberNames()
    {
        return dynamicProperties.Keys;
    }
    #endregion
}

实现 ISerializable

[DataContract]
public class SerializableDynamicObject : IDynamicMetaObjectProvider, ISerializable
{
    [DataMember]
    private IDictionary<string, object> dynamicProperties = new Dictionary<string, object>();
    #region IDynamicMetaObjectProvider implementation
    public DynamicMetaObject GetMetaObject(Expression expression)
    {
        return new SerializableDynamicMetaObject(expression,
            BindingRestrictions.GetInstanceRestriction(expression, this), this);
    }
    #endregion

    #region Helper methods for dynamic meta object support
    internal object setValue(string name, object value)
    {
        dynamicProperties.Add(name, value);
        return value;
    }
    internal object getValue(string name)
    {
        object value;
        if (!dynamicProperties.TryGetValue(name, out value))
        {
            value = null;
        }
        return value;
    }
    internal IEnumerable<string> getDynamicMemberNames()
    {
        return dynamicProperties.Keys;
    }
    #endregion
    #region ISerializable Members
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        foreach (var key in dynamicProperties.Keys)
        {
            info.AddValue(key.ToString(), dynamicProperties[key]);
        }
    }
    #endregion
}

现在在一个小控制台应用程序中测试此代码...

    static void Main(string[] args)
    {
        SerializableDynamicObject obj1 = new SerializableDynamicObject();
        obj1.setValue("WatchId", 7771);
        obj1.setValue("Issues", 0);
        obj1.setValue("Location", "sample location1");
        obj1.setValue("Name", "sample name 1");
        SerializableDynamicObject obj2 = new SerializableDynamicObject();
        obj2.setValue("WatchId", 7771);
        obj2.setValue("Issues", 0);
        obj2.setValue("Location", "sample location1");
        obj2.setValue("Name", "sample name 1");
        SerializableDynamicObject obj3 = new SerializableDynamicObject();
        obj3.setValue("WatchId", 7771);
        obj3.setValue("Issues", 0);
        obj3.setValue("Location", "sample location1");
        obj3.setValue("Name", "sample name 1");
        SerializableDynamicObject[] dictArray = new
        SerializableDynamicObject[] { 
            obj1, obj2, obj3
        };
        Newtonsoft.Json.JsonSerializer ser = new Newtonsoft.Json.JsonSerializer();
        ser.Serialize(Console.Out, dictArray);
}

这是程序的输出:

[

{"监视 ID":7771,"问题":0,"位置":"示例位置 1","名称":"示例名称 1"},{"监视 ID":7771,"问题":0,"位置":"示例位置 2","名称":"示例名称 2"},{"监视 ID":7771,"问题":0,"位置":"示例位置 3","名称":"示例名称 3"}]

相关内容

  • 没有找到相关文章

最新更新