Hello,
I'm writing a module mostly based on javascript & json and call to Drupal.parseJson a lot. Normally, Drupal.parseJson works fine for all parts of the module but does not work correctly in a page. It always return an object with 2 properties, status and data, no matter what the input string is. The 'status' property contains nothing(according to my test) and 'data' contains the input string. Then, I use eval() instread of Drupal.parseJson and everything works as normal. I'm busy now, so don't have time to put my hand on it.

CommentFileSizeAuthor
#6 parsejson.patch725 bytescasey
#4 parseJson.patch1.61 KBmarcingy
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Chill35’s picture

Drupal.parseJson is deprecated in Drupal 6. Please use the jQuery function getJSON instead.

The documentation says that Drupal.parseJSON has been removed, while it is still available... http://drupal.org/node/114774

Removed several functions from drupal.js

* Drupal.extend(): this function has been removed in favor of jQuery.extend(Drupal, ...).
* Drupal.absolutePosition(elem): this functionality is now in jQuery 1.2 in $(elem).offset().
* Drupal.dimensions(elem): these values can be gathered by calling $(elem).width() or $(elem).height().
* Drupal.mousePosition: You most likely need these information at a mouse* event. e.pageY resp. e.pageX contain the mouse position relative to the page (e is the first parameter of an event callback).
* Drupal.parseJson: jQuery supports all kinds of functionality around JSON. If you’re performing an AJAX callback with jQuery.ajax(), set dataType: 'json' and the returned data will be treated as JSON.

If, for some reason, you still need this function, use data = eval('('+ data +')') to parse data into a JSON object.

I say we get rid of the only place left in core where Drupal.parseJSON is *still* used, on line 84 of ahah.js.

I also say we add something to the header comment to say that Drupal.parseJSON is indeed deprecated, in drupal.js, with a reference to the jQuery doc page on jQuery.getJSON. Like so:

/**
 * Parse a JSON response.
 *
 * The result is either the JSON object, or an object with 'status' 0 and 'data' an error message.
 *
 * This function is deprecated. Please use jQuery.getJSON instead.
 * @see http://docs.jquery.com/Ajax/jQuery.getJSON
 *
 */
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 + ');');
};
Gábor Hojtsy’s picture

Title: Drupal.parseJson function (drupal.js) does not parse json correctly in some situation. » Drupal.parseJson should be removed in favor of jQuery.getJSON
Version: 6.1 » 7.x-dev
Category: bug » task

Yes, looks like it is not deprecated just yet:

$grep -r "Drupal.parseJson" *
misc/ahah.js:        response = Drupal.parseJson(response);
misc/drupal.js:Drupal.parseJson = function (data) {

It would be a good idea to

(1) Remove this function in Drupal 7 and eliminate it from ahah.js
(2) Backport the elimination and add a depreciation note to drupal.js

Retitled and reversioned according to this.

casey’s picture

Drupal.parseJson() still exists and is still being used in (renamed) ajax.js

marcingy’s picture

Status: Active » Needs review
FileSize
1.61 KB

Initial patch to get this rolling

c960657’s picture

Status: Needs review » Needs work

How did you test this? When is the response variable a string?

AFAICT jQuery.getJSON() isn't a drop-in replacement for Drupal.parseJson(). jQuery.getJSON() should be called with a URL and it then does a HTTP request. Drupal.parseJson() simply parses a JSON string.
http://docs.jquery.com/Ajax/jQuery.getJSON

casey’s picture

FileSize
725 bytes

Like @c960657 said in #5: Drupal.parseJson() isn't same as jQuery.getJSON(), so we shouldn't replace it.

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.

casey’s picture

Status: Needs work » Closed (won't fix)

New issue for that patch: #673884: Optimize Drupal.parseJSON

btully’s picture

subscribe