2017-03-09 9 views
0

私はリストから派生したクラスを持っています。何をしても、JSON.NETが値を設定している場所を見つけることができません。マークされたプロパティさえも発火していないようです。JSON.NET逆シリアル化がセッターを起動していません

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); 
    } 

およびコンバータクラスも簡単です。他のクラスにJsonPropertyが付い

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("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はセッターとリストをどのように扱いますか?明らかに、どちらもヒットしていませんか?私はそれがセッターロジックに当たってもいいのですか?

EDIT: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

は、だから、これらのいずれかに当たっていますか?結果のクラスを調べることで、最後の例のセッターがヒットしたように見えることはありませんか?私は親が設定されるのを見たことはありません... – dudeinco

+0

リフレクションを使用して、セッターロジックをバイパスし、プライベートプロパティに値を強制することによって、クラスの標準的な追加/挿入機能を完全に使用しますか? – dudeinco

+0

JSON.NETは、オブジェクトの「追加/挿入」メソッドを起動しません。 –

答えて

0

は、私はそれがCartItemクラスの子供たち(OPの最後の一例)を付加する方法のための答えを持っています。これはセッターを呼び出すことはありません。ゲッターを使用してクラスを取得してから、そこからデータを取り込むためです。もちろん、子クラスの親には属性[JsonIgnore]が添付されていなければなりません。

私はまだその子の親を設定するには、クラスの移入を傍受するかどうかはわかりません:子供の親プロパティを設定するには、逆シリアル化の

Public CartItems : List<CartItem> 

が、これは1つのアウトです2つの答え。

編集:第二の問題は、ここでは、DBCで答えた:

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-null-des-deserializing-some-valid-js)を指定します。 – dbc

関連する問題