tags:

views:

2181

answers:

4

Duplicate: http://stackoverflow.com/questions/191881/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

+1  A: 

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

Brian Yarger
+2  A: 

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.

Cory Larson
+3  A: 

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.

stefpet
+2  A: 

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).

Dave Ward