Download & Extend

Drupal.toJson object encoding errors

Project:JSON server
Version:6.x-2.x-dev
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:needs work

Issue Summary

From PHP documentation:

- Common JSON mistakes

<?php
// the following strings are valid JavaScript but not valid JSON

// the name and value must be enclosed in double quotes
// single quotes are not valid
$bad_json = "{ 'bar': 'baz' }";
json_decode($bad_json); // null

// the name must be enclosed in double quotes
$bad_json = '{ bar: "baz" }';
json_decode($bad_json); // null

// trailing commas are not allowed
$bad_json = '{ bar: "baz", }';
json_decode($bad_json); // null
?>

Drupal.toJson is making second and third. Solution:

        case 'object':
          var output = "{";
          for(i in v) {
            output = output + '"' + i + '"' + ":" + this._toJson(v[i]) + ",";
          }
          if (output[output.length - 1] == ',') {
            output = output.substring(0, output.length - 1);
          }
          output = output + "}";
          return output;

Also json_decode function is returning as default stdClass instead of array. Services module doesn't support object just arrays. Solution:

function drupal_parse_json($v) {
  // PHP 5 only
  if (function_exists('json_decode')) {
    return json_decode($v, TRUE);
  }

Comments

#1

Version:6.x-1.x-dev» 6.x-2.x-dev

This bug still exists in 6x.2x-dev. The fix to the JS for Drupal.service is outlined above.

#2

Status:needs review» closed (fixed)

#3

Status:closed (fixed)» needs review

The JS part is still not fixed, attaching patch.

AttachmentSize
424610-fix-toJson-output.patch 439 bytes

#4

Status:needs review» closed (fixed)

php compatibility issue has been fixed in the new alpha

#5

Status:closed (fixed)» needs work

This isn't a problem of PHP but JSON that is produced on client side. Simple example:

<?php
// This is what is produced by Drupal.toJson function in current version. PHP can't handle extra commas
// and json_decode will return NULL variable
$str = '{"test":{"a":"b",},}';
print_r(json_decode($str));

// After applying patch this output is produced and PHP can parse JSON string properly
$str = '{"test":{"a":"b"}}';
print_r(json_decode($str));
?>

This patch is removing extra commas from produced JSON string.

#6

We should prevent creating the bad json at first place instead of massaging the output. I am not sure how the extra "," got created.

#7

Its because module uses custom function in Drupal namespace - Drupal.toJson which creates JSON string. The output means serialized JSON, whcih I am trying to fix by this patch. By applying this patch none extra "," will be created.

#8

The patch only removes trailing "," after the whole JSON string gets created with the trailing ",". That's not a good approach at all. We should fix the creation process if there is indeed a bug.

#9

You are right, here is proper fix.

AttachmentSize
424610-fix-toJson-output.patch 766 bytes
nobody click here