*
* @param {string} key Key of item to retrieve
* @return {string|null|boolean} String value, null if no value exists, or false
- * if localStorage is not available.
+ * if storage is not available.
*/
SafeStorage.prototype.get = function ( key ) {
try {
*
* @param {string} key Key name to store under
* @param {string} value Value to be stored
- * @return {boolean} Whether the save succeeded or not
+ * @return {boolean} The value was set
*/
SafeStorage.prototype.set = function ( key, value ) {
try {
* Remove a value from device storage.
*
* @param {string} key Key of item to remove
- * @return {boolean} Whether the save succeeded or not
+ * @return {boolean} Whether the key was removed
*/
SafeStorage.prototype.remove = function ( key ) {
try {
return false;
};
+ /**
+ * Retrieve JSON object from device storage.
+ *
+ * @param {string} key Key of item to retrieve
+ * @return {Object|null|boolean} Object, null if no value exists or value
+ * is not JSON-parseable, or false if storage is not available.
+ */
+ SafeStorage.prototype.getObject = function ( key ) {
+ var json = this.get( key );
+
+ if ( json === false ) {
+ return false;
+ }
+
+ try {
+ return JSON.parse( json );
+ } catch ( e ) {}
+
+ return null;
+ };
+
+ /**
+ * Set an object value in device storage by JSON encoding
+ *
+ * @param {string} key Key name to store under
+ * @param {Object} value Object value to be stored
+ * @return {boolean} The value was set
+ */
+ SafeStorage.prototype.setObject = function ( key, value ) {
+ var json;
+ try {
+ json = JSON.stringify( value );
+ return this.set( key, json );
+ } catch ( e ) {}
+ return false;
+ };
+
/**
* A wrapper for the HTML5 `localStorage` interface
* that is safe to call on all browsers.