Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

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));
share|improve this answer
1  
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<Refiners>(HttpContext.Current.Server.UrlDecode(cookie.Val‌​ue)); –  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 7:21

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

Refiners refiners = serializer.Deserialize<Refiners>(Server.UrlDecode(cookie.Value));
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.