I have data-structures that are defined as Javascript objects. I like to stringify them to send over network. Merely calling JSON.stringify() on them seems to work. But is it a good practice?
Are there any special cases where this may break (for example static methods?)
JSON is a subset of JS, so from that standpoint, it’s 100% fine.
JSON is also intended to be converted into JS to be operated on, and converted back, to be sent out, or stored.
So turning an object into JSON is part of the intended purpose, rather than having to write all JSON by hand.
In terms of special cases, JSON can contain no functions, no circular references… nothing other than strings, numbers and booleans, inside of nested objects and arrays.
If that’s already the format your data is in, you’re done.
IE6 and 7 don’t have native JSON support.
However, json2.js is the file from Douglas Crockford which all of the native implementations are based on.
If
!window.JSONthen load that file, and it will work just fine, in OldIE.