However, it appears that my object is being changed to a string when I try to store a JavaScript object in HTML5 localStorage.
Using localStorage, I can save and get primitive JavaScript types and arrays, but objects don't appear to function. Must they?
var testObject = { 'one': 1, 'two': 2, 'three': 3 };
console.log('typeof testObject: ' + typeof testObject);
console.log('testObject properties:');
for (var prop in testObject) {
console.log(' ' + prop + ': ' + testObject[prop]);
}
// Put the object into storage
localStorage.setItem('testObject', testObject);
// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');
console.log('typeof retrievedObject: ' + typeof retrievedObject);
console.log('Value of retrievedObject: ' + retrievedObject);
typeof testObject: object
testObject properties:
one: 1
two: 2
three: 3
typeof retrievedObject: string
Value of retrievedObject: [object Object]
It appears to me that the input is converted to a string before being stored by the setItem method.
This behavior is present in Safari, Chrome, and Firefox, thus I infer that it is not a defect or restriction unique to any one browser but rather my misinterpretation of the HTML5 Web Storage definition.
I have made an effort to understand the structured clone algorithm that is mentioned in 2 Common infrastructure. I'm not sure what it's saying exactly, but perhaps my issue has to do with the fact that the properties of my object aren't enumerable (???).
Is there a simple solution?