Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Invalid JSON Primitive Reading from Cookie

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

like image 398
user1231748 Avatar asked Jul 17 '13 18:07

user1231748


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

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));
like image 153
Grant Thomas Avatar answered Sep 27 '22 19:09

Grant Thomas


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

Refiners refiners = serializer.Deserialize<Refiners>(Server.UrlDecode(cookie.Value));
like image 21
evanmcdonnal Avatar answered Sep 27 '22 18:09

evanmcdonnal