It appears that the 6.x branch has several issues in the JSON encoding for vanilla JSON data. I haven't looked at the 5 version at all.
1. Output begins: "nodes": [ /* list of objects in here.*/ ]. This is invalid in JSON and JavaScript. Pairs must be part of an object literal.
2. Output uses trailing commas: [{"name": "value", "name2": "value2", },]. This is invalid JSON and JavaScript.
3. strtotime usage has two problems:
a. The check for serialized data assumes that FALSE will be returned when strtotime can't parse. In older PHP versions (prior to 5.1) it returns -1.
b. strtotime is used as a date checker (if strtotime($value) { /* convert to date... */)). But this is bad. It converts a node title like "Last Monday" or "Today" to a date stamp.
I'll submit a patch to fix these three things.
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | views-view-json.tpl-update-02062010.patch | 1.45 KB | shawngo |
| #1 | views_datasource-318130_00.patch | 6.28 KB | mbutcher |
Comments
Comment #1
mbutcher commentedAttached is a patch to fix the issues above. In addition, I did the following:
* Fixed a few places where formatting did not accord with the Drupal coding standards.
* Fixed a few doc blocks
* Added a new function that checks to see if a string is a serialized date (returns bool instead of trying to convert)
* Made a minor tweak to the JSON encoding so that it is done in one low-level C call instead of repeated str_replace calls.
I didn't try to do anything all that comprehensive... just got it working for what I need.
The patch was generated against CVS branch DRUPAL-6--1, which I presume is the current latest branch. It applied cleanly to the alpha2 release, as well. (Note that I tested it against an older version of Views2, having seen bugs about the recent Views 2 changes.)
Thanks for building this module. It is very useful.
Comment #2
mbutcher commentedI see there has been no movement on this patch.
I think there is an even simpler solution than the one I provided above, since the drupal_to_js() function accomplishes the goal of entire functions in the views_datasource plugin.
I would be more than happy to submit another patch... but is this module even being maintained at this point?
Comment #3
allisterbeharry commentedThanks for the patch - I'll incorporate the date check and the more efficient JSON encoding in the next release. Docs and code formatting are a project-wide issue I have to work on.
Comment #4
shawngo commentedA comma still exists after the last field in each view result in views-view-json.tpl.php.
Example:
{ "nid" : "1", "node_title" : "My Node",}The trailing comma after "My Node", breaks further execution in Internet Explorer 6 (or at least causes some havoc).
{ "nid" : "1", "node_title" : "My Node"}The patch increments $field_count in views-view-json.tpl.php which in effect allows the ternary operator to result to TRUE and skip the trailing comma.
It looks like this was going to be fixed in 1.3a but $field_count wasn't incremented.
Thanks,
Shawn
Comment #5
buddaI'm not seeing any commas in the output for myself. Here's an example output...
Comment #6
allisterbeharry commentedThanks for the patch. This should be superceded in the current dev version by the new JSON renderer.