3

I'm serialising an object into a JSON string in order to store it in a cookie. The object looks like this:

public class ShoppingCart
{
    public List<ShoppingCartItem> Items { get; set; }

    public ShoppingCart()
    {
        Items = new List<ShoppingCartItem>();
    }
}

public class ShoppingCartItem
{
    public enum ShoppingCartItemType
    {
        TypeOfItem,
        AnotherTypeOfItem
    }
    public int Identifier { get; set; }
    public ShoppingCartItemType Type { get; set; }
}

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 ShoppingCart, with its items correctly deserialised.

This is the code I'm using to do this:

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

}

Note that storing the cookie works beautifully. It has the correct name, and the JSON string looks good to me; here's an example:

{"Items":[{"Identifier":1,"Type":1}]}

The problem is, when I try to deserialise this, I think it's not recognising that the array is actually a List<ShoppingCartItem> and so I end up with an instance of the ShoppingCart class with the Items property set to an empty List<ShoppingCartItem>.

Does anyone know how I can make this work? I'd prefer to continue using the standard System.Web.Helpers.Json for this if possible, but if a more robust JSON serialiser is required I'm willing to do that.

Nick Coad
  • 3,623
  • 4
  • 30
  • 63
  • 1
    Are you expecting the object to always be an instance of `ShoppingCart`? If so, I think explicitly converting the type, ie `Json.Decode>(value)`, should solve the problem. – Mark Miller Jun 11 '15 at 03:58
  • @MarkM I was able to use your comment as an answer to this, thank you so much! I changed my GetCookie method to accept a type parameter and run the decode with that same type. (The type needed, by the way, was `ShoppingCart`, not `List`). Can you post your comment as an answer? I'd like to accept it so you get credit. – Nick Coad Jun 11 '15 at 04:03
  • Thanks gnack. I posted the answer, glad I could help :) – Mark Miller Jun 11 '15 at 04:05
  • Could you please paste the exact Json you are getting which needs to be de-serialized in an object, I wonder if the Json is correct then why would it not transformed into object, btw, which Json serializer you are using and are you using Web API controllers – Mrinal Kamboj Jun 11 '15 at 04:17

2 Answers2

2

You need to explicitly convert the type, ie

return Json.Decode<ShoppingCart>(value);
Mark Miller
  • 7,442
  • 2
  • 16
  • 22
2

Similar question has been asked earlier, Using Newtonsoft JsonConvert.DeserializeObject works well for the purpose, check the following links. You may even consider Javascriptserializer

How to convert Json array to list of objects in c#

Deserialize JSON array(or list) in C#

deserialize json array list in c#

Community
  • 1
  • 1
Mrinal Kamboj
  • 11,300
  • 5
  • 40
  • 74