反序列化包含Dictionary参数的对象会导致字典成为空对象

时间:2018-06-16 06:19:00

标签: c# json dictionary json.net

这个json对象包含一个Person对象列表,每个对象都有一个字典。

{
  "$type": "PeopleDataObject, Assembly-CSharp",
  "People": {
    "$type": "System.Collections.Generic.List`1[[Person, Assembly-CSharp]], 
                                                                 mscorlib",
    "$values": [
      {
        "$type": "Person, Assembly-CSharp",
        "Id": 0,
        "FirstName": "Daffy",
        "LastName": "Duck",
        "Age": 0,
        "Sex": true,
        "Stats": {
          "$type": "System.Collections.Generic.Dictionary`2[[System.String, 
                            mscorlib],[System.Int32, mscorlib]], mscorlib",
          "str": 9,
          "dex": 13,
          "con": 9,
          "int": 13,
          "wis": 10,
          "cha": 8
        }
      },
      {
        "$type": "Person, Assembly-CSharp",
        "Id": 1,
        "FirstName": "Wilma",
        "LastName": "Flintstone",
        "Age": 0,
        "Sex": false,
        "Stats": {
          "$type": "System.Collections.Generic.Dictionary`2[[System.String, 
            mscorlib],[System.Int32, mscorlib]], mscorlib",
          "str": 7,
          "dex": 9,
          "con": 9,
          "int": 12,
          "wis": 12,
          "cha": 12
        }
      }
    ]
  },
  "Count": 2
}

我检查了读取的字符串是不是通过将其写入另一个文件而出错,所以我知道所有内容都正确序列化但是在反序列化时字典丢失了,所以当我去使用它或者再次保存时它们'重新。调试器将其标识为空对象。下面的代码遵循一般对象的文档,但我找不到包含字典的对象的特定情况。我确保包含TypeNameHandling,以便确保它知道反序列化为字典,但我没有得到该结果。

private void LoadRoster()
{
    string path = Path.Combine(Application.streamingAssetsPath, "PeopleData.json");
    string debug = Path.Combine(Application.streamingAssetsPath, "debug.json");
    if (!File.Exists(path))
    {
        Debug.Log("NO SAVE FILE RECOGNISED");
    }
    else
    {
        PeopleDataObject allPeople = PeopleDataObject.Instance;
        string jsonString = File.ReadAllText(path);

        //check that the file is read correctly
        File.WriteAllText(debug, jsonString);

        allPeople = JsonConvert.DeserializeObject<PeopleDataObject>(jsonString, new JsonSerializerSettings
        {
            TypeNameHandling = TypeNameHandling.All
        });
        People = allPeople.People;
        Person.AllTimeCount = allPeople.Count;
        Debug.Log("Loaded");
    }
}

编辑:它正在尝试反序列化到以下PeopleDataObject类,该类本身包含Person列表,我将在下面提供。

PeopleDataObject:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PeopleDataObject
{
    //<Singleton Boilerplate>
    private static PeopleDataObject instance = null;
    private static readonly object padlock = new object();

    PeopleDataObject() {}

    public static PeopleDataObject Instance
    {
        get
        {
            lock(padlock)
            {
                if (instance == null)
                {
                    instance = new PeopleDataObject();
                }
                return instance;
            }
        }
    }
    //</Singletone Boilerplate>

    public List<Person> People { get; set; }
    public int Count { get; set; }
}

人:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Person
{

    public int Id { get; private set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; private set; }
    public bool Sex { get; private set; } //true = male, false = female
    public Dictionary<string, int> Stats { get; private set; }

    public static int AllTimeCount { get; set; }

    private GameObject messageController;

    public Person(string first, string last, bool sex, GameObject controller)
    {
        Debug.Log(" overload 1");
        FirstName = first;
        LastName = last;
        Age = 0;
        Sex = sex;
        Id = AllTimeCount;
        AllTimeCount++;

        if (controller != null)
            messageController = controller;
        else
            messageController = GameObject.Find("PersonManager");

        //if sex is true (male) colour modifier is blue, else colour modifier is pink
        string colourMod = sex ? "#4286f4" : "#ff56ff";

        Notification(string.Format("<{0}>{1} {2}</color> was born. ", colourMod, first, last));

    }

    public Person(int id, string first, string last, bool sex, Dictionary<string, int> stats)
    {
        Debug.Log("overload 2");
        Id = id;
        FirstName = first;
        LastName = last;
        Sex = sex;
        Stats = stats;
    }

    public Person()
    {
        Debug.Log("overload 3");
    }

    public void SetStats(Dictionary<string, int> s)
    {
        Stats = s;
    }

    public void IncrementAge()
    {
        Age++;
    }

    public void Notification(string message)
    {
        MessageController controller = messageController.GetComponent<MessageController>();
        controller.PostMessage(message);
    }
}

顺便说一下,我知道当使用构造函数2或3时,Person中的messageController为null,但这不是我现在关注的问题,它与当前问题无关,但可以看到它。

编辑编辑:尝试过TypeHandling.Auto而不是TypeHandling.All,它没有效果。

我已经坚持了一段时间。我已经知道我是个白痴,但我非常感谢能帮助告诉我为什么我是个白痴。

干杯, 丹

1 个答案:

答案 0 :(得分:0)

答案证明是我的财产的可访问性。

我在年龄,性别和统计数据上使用{ get; private set; }因为我认为最佳做法是控制可访问性但我应该使用{ get; set }以便json库有权将值写入反序列化的对象。

这对字典来说更为明显,因为Age int默认为零,而我所有的测试人员都是0岁的婴儿;性别默认为假(女性),我只是没有发现;字典默认为null,你丢失了很多数据,所以它是我注意到的症状。