Default option json parse for StorageModule.readSync() / StorageModule.read()

Hi there,
It would be nice if we could define default value in this StorageModule.readSync(filename)
For instance;
I have a User Model like this;

function User(user){
 //Default property declaration
  this.username= null;
  this.password= null;

 //Some logical things here
 if(user.username !== undefined){
    this.username == user.username;
 }

}

I would like to use this like that;

var UserLocal = new User(Storage.readSync("user.cd", true, {})); //filename , json parse option, default value
/*
 Then I know I've a json parsed Object called as UserLocal
*/

I know it’s not a very serious need, but it might be a nice feature.

Hi,

The reason we don’t support automatic JSON parsing in the Storage module is that, while JSON is extremely common these days, it is still only one of many file formats available. Supporting only one of these formats, apart from pure text, doesn’t really make sense.

It is best that the Storage module only knows how to translate from file -> string, and the JSON.parse function knows how to translate from string -> JavaScript object.
This way, Storage does not need to know about the structure of the file’s contents, and JSON.parse doesn’t need to know what a file is.

However, you can very easily define this function yourself:

function readJSONSync(filename, defaultValue) {
	try {
		return JSON.parse(Storage.readSync(filename));
	}
	catch(ex) {
		return defaultValue;
	}
}

The async version can be defined as:

function readJSON(filename, defaultValue) {
	return Storage.read(filename)
		.then(JSON.parse)
		.catch(function() {
			return defaultValue;
		});
}