Has anyone implemented Mozilla\'s Object.toSource() method for Internet Explorer and other non-Gecko browsers? I\'m looking for a lightweight way to serialize simple object
You don't have to use toSource()
; wrap the code to be serialized in a function that returns the JSON struct, and use function#toString()
instead:
var serialized = function () {
return {
n: 8,
o: null,
b: true,
s: 'text',
a: ['a', 'b', 'c'],
f: function () {
alert('!')
}
}
};
serialized.toString();
See a live demo on jsFiddle.
If matching the exact serialization format of Firefox is not your aim, you could use one of the JavaScript JSON serialization/deserialization libraries listed at http://json.org. Using a standard scheme like JSON may be better than mimicking the proprietary Gecko format.