Skip to content

Instantly share code, notes, and snippets.

@lightjs
Created January 23, 2013 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lightjs/4604334 to your computer and use it in GitHub Desktop.
Save lightjs/4604334 to your computer and use it in GitHub Desktop.
Turn a json into a full object, with getter / setter for all values and init function for new getter / setter.
var jsonize = function(obj) {
if( typeof obj == 'object' && !(obj[k] instanceof Array) && Object.keys(obj).length > 0 ) {
for( var k in obj ) {
if( obj[k] instanceof Array ) for( var k2 in obj[k] ) obj[k][k2] = jsonize(obj[k][k2]);
else if( typeof obj[k] == 'object' && !(obj[k] instanceof Array) && Object.keys(obj[k]).length > 0 ) jsonize(obj[k]);
obj['_'+k] = obj[k];
obj[k] = (function(n) {
return function(_) {
if(!arguments.length) return obj['_'+n];
obj['_'+n] = _;
return obj;
}
})(k);
}
obj['init'] = function(n) {
obj[n] = function(_) {
if(!arguments.length) return obj['_'+n];
obj['_'+n] = _;
return obj;
}
return obj;
}
}
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment