JSON .NET不尊重反序列化PreserveReferencesHandling [英] JSON .Net not respecting PreserveReferencesHandling on Deserialization

查看:285
本文介绍了JSON .NET不尊重反序列化PreserveReferencesHandling的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经双向链表,我想deserialise列表

I have doubly linked list that I am trying to deserialise.

我的方案密切相关这一SO:的双向链表以JSON

My scenario closely relates to this SO: Doubly Linked List to JSON

我有以下的JSON设置:

I have the following JSON settings:

_jsonSettings = new JsonSerializerSettings() 
{ 
    TypeNameHandling = TypeNameHandling.Auto, 
    ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
    PreserveReferencesHandling = PreserveReferencesHandling.Objects,
    ObjectCreationHandling = ObjectCreationHandling.Auto
};

当我看着序列化输出,它似乎正确的,节点之间的引用适当的代表。

When I look at the serialised output, it appears correct, and the references between nodes are properly represented.

当数据被deserialised,在子对象的父属性为null,即使它们填充了$ REF正确。

When the data is deserialised, the Parent properties in the Child objects are null, even though they are populated with $ref correctly.

下面是JSON(修剪可读性)

Below is a sample of the JSON (trimmed for readability)

在键入这个问题的过程中的一个样本 - 我可能已经看到了麻烦的根源...

In the process of typing this question - I may have seen the source of the trouble...

在儿童数组属性的对象没有$类型的属性。

The objects in the "Children" array property do not have $type attributes.

这可能是因为儿童和家长的属性是通用类型T的

This could be because the Children and Parent properties are of generic type T.

请注意,实际的类型被序列化是一个派生类TemplateDataLinkedListBase的

Note that the actual type being serialised is a derived class of TemplateDataLinkedListBase

public class TemplateDataQueryElement : TemplateDataLinkedListBase<TemplateDataQueryElement>

下面是基类的摘录:

public class TemplateDataLinkedListBase<T> where T : TemplateDataLinkedListBase<T>
{
    [JsonProperty(TypeNameHandling = TypeNameHandling.Objects)]
    public T Parent { get; set; }

    [JsonProperty(TypeNameHandling=TypeNameHandling.Objects)]
    public List<T> Children { get; set; }
}



我怎么能deserialise这个JSON以这样的方式父属性?NOT NULL,并包含对父对象的引用

