5

I'm using JQuery.Cookie to store a javascript object as a cookie value:

    var refinerData = {};
// Read in the current cookie if it exists:
if ($.cookie('RefinerData') != null) {
    refinerData = JSON.parse($.cookie('RefinerData'));
}
// Set new values based on the category and filter passed in
switch(category)
{
    case "topic":
        refinerData.Topic = filterVal;
        break;
    case "class":
        refinerData.ClassName = filterVal;
        break;
    case "loc":
        refinerData.Location = filterVal;
        break;
}    
// Save the cookie:
$.cookie('RefinerData', JSON.stringify(refinerData), { expires: 1, path: '/' });

When I debug in firebug, the value of the cookie value seems to be formatted correctly:

{"Topic":"Disease Prevention and Management","Location":"Hatchery Hill Clinic","ClassName":"I have Diabetes, What can I Eat?"}

I'm writing a SharePoint web part in C# that reads the cookie in and parses it:

        protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies["RefinerData"];
        if (cookie != null)
        {
            string val = cookie.Value;
            // Deserialize JSON cookie:
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            var refiners = serializer.Deserialize<Refiners>(cookie.Value);
           output.AppendLine("Deserialized Topic = " + refiners.Topic);
            output.AppendLine("Cookie exists: " + val);
        }
    }

I have a Refiners class for serializing to:

    public class Refiners
{
    public string Topic { get; set; }
    public string ClassName { get; set; }
    public string Location { get; set; }
}   

However, this code throws an "Invalid JSON Primitive" error. I can't figure out why this isn't working. One possibility is that its not reading the cookie correctly. When I print out the value of the cookie as a string, I get:

%7B%22Topic%22%3A%22Disease%20Prevention%20and%20Management%22%2C%22Class%22%3A%22Childbirth%20%26%20Parenting%202013%22%2C%22Location%22%3A%22GHC%20East%20Clinic%22%7D

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
user1231748
  • 95
  • 1
  • 4

2 Answers2

13

Appears URL encoded, try decoding the value using the UrlDecode method of the HtmlUtility (of which an instance is exposed by a page through the Server property):

var refiners = serializer.Deserialize<Refiners>(Server.UrlDecode(cookie.Value));
Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • 2
    Yes THANK YOU! that worked. I had to make a slight adjustment to access Server.UrlDecode since its an Share Point webpart, but it worked wonderfully: var refiners = serializer.Deserialize(HttpContext.Current.Server.UrlDecode(cookie.Value)); – user1231748 Jul 17 '13 at 18:44
  • @user1231748 Of course, I should have given you that detail to access the static reference, my bad. Glad you got it going. – Grant Thomas Jul 18 '13 at 07:21
  • @user1231748 If answer works for you - feel free to mark it as accepted one (checkmark to the left). – Guru Stron Aug 01 '23 at 11:28
1

I think you need to decode the cookie prior to deserialization. Try using;

Refiners refiners = serializer.Deserialize<Refiners>(Server.UrlDecode(cookie.Value));
evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115