<small id='XV8wC'></small><noframes id='XV8wC'>

      <tfoot id='XV8wC'></tfoot>
      <i id='XV8wC'><tr id='XV8wC'><dt id='XV8wC'><q id='XV8wC'><span id='XV8wC'><b id='XV8wC'><form id='XV8wC'><ins id='XV8wC'></ins><ul id='XV8wC'></ul><sub id='XV8wC'></sub></form><legend id='XV8wC'></legend><bdo id='XV8wC'><pre id='XV8wC'><center id='XV8wC'></center></pre></bdo></b><th id='XV8wC'></th></span></q></dt></tr></i><div id='XV8wC'><tfoot id='XV8wC'></tfoot><dl id='XV8wC'><fieldset id='XV8wC'></fieldset></dl></div>
        <bdo id='XV8wC'></bdo><ul id='XV8wC'></ul>
      <legend id='XV8wC'><style id='XV8wC'><dir id='XV8wC'><q id='XV8wC'></q></dir></style></legend>

      1. Json.Net在列表反序列化过程中调用属性getter,导致重复项

        时间:2023-05-22

          1. <tfoot id='yGRCL'></tfoot>
              <bdo id='yGRCL'></bdo><ul id='yGRCL'></ul>
              <i id='yGRCL'><tr id='yGRCL'><dt id='yGRCL'><q id='yGRCL'><span id='yGRCL'><b id='yGRCL'><form id='yGRCL'><ins id='yGRCL'></ins><ul id='yGRCL'></ul><sub id='yGRCL'></sub></form><legend id='yGRCL'></legend><bdo id='yGRCL'><pre id='yGRCL'><center id='yGRCL'></center></pre></bdo></b><th id='yGRCL'></th></span></q></dt></tr></i><div id='yGRCL'><tfoot id='yGRCL'></tfoot><dl id='yGRCL'><fieldset id='yGRCL'></fieldset></dl></div>
                <tbody id='yGRCL'></tbody>
            • <legend id='yGRCL'><style id='yGRCL'><dir id='yGRCL'><q id='yGRCL'></q></dir></style></legend>

                <small id='yGRCL'></small><noframes id='yGRCL'>

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

                  问题描述

                  限时送ChatGPT账号..

                  我正在使用 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,导致重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Json.net 将数字属性序列化为字符串 下一篇:序列化无符号整数 (ulong) 数组时 Json.NET 崩溃

                  相关文章

                  <small id='1lFfY'></small><noframes id='1lFfY'>

                • <tfoot id='1lFfY'></tfoot>

                • <legend id='1lFfY'><style id='1lFfY'><dir id='1lFfY'><q id='1lFfY'></q></dir></style></legend>
                • <i id='1lFfY'><tr id='1lFfY'><dt id='1lFfY'><q id='1lFfY'><span id='1lFfY'><b id='1lFfY'><form id='1lFfY'><ins id='1lFfY'></ins><ul id='1lFfY'></ul><sub id='1lFfY'></sub></form><legend id='1lFfY'></legend><bdo id='1lFfY'><pre id='1lFfY'><center id='1lFfY'></center></pre></bdo></b><th id='1lFfY'></th></span></q></dt></tr></i><div id='1lFfY'><tfoot id='1lFfY'></tfoot><dl id='1lFfY'><fieldset id='1lFfY'></fieldset></dl></div>

                      • <bdo id='1lFfY'></bdo><ul id='1lFfY'></ul>