1

I have a JSON string stored in a cookie that has a & character in it:

{"Description":"The Livewire TS+ unlocks your vehicle's hidden performance by re-calibrating your vehicle's computer for Maximum Horsepower & Torque, Increased Throttle Response, Firmer Shifts and even Increased Fuel Mileage.","Sku":"SCTLWTSP","Count":1}

When I try to deserialize it, it throws an error about an illegal character. I have narrowed this down to the & character. I got around this by doing .Replace("&","%26"), but this stores the value back in the cookie with %26.

What is the best way to handle the & or any other character that might be a problem? I am trying to keep the original string (not encoded) in the cookie.

I have tried Html.Encode, but this causes more illegal character problems when deserializing and I have tried Uri.EscapeDataString, but this did nothing.

To put some context this:

When the user clicks add to cart, it adds a serialized object to a cookie. When they click add to cart again, I deserialize the JSON string so I can update the Count variable, but this is when the error happens because of the & character?

I have a custom class ShoppingCartItem, I am storing an array of ShoppingCartItem in the cookie. Here is some code:

This is when I want to read the cookie string back into a List<ShoppingCartItem>

List<ShoppingCartItem> shoppingCartItems = JsonConvert.DeserializeObject<List<ShoppingCartItem>>(shoppingCartCookie["ShoppingCartItems"]);

When I write to the cookie, I use:

shoppingCartCookie["ShoppingCartItems"] = JsonConvert.SerializeObject(shoppingCartItems);

Json array example:

ShoppingCartItems=[{"Name":"X4 Performance Programmer ","ShortDescription":"The X4 Power Flash arrives Pre-Loaded with DYNO Proven tune files that INCREASE HORSEPOWER and TORQUE! ","Sku":"SCTX4","Count":1},{"Name":"Livewire TS Plus","ShortDescription":"The Livewire TS+ unlocks your vehicle's hidden performance by re-calibrating your vehicle's computer for Maximum Horsepower & Torque, Increased Throttle Response, Firmer Shifts and even Increased Fuel Mileage.","Sku":"SCTLWTSP","Count":1}]

Here is the exact line and error:

The error happens when this line is executed:

shoppingCartItems = JsonConvert.DeserializeObject<List<ShoppingCartItem>>(shoppingCartCookie["ShoppingCartItems"]);

The error is:

Unterminated string. Expected delimiter: ". Path '[1].ShortDescription', line 1, position 359.

xaisoft
  • 3,343
  • 8
  • 44
  • 72
  • I would imagine using the same library's serialization function would give the best results for a string that deserializes to the original. – chris Dec 16 '15 at 19:11
  • @chris - I am doing that. I am calling `JsonConvert.SerializeObject` when saving the string to the cookie and when I want to get the object back out, I use `JsonConvert.DeserializeObject` – xaisoft Dec 16 '15 at 19:17
  • 1
    What _type_ are you trying to deserialize to? Please include some code. – Eren Ersönmez Dec 16 '15 at 19:17
  • @ErenErsönmez - Hi, I added some code, hopefully that helps. Thanks – xaisoft Dec 16 '15 at 19:21
  • 1
    The json you provided is a single object, but you're trying to deserialize that to a _list_ of objects? – Eren Ersönmez Dec 16 '15 at 19:24
  • @ErenErsönmez - Sorry, I just put that sample because that was the one causing the problem, I will put a better example. – xaisoft Dec 16 '15 at 19:31
  • @ErenErsönmez - I updated the post with an array that is in my cookie – xaisoft Dec 16 '15 at 19:41
  • @xaisoft, I see. AFAICT, & is valid in a JSON string without being escaped. For me, `JsonConvert.DeserializeObject(JsonConvert.SerializeObject("&"))` produces `"&"`. – chris Dec 16 '15 at 19:47
  • @chris - When I serialize the shopping cart item for the first time, it works, but when I want to add it a second time and deserialize the items first that contain the &, I get the error. It only goes away when I remove the & or replace it. – xaisoft Dec 16 '15 at 19:49
  • @xaisoft please provide the line of code you get the error on. And the exact error message. It usually helps if you can provide a tiny program that demonstrates your issue. Certainly, the error you're getting doesn't have anything to do with json serialization -- & is nothing special as far as json is concerned. – Eren Ersönmez Dec 16 '15 at 19:55
  • Yes, a [mcve] would go a long way. – chris Dec 16 '15 at 19:56

1 Answers1

1

I think, most likely, your issue isn't directly related to json serialization, but it is related to the fact that & isn't a valid character to put in a cookie (see this answer).

You will have to use HttpUtility.UrlEncode and HttpUtility.UrlDecode. E.g.:

var json = JsonConvert.SerializeObject(myList);
var cookieString = HttpUtility.UrlEncode(json);
// write cookie
...
// later when reading the cookie
var cookieString = ...
var json = HttpUtility.UrlDecode(cookieString);
var myList = JsonConvert.DeserializeObject<List<ShoppingCartItem>>(json); 
Community
  • 1
  • 1
Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
  • Hi, I think you may be right, I haven't tested your solution out yet, but I did notice when I added other key/value pairs in my cookie, they were separated by the &, so it does seem like a valid character, but I wonder if having it in my JSON string, it is being treated like a separator instead of the literal character. I have updated my code with the exact line of the error and the error message. I will try your solution and let you know how it goes. Thanks again. – xaisoft Dec 16 '15 at 22:16