From MDN:
The DOM Storage mechanism is a means through which string key/value pairs can be securely stored and later retrieved for use.
Also, from Dive Into HTML5:
HTML5 Storage is based on named key/value pairs. You store data based on a named key, then you can retrieve that data with the same key. The named key is a string. The data can be any type supported by JavaScript, including strings, Booleans, integers, or floats. However, the data is actually stored as a string. If you are storing and retrieving anything other than strings, you will need to use functions like parseInt() or parseFloat() to coerce your retrieved data into the expected JavaScript datatype.
Emphasis mine in both quotes.
Therefore, as long as you can store your object as a string, and you can deserialize it from a string, you can put it in Local Storage. Quite often objects are stored using JSON notation. However, as the basic types allowed by the JSON specification do not include functions, JSON cannot be used to store functions in Local Storage.
From this other SO answer and Wikipedia, the basic types supported by JSON are:
- Number (integer, real, or floating point)
- String (double-quoted Unicode with backslash escaping)
- Boolean (true and false)
- Array (an ordered sequence of values, comma-separated and enclosed in square brackets)
- Object (collection of key:value pairs, comma-separated and enclosed in curly braces)
null
You can define your own serialisation by defining a function called toJSON
on the object you want to store in Local Storage, see here:
If an object being stringified has a property named toJSON whose value is a function, then the toJSON method customizes JSON stringification behavior: instead of the object being serialized, the value returned by the toJSON method when called will be serialized.