We shouldn't remove Drupal.parseJson in favor of jQuery.getJSON #242642: Drupal.parseJson should be removed in favor of jQuery.getJSON

We can however optimize it. Patch uses Native JSON parser when available (FF and IE8). jQuery 1.4 is parsing json the same way as this patch.

Comments

Status: Needs review » Needs work

The last submitted patch, parsejson.patch, failed testing.

Status: Needs work » Needs review

Re-test of parsejson.patch from comment @comment was requested by casey.

mfer’s picture

StatusFileSize
new546 bytes

Is there a reason data is being overwritten and returned?

The patch I posted on the duplicate just returned the result of either the JSON.parse or new Function method. Is there a benefit of data being overwritten and returned?

casey’s picture

No reason. I like yours better too.

c960657’s picture

Is the Function approach better than eval or just different? I think we use single where possible in JS also.

mfer’s picture

@c960657 IMHO it's a better approach. This is the approach jQuery and other libraries are taking. jQuery uses the same approach (same code, too) as the patches here.

You can check out details on the jQuery switch in it's issue at http://dev.jquery.com/ticket/4680 and you can see the commit at http://github.com/jquery/jquery/commit/90a87c03b4943d75c24bc5e6246630231....

mfer’s picture

Bump, the patch in #3 still applies.

casey’s picture

I'd say RTBC. Anyone else?

robloach’s picture

Status: Needs review » Reviewed & tested by the community

I'd trust jQuery.getJSON over Drupal.parseJSON anyday, but this is a good first step to make sure they're aligned before replacing them. If they arn't the same, we should eventually take steps to either push the missing functionality back into jQuery, or take steps to align them properly. Maybe instead of won't fix, we push #242642: Drupal.parseJson should be removed in favor of jQuery.getJSON to Drupal 8?

This is a good step for now, though.

int’s picture

jQuery 1.4.1 as been released with New Features and fix for parseJSON.

http://jquery14.com/day-12/jquery-141-released

mbutcher’s picture

Reviewed, and this does look like a good solution. Using JSON.parse where possible is totally the right move. And following jQuery is generally a safe bet.

casey’s picture

Status: Reviewed & tested by the community » Postponed

Postponed per #10: we should wait for #653580: Upgrade to jQuery 1.4 to land and use jQuery.parseJSON (http://api.jquery.com/jQuery.parseJSON/)

casey’s picture

Status: Postponed » Needs review
StatusFileSize
new1.43 KB

Patch can be committed when #653580: Upgrade to jQuery 1.4 is fixed (which can be any day now).

robloach’s picture

Status: Needs review » Reviewed & tested by the community
mfer’s picture

Yay for cool stuff added in jQuery 1.4.1!

dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Thanks.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

tr’s picture

Issue tags: +Needs documentation

Porting a module to D7, I couldn't find any mention of this change in the documentation http://drupal.org/update/modules/6/7

Adding a "Needs Documentation" tag.

mfer’s picture

Issue tags: -Needs documentation

Good documentation catch. Fixed. You can see it at http://drupal.org/update/modules/6/7#Drupal.parseJSON

btully’s picture

subscribe

krishworks’s picture

this needs backport for drupal 6. After updating jquery to 1.7 in drupal 6, some of the functionality stopped working. eg. ajax link to view older heartbeat messages. A direct replacement of code below works fine.

replace

Drupal.parseJson = function (data) {
  if ((data.substring(0, 1) != '{') && (data.substring(0, 1) != '[')) {
    return { status: 0, data: data.length ? data : Drupal.t('Unspecified error') };
  }
  return eval('(' + data + ');');
};

with

Drupal.parseJson = function (data) {
 // Use native JSON method when available.
 if ( typeof JSON === "object" && JSON.parse ) {
   data = JSON.parse(JSON.stringify(data));
 } 
 else {
   if ((data.substring(0, 1) != '{') && (data.substring(0, 1) != '[')) {
     return { status: 0, data: data.length ? data : Drupal.t('Unspecified error') };
   }
   data = (new Function("return " + data))();
 }
 return data;
};
krishworks’s picture

Status: Closed (fixed) » Patch (to be ported)

patch to be ported for drupal 6

oriol_e9g’s picture

Version: 7.x-dev » 6.x-dev
Status: Patch (to be ported) » Needs review
StatusFileSize
new913 bytes

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 6 is no longer supported. If the issue verifiably applies to later versions, please reopen with details and update the version.