I have a class that derives from a list, and no matter what I do, I cannot find where JSON.NET is setting the value. It appears to me that even properties marked are not firing.
I have set breakpoints at all the following areas:
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;
}
But none of these have triggered a break when running deserialization.
The deserialization process is relatively straightforward, using this:
public T GetJObject<T>(string cookieName, JsonSerializerSettings jset = null)
{
string cookieContent = Get(cookieName);
return JsonConvert.DeserializeObject<T>(cookieContent, jset);
}
And the converter class is also straightforward:
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)
{
}
}
Even areas marked with JsonProperty in other classes are not being hit on deserialization.
[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;
}
}
So how does JSON.NET treat setters and lists? Obviously, neither are being hit? Can I force it to hit the setter logic?
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":[]}]}]