'json.stringify of web audio nodes return empty
Is there a way to stringify a web audio node object (such as gain,biquadfilter
) to save/restore its settings?
var gainNode = audioCtx.createGain();
var str = JSON.stringify( gainNode );
console.log( str );
sadly str returns "{}"
Solution 1:[1]
JSON.stringify()
only includes the object's own properties, and not properties inherited from the prototype chain. See How to stringify inherited objects to JSON? for more details.
One way to print all properties, including inherited ones:
function flatten(obj) {
var ret = {};
for (var i in obj) {
ret[i] = obj[i];
}
return ret;
}
var gainNode = audioCtx.createGain();
var str = JSON.stringify(flatten(gainNode));
console.log(str);
While JSON.stringify()
only includes own properties, the for (var i in obj)
syntax iterates over all properties, including inherited ones (except non-enumerable properties, but a GainNode
doesn't have those).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Thomas McGuire |