You can use JSON.stringify with a replacer like:
JSON.stringify({
color: 'red',
doSomething: function (arg) {
alert('Do someting called with ' + arg);
}
}, function(key, val) {
return (typeof val === 'function') ? '' + val : val;
});
Answer from CD.. on Stack OverflowVideos
You can use JSON.stringify with a replacer like:
JSON.stringify({
color: 'red',
doSomething: function (arg) {
alert('Do someting called with ' + arg);
}
}, function(key, val) {
return (typeof val === 'function') ? '' + val : val;
});
A quick and dirty way would be like this:
Object.prototype.toJSON = function() {
var sobj = {}, i;
for (i in this)
if (this.hasOwnProperty(i))
sobj[i] = typeof this[i] == 'function' ?
this[i].toString() : this[i];
return sobj;
};
Obviously this will affect the serialization of every object in your code, and could trip up niave code using unfiltered for in loops. The "proper" way would be to write a recursive function that would add the toJSON function on all the descendent members of any given object, dealing with circular references and such. However, assuming single threaded Javascript (no Web Workers), this method should work and not produce any unintended side effects.
A similar function must be added to Array's prototype to override Object's by returning an array and not an object. Another option would be attaching a single one and let it selectively return an array or an object depending on the objects' own nature but it would probably be slower.
function JSONstringifyWithFuncs(obj) {
Object.prototype.toJSON = function() {
var sobj = {}, i;
for (i in this)
if (this.hasOwnProperty(i))
sobj[i] = typeof this[i] == 'function' ?
this[i].toString() : this[i];
return sobj;
};
Array.prototype.toJSON = function() {
var sarr = [], i;
for (i = 0 ; i < this.length; i++)
sarr.push(typeof this[i] == 'function' ? this[i].toString() : this[i]);
return sarr;
};
var str = JSON.stringify(obj);
delete Object.prototype.toJSON;
delete Array.prototype.toJSON;
return str;
}
http://jsbin.com/yerumateno/2/edit
Assuming you don't know which properties are JSON, you could use the replacer function parameter on JSON.stringify to check if a value is a JSON string. The below example tries to parse each string inside a try..catch , so is not the most efficient, but should do the trick (on nested properties as well)
var obj = {id:1, options:"{\"code\":3,\"type\":\"AES\"}"};
function checkVal(key,val){
if(typeof val === 'string'){
try{return JSON.parse(val);}catch(e){}
}
return val;
}
var res = JSON.stringify(obj,checkVal);
console.log('normal output', JSON.stringify(obj))
console.log('with replacer', res);
No, you can't do that.
If you did not encode that string, JSON.parse will not return a correct string.
The cleanest solution to do that is use JSON for obj.options, and stringify it when you need to use it.
What do you guys think? Should I make an official proposal? What would be the best way to make such spec proposal?
And tell me what you think! The idea is for any object's toString method override the way JSON.stringify creates the string. For example:
var vec =
{
x: 0,
y: 0,
toString() => `[${this.x},${this.y}]`
}
JSON.stringify(vec) //will return: '[0,0]'This way I won't need to use a custom stream to alter a big object when converting it to JSON!
Or would you say this is very unnecessary, even if an easy thing to add to JS?