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.

Comments

mbutcher’s picture

Status: Active » Needs review
StatusFileSize
new6.28 KB

Attached 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.

mbutcher’s picture

I 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?

allisterbeharry’s picture

Assigned: Unassigned » allisterbeharry

Thanks 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.

shawngo’s picture

Version: 6.x-1.0-alpha2 » 6.x-1.0-alpha3
StatusFileSize
new1.45 KB

A 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.

$json.=str_repeat(" ", 8).'"'.$label.'"'. " ".": ".'"'.$value.'"'.((++$field_count == $total_field_count) ? "":",")."\n";

It looks like this was going to be fixed in 1.3a but $field_count wasn't incremented.

Thanks,
Shawn

budda’s picture

I'm not seeing any commas in the output for myself. Here's an example output...

{
  "nodes":
    [
      {
        "nid" : "13303",
        "node_node_data_field_questions_node_data_field_picture_field_picture_fid" : "13991",
        "node_node_data_field_questions_node_data_field_picture_field_picture_list" : "1",
        "node_node_data_field_questions_node_data_field_picture_field_picture_data" : "a:4:{s:11:\"description\";s:0:\"\";s:3:\"alt\";s:0:\"\";s:5:\"title\";s:0:\"\";s:7:\"process\";b:1;}",
        "node_node_data_field_questions_nid" : "13354",
        "node_node_data_field_questions_type" : "quiz_question",
        "node_node_data_field_questions_vid" : "13354",
        "node_title" : "Eastenders Quiz",
        "node_created_minute" : "201002111403"
      },
      {
        "nid" : "13272",
        "node_node_data_field_questions_node_data_field_picture_field_picture_fid" : "13911",
        "node_node_data_field_questions_node_data_field_picture_field_picture_list" : "1",
        "node_node_data_field_questions_node_data_field_picture_field_picture_data" : "a:4:{s:11:\"description\";s:0:\"\";s:3:\"alt\";s:0:\"\";s:5:\"title\";s:0:\"\";s:7:\"process\";b:1;}",
        "node_node_data_field_questions_nid" : "13273",
        "node_node_data_field_questions_type" : "quiz_question",
        "node_node_data_field_questions_vid" : "13273",
        "node_title" : "Movie Pictures Quiz 3.0",
        "node_created_minute" : "201002101749"
      },
allisterbeharry’s picture

Status: Needs review » Fixed

Thanks for the patch. This should be superceded in the current dev version by the new JSON renderer.

Status: Fixed » Closed (fixed)

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