Question

Duplicate: Serializing to JSON in jQuery

It seems there's a JSON serializer/deserializer built into jQuery, since it's able to implement things like .getJSON . Is it possible to use the built in jQuery JSON processor itself a general purpose JSON processor (ie. make a jQuery.something() call to serialize/deserialize my own data), removing the need for a separate JSON library?

Update: The responses in the question referenced above say there's no JSON serialization code in jQuery. How about JSON deserialization? How is .getJSON implemented, and why isn't the deserialization capability exposed for general use?

Update2: I'm already using json2 from json.org , I'm trying to get rid of it as all I'm doing is simple deserialization. Sounds like jquery really is just doing eval? I had the impression that wasn't so good for security:

The eval function is very fast. However, it can compile and execute any JavaScript program, so there can be security issues

Was it helpful?

Solution

jQuery does just use eval() for now. jQuery 1.4 will include support for browser-native JSON deserialization in browsers that support it. You can take advantage of that now if you want, by using jQuery's dataFilter callback to avoid the eval() when possible (put it in $.ajaxSetup to affect all $.ajax(), $.post(), $.getJSON() calls automatically).

OTHER TIPS

Sounds like what you need is the serialize/deserialize implementation available at http://www.json.org/js.html which provides JSON.parse() and JSON.stringify(), making it easy to (de)serialize and validate data.

If you simple want to translate a JSON text into an object then use the eval() function.

I just started using jQuery, and I find it's easiest just to drop the JSON2.js library from JSON.org in, then extend jQuery to use it.

I believe to deserialize the incoming json data jQuery just eval()'s it. I don't know of any serialziation built in though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top