title: How to convert a JSON array into a C# List?
body: <p>I'm serialising an object into a JSON string in order to store it in a cookie.  The object looks like this:</p>
  
  <pre><code>public class ShoppingCart
  {
      public List&lt;ShoppingCartItem&gt; Items { get; set; }
  
      public ShoppingCart()
      {
          Items = new List&lt;ShoppingCartItem&gt;();
      }
  }
  
  public class ShoppingCartItem
  {
      public enum ShoppingCartItemType
      {
          TypeOfItem,
          AnotherTypeOfItem
      }
      public int Identifier { get; set; }
      public ShoppingCartItemType Type { get; set; }
  }
  </code></pre>
  
  <p>I would then like to be able to retrieve the object from the cookie as a JSON string, and decode it back into an object of type <code>ShoppingCart</code>, with its items correctly deserialised.</p>
  
  <p>This is the code I'm using to do this:</p>
  
  <pre><code>public class CookieStore
  {
      public static void SetCookie(string key, object value, TimeSpan expires)
      {
          string valueToStore = Json.Encode(value);
          HttpCookie cookie = new HttpCookie(key, valueToStore);
  
          if (HttpContext.Current.Request.Cookies[key] != null)
          {
              var cookieOld = HttpContext.Current.Request.Cookies[key];
              cookieOld.Expires = DateTime.Now.Add(expires);
              cookieOld.Value = cookie.Value;
              HttpContext.Current.Response.Cookies.Add(cookieOld);
          }
          else
          {
              cookie.Expires = DateTime.Now.Add(expires);
              HttpContext.Current.Response.Cookies.Add(cookie);
          }
       }
      public static object GetCookie(string key)
      {
          string value = string.Empty;
          HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
  
          if (cookie != null)
          {
              value = cookie.Value;
          }
          return Json.Decode(value);
      }
  
  }
  </code></pre>
  
  <p>Note that storing the cookie works beautifully. It has the correct name, and the JSON string looks good to me; here's an example:</p>
  
  <pre><code>{"Items":[{"Identifier":1,"Type":1}]}
  </code></pre>
  
  <p>The problem is, when I try to deserialise this, I think it's not recognising that the array is actually a <code>List&lt;ShoppingCartItem&gt;</code> and so I end up with an instance of the <code>ShoppingCart</code> class with the Items property set to an empty <code>List&lt;ShoppingCartItem&gt;</code>.</p>
  
  <p>Does anyone know how I can make this work?  I'd prefer to continue using the standard <code>System.Web.Helpers.Json</code> for this if possible, but if a more robust JSON serialiser is required I'm willing to do that.</p>