node-localstorage getItem may not support Objects
sudheer594 opened this issue · 7 comments
node-localstorage doesnt support objects.
In localstorage.setItem method, the value being converted to string make it unusable.
ex:
O:{a:1, b:'some string', c:{x: 1, y: 2}}
saving to local storage localStorage.setItem('2', o)
getting from local storage O:[object Object]
getting from local storage O.a:undefined
Is it expected behaviour or am i missing something?
I wished it worked the way you are suggesting but the spec for localstorage says to call .toString() before storing. That means that objects get stored as the string "[object, Object]". See here.
Of course the work-around is to JSON.stringify before you send it to localstorage and JSON.parse on the way out.
I wiil implement the feature, if you are OK with it.
will create a property useJSON, which will decide whether to stringify the objects.
Jquery cookie https://github.com/carhartl/jquery-cookie addon has the same feature.
Sorry, for the slow reply. I wanted to sleep on my thoughts.
I'm not a big fan of adding a flag to change the way it works. I would prefer that you wrap it with composition like I did in my localcache package, or you could extend the LocalStorage class and override the get and set item methods. In either case, the functionality would be in a new package, maybe call it json-localstorage and would list as a dependency in its package.json, node-localstorage.
Note, localcache is no longer in development and it has some problems (including that I only list node-localstorage as a dev-dependency when it should be a true dependency for the use-case you are describing) so I'm not suggesting that you use it as-is, just that it's an example of building upon a light single-purpose core module like node-localstorage. As a funny aside, I actually set out to create localcache first and only needed to build node-localstorage as a means to test localcache. Now, node-localstorage has a lot of users and localcache has none, not even me. A lesson in creating small, single-purpose packages.
If you decide to extend node-localstorage as I'm suggesting, I'll gladly support you. If you don't want to go through the rigamarole of creating an npmjs.org account and packages, I'd be willing to essentially host, co-code, code-review, and help manage your json-localcache package.
Thanks for the reply.
Perhaps the extra property can be avoided and by default setItem and getItem can use JSON.stringify and JSON.parse respectively.
Users of the node-localstorage doesnt have to do any changes.It works out of the box and supports storing objects as well.
Only notable point is every item in cache takes 2 bits more memory because of JSON conversion.
Is there any problem associated with using JSON.Stringify? let me know.
We can't make it default because it would then not follow the specification for the LocalStorage object in browsers. However, I just upgraded it to have an additional class that inherits from LocalStorage that behaves as you are asking (v0.5.0). Here is the entire source for the new class.
class JSONStorage extends LocalStorage
setItem: (key, value) ->
newValue = JSON.stringify(value)
super(key, newValue)
getItem: (key) ->
return JSON.parse(super(key))
Looks better than the approach i have mentioned. +1