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.

4
  • 1
    Are you expecting the object to always be an instance of ShoppingCart? If so, I think explicitly converting the type, ie Json.Decode<List<ShoppingCart>>(value), should solve the problem. Commented Jun 11, 2015 at 3: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<ShoppingCart>). Can you post your comment as an answer? I'd like to accept it so you get credit. Commented Jun 11, 2015 at 4:03
  • Thanks gnack. I posted the answer, glad I could help :) Commented Jun 11, 2015 at 4: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 Commented Jun 11, 2015 at 4:17

2 Answers 2

2

You need to explicitly convert the type, ie

return Json.Decode<ShoppingCart>(value);
Sign up to request clarification or add additional context in comments.

Comments

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#

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.