I'm using the json server and noticed a problem with some encoded values. This could be a problem with drupal_to_js, however.
The problem has to do with decoding results returned from the server that contain a single quote ( ' ). Below is code that will replicate the problem.
Running:
PHP5
Drupal 6.dev (latest as of this week)
JSON Server 6.x-1.x-dev
//Normally I would use single quotes but this example uses them
$term = array(
"term" => "Women's Leadership Institute",
"dept" => "LS"
);
// This will not decode
$json = drupal_to_js($term);
print_r(json_decode($json)); // Returns False
// This will decode
$json = json_encode($term);
print_r(json_decode($json)); // Return an object.
As a work around I modified json_server.module line 43 from:
return drupal_to_js(array('#error' => FALSE, '#data' => $result));
to:
if(function_exists('json_encode')) {
return json_encode(array('#error' => FALSE, '#data' => $result));
}
else {
return drupal_to_js(array('#error' => FALSE, '#data' => $result));
}
Obviously, this is far from ideal. Project needed to be completed and this got the job done. Anyone have any suggestions on a cleaner way of solving this?
Comments
Comment #1
aroq commentedThank you, this is really helpful.
Comment #2
yajnin commentedThis is interesting. There is a similar issue a couple of us were having here in posting JSON strings as an argument. The solution was to escape (at the minimum) the first curly bracket.
I'm primarily a front end dev, so forgive me if I'm wrong, but reading this and reviewing our issue I'm lead to believe the stumbling block is the drupal_to_js parser? The maintainers (huge thanks for their original work (I'm a huge fan, don't get me wrong)) seem to have abandoned this module, so any insight from other users would be appreciated.
Comment #3
nicholasthompsonI had a similar issue when the result from services has a user object with a message in the session. Essentially, nested curly braces do not encode properly with drupal_to_js.
If the server has PHP 5.2, the best fix is to use json_encode (a native encoder). If <5.2 then I guess the fix is to raise this in the core issue queue?
Comment #4
icylace commentedI looked at drupal_to_js() and noticed that it uses PHP's addslashes() function. This function escapes single quotes as well as some other characters. Since the escaped single quote is not defined in JSON it makes sense that it would be treated as illegal. This would explain the discrepancy. drupal_to_js() also formats strings to be HTML-safe and so doesn't strictly adhere to the JSON spec.
Comment #5
Anonymous (not verified) commentedHere is a patch, thanks ngmaloney
Comment #6
burningdog commentedBusy patching drupal_to_js() to return correct JSON, which has included removing the addslashes() function - so no more escaping single quotes. http://drupal.org/node/479368
Please see if that patch helps - if it gets into core then that'll solve this issue.
Comment #7
Nick Robillard commentedHere's a patch for 2.0-alpha2. I've replaced all instances of drupal_to_js() to json_encode(). Also removed the # from array keys in json_server_server_error() (as required by Services 2). Note that this version only works with PHP 5.
Comment #8
Nick Robillard commentedHere's an update to my patch. I've found that on some setups, json_decode() will return nothing if passed a non-json string, rather than the non-json string like json_server_server() expects. Here specifically: $request = drupal_parse_json($_POST['method']);
Comment #9
g10 commentedit solved the issue, tnx
(allthought the changes had to be applied manually, patch didn't seem to work)
Comment #10
batje commentedYes, there is an issue with the patch, this is my .rej file
However, the patch seems to work without this small failure. That is pretty cool. Any chance of adding this to the alpha download?
Comment #11
Nick Robillard commentedHm the patch works fine for me. Within json_server dir a
patch -p0 < json_server-2.0-alpha2-encode_fix-443090-8.patchreturns with "patching file json_server.module".Comment #12
newnewuser commented#8 works like magic (after playing with this for days!!!)
Thanks so much.
Comment #13
edxxu commented6.x-2.0-alpha3 has been released to fix this bug.
http://drupal.org/project/json_server