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:

  1. 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.
  2. Reorganized the style settings screen.
  3. Changed to the array form of str_replace() in views_json_encode_special_chars().
  4. Empty fields are now included. I don't know why they were originally excluded.
  5. Now using the "application/json" content type.
  6. 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.

AttachmentSize
views_datasource_json_improvements.patch19.58 KB

#1

icylace - November 9, 2009 - 08:47

Updated the patch with the following:

  1. Renamed views_json_strip_illegal_chars() to views_json_check_label(). It's less of a misnomer and more informative of its intended use.
  2. Corrected function comments for views_json_check_label().
  3. Using views_json_check_label() on object labels as it should be.
AttachmentSize
627384_views_datasource_json_improvements_v2.patch 20.25 KB

#2

allisterbeharry - November 12, 2009 - 05:03
Assigned to:Anonymous» allisterbeharry
Status:needs review» postponed (maintainer needs more info)

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

icylace - November 12, 2009 - 16:17

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.

AttachmentSize
627384_views_datasource_json_improvements_v3.patch 20.22 KB

#4

allisterbeharry - November 16, 2009 - 04:46
Status:postponed (maintainer needs more info)» fixed

Ok cool, if that's the standard. Committed to DRUPAL-6--1 branch.

 
 

Drupal is a registered trademark of Dries Buytaert.