Json.Net在列表反序列化过程中调用属性getter,导致重复项 [英] Json.Net calls property getter during deserialization of list, resulting in duplicate items

查看:16
本文介绍了Json.Net在列表反序列化过程中调用属性getter,导致重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 json.net 为 winform 应用程序实现 memento 模式.我正在使用备忘录回滚失败的数据库事务中的对象.我遇到的问题是,在反序列化备忘录时,调用的是 getter 而不是 setter.让我演示一下:

I'm using json.net to implement the memento pattern for a winform application. I'm using the memento to rollback an object on a failed database transaction. The problem I'm getting is that when deserializing the memento, the getter is called rather than the setter. Let me demonstrate:

class MyClass
{
    public int ID { get; set; }
    public string field1 { get; set; }
    public string field2 { get; set; }

    private List<SomeObject> _someObjects;
    public List<SomeObject> SomeObjects
    {
        get
        {
            if(_someObjects == null)
            {
                _someObjects = LoadSomeObjectsFromDB();
            }
            return _someObjects;
        }
        set
        {
            _someObjects = value;
        }
    }

    private List<AnotherObject> _anotherObjects;
    public List<AnotherObject> AnotherObjects
    {
        get
        {
            if(_anotherObjects == null)
            {
                _anotherObjects= LoadAnotherObjectsFromDB();
            }
            return _anotherObjects ;
        }
        set
        {
            _anotherObjects = value;
        }
    }
}

*MyObjectSomeObjectAnotherObject 扩展同一个基类 Model

*MyObject, SomeObject, and AnotherObject extend the same base class, Model

从上面的示例类可以看出,如果 SomeObjects 尚未加载,它使用 延迟加载 从数据库中加载它.现在当我序列化这个对象时.

As you can see from the sample class above, if SomeObjects is not loaded yet, it uses Lazy Loading to load it from the Database. Now When I serialize this object.

string memento = JsonConvert.SerializeObject(obj);

导致

{
  "ID": 1,
  "field1": "field 1",
  "field2": "field 2",
  "SomeObjects": [
    {
      "ID": 1,
    },
    {
      "ID": 2,
    },
    {
      "ID": 3,
    }
  ],
  "AnotherObjects": [
    {
      "ID": 4,
    },
    {
      "ID": 5,
    },
    {
      "ID": 6,
    }
  ]
}

然后反序列化它.

MyObject obj = JsonConvert.DeserializeObject(memento, typeof(MyObject));

我得到一个由以下 JSON 表示的对象

I get an object represented by the following JSON

{
  "ID": 1,
  "field1": "field 1",
  "field2": "field 2",
  "SomeObjects": [
    {
      "ID": 1,
    },
    {
      "ID": 2,
    },
    {
      "ID": 3,
    },
    {
      "ID": 1,
    },
    {
      "ID": 2,
    },
    {
      "ID": 3,
    }
  ],
  "AnotherObjects": [
    {
      "ID": 4,
    },
    {
      "ID": 5,
    },
    {
      "ID": 6,
    },
    {
      "ID": 4,
    },
    {
      "ID": 5,
    },
    {
      "ID": 6,
    }
  ]
}

SomeObjectsAnotherObjects Lists

SomeObjects getter 被调用,这会导致 List 从 DB 初始化,然后将序列化的列表附加到它.让我在列表中留下比预期更多的项目.但是如果我删除延迟加载部分

SomeObjects getter is called, which causes the List to initialize from the DB then the serialized list is Appended to it. Leaving me with more items in the list than desired. But if I remove the Lazy Loading portion

public List<SomeObject> SomeObjects
{
    get
    {
        /*if(_someObjects == null)
        {
            _someObjects = LoadSomeObjectsFromDB();
        }*/
        return _someObjects;
    }
    set
    {
        _someObjects = value;
    }
}

getter 仍然被调用,但返回 null.然后 json.net 调用 setter,其中 value 是一个列表,其中已经添加了正确数量的元素,而如果 getter 返回一个初始化列表,它会附加到它永远不会调用这个 setter.为什么存在差异,如果列表已经初始化,则调用 getter 并将其附加到.但如果未初始化,则调用 setter 并使用已填充对象的列表对其进行初始化.有没有办法让 json.net 只在通用 List 的反序列化期间调用 setter?

The getter is still called, but returns null. Then json.net calls the setter with value being a list with the correct number of elements already added, whereas if the getter returns an initalized list, it appends to it never calling ths setter. Why the discrepancy, if a list is already initialized, the getter is called and it is appended to. But if not initialized, the setter is called and it is initialized with a list already filled with objects. Is there a way to make json.net only call the setter during deserialization of a generic List?

推荐答案

Json.Net 有一个 ObjectCreationHandling 设置用于此目的.尝试将其设置为 Replace,例如:

Json.Net has an ObjectCreationHandling setting for this purpose. Try setting it to Replace, e.g.:

JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ObjectCreationHandling = ObjectCreationHandling.Replace;

MyObject obj = JsonConvert.DeserializeObject<MyObject>(memento, settings);

演示:https://dotnetfiddle.net/rkY1pt

这篇关于Json.Net在列表反序列化过程中调用属性getter,导致重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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