CSharp · Console
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Runtime.Serialization;
using System.Data;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections.Specialized;
using System.Web.Routing;
using System.Net.Mail;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Net;
using System.Runtime.Serialization.Formatters;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System.Web.SessionState;
// https://stackoverflow.com/questions/42723879/intercept-list-population-to-assign-values-in-deserialization
public class CartItems : Collection<CartItem>
{
public CartItems() : base() { }
public CartItems(CartItem parent) : this()
{
this.Parent = parent;
}
public CartItem Parent { get; private set; }
protected override void RemoveItem(int index)
{
CartItem oldItem = null;
if (index >= 0 && index < Count)
{
oldItem = this[index];
}
base.RemoveItem(index);
}
protected override void InsertItem(int index, CartItem item)
{
base.InsertItem(index, item);
if (item != null)
item.Parent = this;
}
protected override void SetItem(int index, CartItem item)
{
CartItem oldItem = null;
if (index >= 0 && index < Count)
{
oldItem = this[index];
}
base.SetItem(index, item);
if (oldItem != null)
oldItem.Parent = null;
if (item != null)
item.Parent = this;
}
protected override void ClearItems()
{
foreach (var item in this)
if (item != null)
item.Parent = null;
base.ClearItems();
}
}
public class CartItem
{
public CartItem()
{
this.Children = new CartItems(this);
}
public int t { get; set; }
public int i { get; set; }
public int n { get; set; }
public bool r { get; set; }
[JsonProperty("c")]
public CartItems Children { get; private set; }
[JsonIgnore]
public CartItems Parent { get; set; }
}
public static class CartItemExtensions
{
public static IEnumerable<CartItem> DescendantsAndSelf(this CartItem item)
{
if (item == null)
yield break;
yield return item;
foreach (var child in item.Children)
yield return child;
}
public static IEnumerable<CartItem> DescendantsAndSelf(this IEnumerable<CartItem> items)
{
if (items == null)
return Enumerable.Empty<CartItem>();
return items.SelectMany(i => i.DescendantsAndSelf());
}
}
class TestClass
{
static string GetJson()
{
var 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"":[]}]}]";
return json;
}
internal static void Test()
{
var cookieContent = GetJson();
var root = JsonConvert.DeserializeObject<CartItems>(cookieContent);
Console.WriteLine("Re-serialized JSON: ");
Console.WriteLine(JsonConvert.SerializeObject(root, Formatting.Indented));
foreach (var parent in root.DescendantsAndSelf())
{
Assert.IsTrue(parent.Children.Parent == parent);
foreach (var child in parent.Children)
Assert.IsTrue(child.Parent == parent.Children);
}
// Verify that Parent as set as items are removed and added
var oldItem = root[0];
var newItem = root[0] = new CartItem();
Assert.IsTrue(oldItem.Parent == null);
Assert.IsTrue(root[0].Parent == root);
root.Clear();
Assert.IsTrue(newItem.Parent == null);
Console.WriteLine("All parent relationships are set correctly.");
}
}
public class AssertionFailedException : System.Exception
{
public AssertionFailedException() : base() { }
public AssertionFailedException(string s) : base(s) { }
}
public static class Assert
{
public static void IsTrue(bool value)
{
if (value == false)
throw new AssertionFailedException("failed");
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("");
TestClass.Test();
}
}