I am using the Sencha Connect cookie sessions. It works great in Node but I need to parse the cookie on the client side. Connect adds s:j: to the front of the cookie and uses a dot notation + generated key at the end. Is there a way to prevent this or parse this in JS or do I need to write my own parser?
The cookie value looks like so:
s: j: {
"oauth": {
"token": "xxxxxx",
"token_secret": "xxxxxx",
"results": {
"oauth_callback_confirmed": "true"
},
"access_token": "xxxxx-xxxx",
"access_token_secret": "xxxx"
},
"me": {
"val": "bu-rcizj7u8jm7jzgea",
"refs": {
"bu-rcizj7u8jm7jzgea": {
"id": "bu-rcizj7u8jm7jzgea",
"creation_time": "2012-07-13T16:01:17.159+0000",
"twitter_id": "138031018",
"first_name": "xxx",
"last_name": "xxx",
"email": "xxx@xxx.com",
"permission": {
"can_update": false,
"can_delete": false,
"can_read": false
},
"twitter_screen_name": "xxxx"
}
}
}
}.DdFx96zckmMbWY8wLiFhuGwnofluk3x2WSVBv71LOV8
Why don't you just do something like this? It should work for any returned structure (note indexOf returns -1 if not found, and '}.' is not valid JSON generally, so it just reads to the end in the case of actually valid JSON). An alternative would be to write an implementation of Dijkstra's algorithm that only starts pushing onto the value stack once it finds the opening '{'; this would be slightly faster because it would only be one-pass (whereas calling indexOf twice can make my implementation at worst three-pass). I'm happy to send along the one-pass code if you're interested.
var rawCookie = 's: j: { "oauth": { "token": "xxxxxx", "token_secret": "xxxxxx", "results": { "oauth_callback_confirmed": "true" },"access_token": "xxxxx-xxxx", "access_token_secret": "xxxx"}, "me": {"val": "bu-rcizj7u8jm7jzgea","refs": {"bu-rcizj7u8jm7jzgea": { "id": "bu-rcizj7u8jm7jzgea", "creation_time": "2012-07-13T16:01:17.159+0000", "twitter_id": "138031018", "first_name": "xxx", "last_name": "xxx", "email": "xxx@xxx.com", "permission": { "can_update": false, "can_delete": false,"can_read": false }, "twitter_screen_name": "xxxx" } } } }.DdFx96zckmMbWY8wLiFhuGwnofluk3x2WSVBv71LOV8';
var niceJsonCookie = rawCookie.slice(rawCookie.indexOf('{'), rawCookie.indexOf('}.') + 1);
var niceJsonObject = JSON.parse(niceJsonCookie);