Various JSON improvements
icylace - November 9, 2009 - 07:24
| Project: | Views Datasource |
| Version: | 6.x-1.x-dev |
| Component: | Code |
| Category: | task |
| Priority: | normal |
| Assigned: | allisterbeharry |
| Status: | fixed |
Description
This patch contains the following changes I've made:
- Rewrote the JSON renderer. A preprocess step for making MIT Simile/Exhibit JSON has been added to streamline rendering. json_encode() will be used if it's available otherwise rendering will be done based on code from the drupal_to_js() Drupal core function. This allows for all data types including nested objects and arrays to be encoded properly. Preview rendering will be done using a more hacked version of the code from drupal_to_js() to allow for pretty-printing.
- Reorganized the style settings screen.
- Changed to the array form of str_replace() in views_json_encode_special_chars().
- Empty fields are now included. I don't know why they were originally excluded.
- Now using the "application/json" content type.
- Renamed views_json_get_json() to views_json_get(). It now returns an object by default and also has an optional parameter to return the raw JSON string.
I'm still a little confused about how to do a patch properly but hopefully this latest one is formatted better than my previous ones.
| Attachment | Size |
|---|---|
| views_datasource_json_improvements.patch | 19.58 KB |

#1
Updated the patch with the following:
#2
Looks good but I'd prefer keeping the JSON Content-type to text/javascript so the output can be displayed in browsers. Any reason you changed it to application/json?
#3
It avoids a security vulnerability:
http://jibbering.com/blog/?p=514
And "application/json" is in the standard:
http://www.ietf.org/rfc/rfc4627.txt
"text/javascript" may have been necessary in the past with the older render code because of trailing commas that are illegal in JSON but since rendering has improved the proper content type should be used.
Thoughts ?
Also, here's a newer version of the patch that fixes a bug. It removes a call to a non-existent function in the MIT Simile/Exhibit JSON preprocess step.
#4
Ok cool, if that's the standard. Committed to DRUPAL-6--1 branch.