Skip to content

Commit

Permalink
Use JSON for serialization of values put into local storage using sto…
Browse files Browse the repository at this point in the history
…re.js.

This addresses Issue 2, which was closed earlier as a won't-fix before I wisened up.

Thanks eddorre and jdunck for similar patches
  • Loading branch information
marcuswestin committed Jul 2, 2010
1 parent e4e0cc6 commit 1611fa5
Show file tree
Hide file tree
Showing 4 changed files with 567 additions and 17 deletions.
58 changes: 49 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
store.js
========

store.js exposes a simple API for cross browser local store
store.js exposes a simple API for cross browser local storage

// Store 'marcus' at 'username'
store.set('username', 'marcus')
Expand All @@ -15,19 +15,56 @@ store.js exposes a simple API for cross browser local store
// Clear all keys
store.clear()

// Use JSON to stash an object (see http://www.json.org/json2.js)
store.set('user', JSON.stringify({ name: 'marcus', likes: 'javascript' }))
// Store an object literal - store.js uses JSON.stringify under the hood
store.set('user', { name: 'marcus', likes: 'javascript' })

// Use JSON to retrieve an object (see http://www.json.org/json2.js)
var user = JSON.parse(store.get('user'))
// Get the stored object - store.js uses JSON.parse under the hood
var user = store.get('user')
alert(user.name + ' likes ' + user.likes)

store.js depends on JSON for serialization.

How does it works?
------------------
store.js uses localStorage when available, and falls back on globalStorage for earlier versions of Firefox and the userData behavior in IE6 and IE7.
No flash to slow down your page load. No cookies to fatten your network requests.

Serialization
-------------
localStorage calls toString on all values that get stores. This means that you can't conveniently store and retrieve numbers, objects or arrays:

localStorage.myage = 24
localStorage.myage != 24
localStorage.myage == '24'

localStorage.user = { name: 'marcus', likes: 'javascript' }
localStorage.user == "[object Object]"

localStorage.tags = ['javascript', 'localStorage', 'store.js']
localStorage.tags.length == 32
localStorage.tags == "javascript,localStorage,store.js"

What we want (and get with store.js) is

store.set('myage', 24)
store.get('myage', 24) == 24

store.set('user', { name: 'marcus', likes: 'javascript' })
alert("Hi my name is " + store.get('user').name + "!")

store.set('tags', ['javascript', 'localStorage', 'store.js'])
alert("We've got " + store.get('tags').length + " tags here")

The native serialization engine of javascript is JSON. Rather than leaving it up to you to serialize and deserialize your values, store.js uses JSON.stringify() and JSON.parse() on each call to store.set() and store.get(), respectively.

Some browsers do not have native support for JSON. For those browsers you should include [JSON.js] (non-minified copy is included in this repo).

Tests
-----
Go to test.html in your browser

Supported
---------
Supported browsers
------------------
- Tested in Firefox 2.0
- Tested in Firefox 3.0
- Tested in Firefox 3.5
Expand All @@ -46,8 +83,8 @@ Supported but borken (please help fix)
- Chrome 4
- Opera 10.10

Not supported
-------------
Unsuported browsers
-------------------
- Firefox 1.0: no means (beside cookies and flash)
- Safari 2: no means (beside cookies and flash)
- Safari 3: no synchronous api (has asynch sqlite api, but store.js is synch)
Expand All @@ -58,3 +95,6 @@ TODO
----
- I believe underlying APIs can throw under certain conditions. Where do we need try/catch?
- Test different versions of Opera 10.X explicitly


[JSON.js]: http://www.json.org/json2.js
Loading

0 comments on commit 1611fa5

Please sign in to comment.