9

This is probably a simple question, which I can't seem to find a solid answer to.

Why would one choose JSON2 over jquery-json plugin (http://code.google.com/p/jquery-json/)? Given that a web application is using jQuery to begin with.

Everyone's writing about how great it is that JSON2 falls back on the native implementation; well, so does jquery-json. I welcome links to blogs, articles and examples. However, I'm looking for a strong solid answer on which one is better to use and why.

halfer
  • 19,824
  • 17
  • 99
  • 186
Dimskiy
  • 5,233
  • 13
  • 47
  • 66

3 Answers3

16

Author of jquery-json here. It really doesn't matter much. I made jquery-json a long time ago when there were no good options, and keep it there because a lot of people like it.

They are both a handful of functions that do just about the same thing.

10

An important difference between the two is that JSON2's api is exactly the same as the native api whereas jquery-json is a jquery plugin (which is slightly different than falling back on the native implementation).

I would say your answer depends on which api you want to use since you'll get the same results with either implementation (you should at least).

As a thought experiment, let's imagine that every browser had a native JSON api implementation. Would you still use jquery-json?

If so, then use the jquery plug-in.

If not, then why would you tie your code to the jquery-json api when the native api is already well-known (even if its not globally implemented)?

If it means anything to you, John Resig (the creator of jQuery) has said "In the meantime PLEASE start migrating your JSON-using applications over to Crockford's json2.js"

Ken Browning
  • 28,693
  • 6
  • 56
  • 68
  • 1
    Good thought. If all browsers had native JSON api, I would NOT use any plugin. Let me take a look at that link to John's blog. – Dimskiy Oct 20 '10 at 16:18
  • I think the answer takes several readings until one gets that json2.js implements the native API and that jquery-json does not. – Carl Oct 07 '11 at 15:03
2

Most modern browsers have native JSON build in, jQuery defaults to those functions when using e.g. $.getJSON() or $.parseJSON().

So if you are using jQuery, you don't need any plugin/library for JSON.

saibotd
  • 741
  • 8
  • 20
  • Most of the time, I use $.ajax() function, so I need to serialize/deserialize somewhat manually. – Dimskiy Oct 20 '10 at 16:15
  • 3
    You shouldn't need to deserialize your data if you set the dataType property of the configuration object you pass into `$.ajax()` – Ken Browning Oct 20 '10 at 16:21
  • 1
    @Ken Browning - I was getting my data from an asmx service. So it was prefixed with Microsoft's 'd'. I had to do something like $.evalJSON(resultData.d) with jquery-json plugin. That was sometime back, though. I'm currently trying to evaluate some UI related questions/issues for an upcoming project. Thanks for the suggestion. – Dimskiy Oct 20 '10 at 16:39