How can I deserialise this JSON in such a way that the Parent property is not null and contains a reference to the parent object?

    {
    "$id": "9",
    "$type": "Contracts.Models.TemplateDataQueryElement, Contracts",
    "Query": null,
    "Parent": null,
    "Children": [
      {
        "$id": "11",
        "Query": null,
        "Parent": {
          "$ref": "9"
        },
        "Children": [
          {
            "$id": "13",
            "Query": null,
            "Parent": {
              "$ref": "11"
            },
            "Children": [],
            "EntityName": "Widgets",
            "Fields": [
              "Id"
            ],
            "Key": ""
          },

下面是Pastebin链接的相关代码:

Here are PasteBin links to the relevant code:

的http:// pastebin.com/i1jxVGG3
http://pastebin.com/T1xqEWW2
http://pastebin.com/ha42SeF7
的http://引擎收录.COM / cezwZqx6
http://pastebin.com/uFbTbUZe
http://pastebin.com/sRhNQgzh

推荐答案

下面是我尝试和工作得很好:

Here is what I tried and worked fine:

的类

public class TemplateDataLinkedListBase<T> where T : TemplateDataLinkedListBase<T>
{
    [JsonProperty(TypeNameHandling = TypeNameHandling.Objects)]
    public T Parent { get; set; }

    [JsonProperty(TypeNameHandling = TypeNameHandling.Objects)]
    public List<T> Children { get; set; }
}

public class TemplateDataQueryElement : TemplateDataLinkedListBase<TemplateDataQueryElement>
{
    public string Query { get; set; }

    public TemplateDataQueryElement()
    {
        Children = new List<TemplateDataQueryElement>();
    }
}



初始化

var childLowest = new TemplateDataQueryElement
{
    Query = "Lowest"
};

var childMiddle = new TemplateDataQueryElement
{
    Query = "Middle",
    Children = new List<TemplateDataQueryElement>
    {
        childLowest
    }
};

childLowest.Parent = childMiddle;

var parent = new TemplateDataQueryElement
{
    Query = "Parent",
    Children = new List<TemplateDataQueryElement>
    {
        childMiddle
    }
};

childMiddle.Parent = parent;



序列化设置

var _jsonSettings = new JsonSerializerSettings()
{
    TypeNameHandling = TypeNameHandling.Auto,
    ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
    PreserveReferencesHandling = PreserveReferencesHandling.Objects,
    ObjectCreationHandling = ObjectCreationHandling.Auto
};



连载

var serializedStr = JsonConvert.SerializeObject(parent, Formatting.Indented, _jsonSettings);



序列化的JSON看起来是这样的:

The serialized json looks like this:

{
  "$id": "1",
  "Query": "Parent",
  "Parent": null,
  "Children": [
    {
      "$id": "2",
      "Query": "Middle",
      "Parent": {
        "$ref": "1"
      },
      "Children": [
        {
          "$id": "3",
          "Query": "Lowest",
          "Parent": {
            "$ref": "2"
          },
          "Children": []
        }
      ]
    }
  ]
}

反序列化

var deserializedStructure = JsonConvert.DeserializeObject<TemplateDataQueryElement>(serializedStr, _jsonSettings);

deserializedStructure 正确保存的参考

演示
https://开头dotnetfiddle。净/ j1Qhu6

更新1

原因我的榜样工作正常,你在其他链接发布的代码不会是因为我的类包含默认构造函数,而你没有。分析你的类,添加一个默认的构造他们,也不会断裂的功能和反序列化将是成功的正确初始化属性。所以你基本上需要做的就是添加一个默认的构造函数来两个类:

The reason my example works, and the code you posted in the additional links doesn't is because my classes contain default constructor, and yours don't. Analyzing your classes, adding a default constructor to them, it won't break the functionality and the deserialization will be successful with Parent property initialized correctly. So what you basically need to do is add a default constructor to both classes:

public class TemplateDataLinkedListBase<T> where T : TemplateDataLinkedListBase<T>
{
    [JsonProperty(TypeNameHandling = TypeNameHandling.Objects)]
    public T Parent { get; set; }

    [JsonProperty(TypeNameHandling=TypeNameHandling.Objects)]
    public List<T> Children { get; set; }
    public string EntityName { get; set; }
    public HashSet<string> Fields { get; set; }

    public string Key { get { return getKey(); } }


    public TemplateDataLinkedListBase()
    {
        Children = new List<T>();
        Fields = new HashSet<string>();
    }

    public TemplateDataLinkedListBase(string entityName)
    {
        EntityName = entityName;
        Children = new List<T>();
        Fields = new HashSet<string>();
    }

    private string getKey()
    {
        List<string> keys = new List<string>();
        keys.Add(this.EntityName);
        getParentKeys(ref keys, this);
        keys.Reverse();
        return string.Join(".", keys);

    }

    private void getParentKeys(ref List<string> keys, TemplateDataLinkedListBase<T> element)
    {
        if (element.Parent != null)
        {
            keys.Add(element.Parent.EntityName);
            getParentKeys(ref keys, element.Parent);
        }
    }

    public T AddChild(T child)
    {
        child.Parent = (T)this;
        Children.Add(child);
        return (T)this;
    }

    public T AddChildren(List<T> children)
    {
        foreach (var child in children)
        {
            child.Parent = (T)this;
        }
        Children.AddRange(children);
        return (T)this;
    }

    public void AddFields(IEnumerable<string> fields)
    {
        foreach (var field in fields)
            this.Fields.Add(field);
    }

    public TemplateDataLinkedListBase<T> Find(string searchkey)
    {
        if (this.Key == searchkey)
        {
            return this;
        }
        else
        {
            foreach (var child in Children)
            {
                if (child.Key == searchkey)
                {
                    return child;
                }
                else
                {
                    var childResult = child.Find(searchkey);
                    if (childResult != null) return childResult;
                }
            }
        }
        return null;
    }
}

public class TemplateDataQueryElement : TemplateDataLinkedListBase<TemplateDataQueryElement>, ITemplateDataQueryElement
{
    public string TemplateModelName { get; set; }
    public string RecordId { get; set; }
    public string ParentForeignKeyName { get; set; }
    public string Query { get; set; }
    public dynamic ObjectData { get; set; }
    public ITemplateDataParseResult ParseResult { get; set; }


    public TemplateDataQueryElement() : base()
    {
        Fields.Add("Id"); //Always retrieve Id's
        ObjectData = new ExpandoObject();
    }

    public TemplateDataQueryElement(string entityName)
        : base(entityName)
    {
        Fields.Add("Id"); //Always retrieve Id's
        ObjectData = new ExpandoObject();
    }

    public override string ToString()
    {
        return string.Format("{0}: {1}", EntityName, Query);
    }
}



实体名称,你通过你的构造函数中设置属性,将正确反序列化,因为它是一个公共属性。

The EntityName property which you set through your constructor, will be deserialized correctly, since it is a public property.

这篇关于JSON .NET不尊重反序列化PreserveReferencesHandling的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