[SOLVED] JSON stored in cookie is read differently in a different session.

Answered
0
0

This bug took a while to track down and appears in WiseJ 2.0.28.0. I have a settings class that I am trying to store via a cookie, and using JSON serialization/deserialization.

Writing the JSON string to a cookie the first time looks like this (Visual Studio watch):

"{""cboIssuesDepartmentSelectedIndex"":0,""txtIssuesText"":"""",""txtIssuesAutoCompleteText"":[""""],""txtStaffSearchText"":"""",""txtStaffSearchAutoCompleteText"":[""1"",""2"",""test"",""3"",""4""],""cboStaffDeptFilterSelectedIndex"":-1,""cboIssuesFilterSelectedIndex"":1,""cboIssuesStatusSelectedIndex"":0,""cboIssuesTypeSelectedIndex"":0,""splitContainer1SplitterDistance"":1410,""scListViewMapSplitContainerSplitterDistance"":631}"

After saving the cookie, you can see the JSON is stored in the browser like this, double quotes were escaped: (Chrome Cookie Viewer)

{\"cboIssuesDepartmentSelectedIndex\":0,\"txtIssuesText\":\"\",\"txtIssuesAutoCompleteText\":[\"\"],\"txtStaffSearchText\":\"\",\"txtStaffSearchAutoCompleteText\":[\"1\",\"2\",\"test\",\"3\",\"4\"],\"cboStaffDeptFilterSelectedIndex\":-1,\"cboIssuesFilterSelectedIndex\":1,\"cboIssuesStatusSelectedIndex\":0,\"cboIssuesTypeSelectedIndex\":0,\"splitContainer1SplitterDistance\":1410,\"scListViewMapSplitContainerSplitterDistance\":631}

If we load that cookie again in the same session, it un-escapes the quotes, and is read just the way it was saved: (Visual Studio watch)

"{""cboIssuesDepartmentSelectedIndex"":0,""txtIssuesText"":"""",""txtIssuesAutoCompleteText"":[""""],""txtStaffSearchText"":"""",""txtStaffSearchAutoCompleteText"":[""1"",""2"",""test"",""3"",""4""],""cboStaffDeptFilterSelectedIndex"":-1,""cboIssuesFilterSelectedIndex"":1,""cboIssuesStatusSelectedIndex"":0,""cboIssuesTypeSelectedIndex"":0,""splitContainer1SplitterDistance"":1410,""scListViewMapSplitContainerSplitterDistance"":631}"

So far so good. This read/write/read cycle can be done over and over again successfully.

However, after rebuilding/restarting the session, the cookie is read like this:

"{\""cboIssuesDepartmentSelectedIndex\"":0,\""txtIssuesText\"":\""\"",\""txtIssuesAutoCompleteText\"":[\""\""],\""txtStaffSearchText\"":\""\"",\""txtStaffSearchAutoCompleteText\"":[\""1\"",\""2\"",\""test\"",\""3\"",\""4\""],\""cboStaffDeptFilterSelectedIndex\"":-1,\""cboIssuesFilterSelectedIndex\"":1,\""cboIssuesStatusSelectedIndex\"":0,\""cboIssuesTypeSelectedIndex\"":0,\""splitContainer1SplitterDistance\"":1410,\""scListViewMapSplitContainerSplitterDistance\"":631}"

and throws a JSON deserialization error.  Notice the first few characters are different.

Newtonsoft JSON.NET error message is Invalid Property identifier character: \. Path”, line 1, position 1.

 

A sample is provided. To reproduce:

 

  1. Hit the write button
  2. Hit the read button and confirm accuracy
  3. Close the app
  4. Restart the app
  5. Hit the read button
  6. Confirm the results are different!
  • Ewan Walker
    Hi Andrew using your example I can reproduce the issue by following your instructions. Interesting!
  • You must to post comments
Best Answer
0
1

Looks like a bug. Writing the cookie applies an additional escaping of the json string. You see it only on a new session because that’s the only time the cookies are reloaded from the browser. When you add the cookie it stays in the collection. It was probably added to solve (but it doesn’t) a general bug with ASP.NET cookies not being able to contain the semicolon as part of the string.

As a temporary workaround use this:

  Public Function JavaScriptStringDecode(source As String) As String
      Return source.Replace("\'", "'").Replace("\""", """").Replace("\/", "/").Replace("\t", "\t").Replace("\n", "\n")
  End Function

https://stackoverflow.com/questions/14982917/how-to-decode-a-string-encoded-with-javascriptstringencoded?lq=1

Logged as #1924, will be fixed in the upcoming (overdue) update.

 

 

 

  • Andrew
    Should I call this function before writing the cookie or before after reading it?
  • Andrew
    Nevermind. It’s prior to writing. The workaround appears to work. Thank you
  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.