2017-03-09 32 views
0

我有一个派生自列表的类,无论我做什么,我都找不到JSON.NET设置值的位置。在我看来,即使是标记的属性也不会触发。JSON.NET反序列化不触发setter

我在所有以下方面设置断点:

public CartItem Add(int id, int numItems, CARTITEMTYPE type = CARTITEMTYPE.GENERIC) 
    { 
     var item = NewByType(type); 
     item.ID = id; 
     item.NumItems = numItems; 
     return this.Add(item); 
    } 

    public new CartItem Add(CartItem item) 
    { 
     item.Parent = this; 
     base.Add(item); 
     return item; 
    } 

    public new void AddRange(IEnumerable<CartItem> items) 
    { 
     items.Any(f => { f.Parent = this; return true; }); 
     base.AddRange(items); 
    } 

    public new void InsertRange(int index, IEnumerable<CartItem> items) 
    { 
     items.Any(f => { f.Parent = this; return true; }); 
     base.InsertRange(index, items); 
    } 

    public new CartItem Insert(int index, CartItem item) 
    { 
     item.Parent = this; 
     base.Insert(index,item); 
     return item; 
    } 

但这些都没有运行反序列化时触发中断。

反序列化过程相对简单,使用这样的:

public T GetJObject<T>(string cookieName, JsonSerializerSettings jset = null) 
    { 
     string cookieContent = Get(cookieName); 
     return JsonConvert.DeserializeObject<T>(cookieContent, jset); 
    } 

和转换器类也很简单:

public class JsonCartConverter : JsonConverter 
{ 
    public override bool CanConvert(Type objectType) 
    { 
     return typeof(CartItem).IsAssignableFrom(objectType); 
    } 

    public override bool CanWrite 
    { 
     get 
     { 
      return false; 
     } 
    } 

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
    { 
     JObject obj = JObject.Load(reader); 

     var type = obj["t"] != null ? (CARTITEMTYPE)obj["t"].Value<int>() : CARTITEMTYPE.GENERIC; 
     var item = CartItems.NewByType(type); 
     serializer.Populate(obj.CreateReader(), item); 
     return item; 
    } 


    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 

    } 
} 

即使区域标有JsonProperty其他类不被上反序列化打。

[JsonProperty("c")] 
    public CartItems Children 
    { 
     get 
     { 
      if (_Children == null) _Children = new CartItems(); 
      if (_Children.Parent == null) _Children.Parent = this; 
      _Children.SiteID = SiteID; 
      _Children.UserID = UserID; 
      return _Children; 
     } 
     set 
     { 
      Children.Parent = this; 
      Console.WriteLine("we set the parent"); 
      _Children = value; 
     } 
    } 

那么JSON.NET如何对待setter和列表呢?显然,没有被击中?我能强制它达到setter逻辑吗?

编辑:JSON

[{"t":1,"i":1,"n":4,"r":false,"c":[{"t":5,"i":2,"n":4,"r":false,"c":[]}]},{"t":1,"i":3,"n":4,"r":false,"c":[{"t":5,"i":4,"n":4,"r":false,"c":[{"t":4,"i":6,"n":14,"r":false,"c":[]}]},{"t":1,"i":5,"n":15,"r":false,"c":[]}]}] 
+0

因此,它是击中任何这些:第二个问题是由DBC在这里找到答案?通过检查所得到的班级,看起来最后一个例子的制定者没有受到影响?我从来没有看到父母得到设置... – dudeinco

+0

它是否使用反射来绕过setter逻辑,并通过将值强制为私有属性完全在标准的类中添加/插入功能? – dudeinco

+0

JSON.NET将不会在您的对象上调用任何“添加/插入”方法。 –

回答

0

我有它附着在CartItem类(在OP最后一个例子)的孩子的方式回答。它永远不会调用setter,因为它使用getter捕获类,然后从那里填充它。 ...当然,子类的父类必须具有附加的属性[JsonIgnore]。

我仍然不知道如何拦截之类的填充设置其孩子的父母:

Public CartItems : List<CartItem> 

在反序列化设置孩子的Parent属性,但是这是一个出两个答案。

编辑:

Intercept list population to assign values in deserialization

+0

是的,这是正确的,如果集合是预先分配的,它永远不会退后。请参阅[使用.NET Newtonsoft.Json组件反序列化一些有效的json时,为什么我的POCO中的所有集合都为null](https://stackoverflow.com/questions/32491966/why-are-all-the-collections-in -my-poco-are-null-when-deserializing-some-valid-js)的相关问题。 – dbc