[development] drupal_to_js

Jeff Robbins lists at jjeff.com
Sat Jan 28 18:16:10 UTC 2006


Jeff,

Can you expand on how you're using JSON with Drupal? I've made some  
expansion on the drupal_to_js() function for use in the Scriptaculous/ 
Prototype module that I'm writing. It adds support for anonymous (non- 
keyed) arrays and non-quoted data for writing function data. I wasn't  
sure if this was generic enough for a core patch or not, so let me  
know if it meets your needs as well. And perhaps someone more  
familiar with core JS would like to let me know if this is a good or  
a bad thing to have in core. Here's the code:

/**
* Modification of drupal_to_js()
* adds support for anonymous arrays & function (non-quoted) strings
*
* prefix key with # to prevent quoting of value
*
* @example
* spajax_to_js(
*   array('containment' => array('element_1', 'element_2'),  
'#onUpdate' => 'function(){alert("hi");}'));
* returns:
*   {containment: ['element_1', 'element_2'], onUpdate: function() 
{alert("hi");}}
*
* @param mixed $var
*   any type of variable
*
* @param string $key
*   used internally
*
* @return string
*   a javascript representation of the variable
*/
function spajax_to_js($var, $key = NULL) {
   switch (gettype($var)) {
     case 'boolean':
     case 'integer':
     case 'double':
       return $var;
     case 'resource':
     case 'string':
       // if the key for this variable starts with a #, don't quote it
       if ($key{0} == '#') {
         $output = $var;
       }
       else {
         $output = str_replace(array("\r", "\n"), array('\r', '\n'),  
addslashes($var));
         $output = '"'. $output .'"';
       }
       return $output;
     case 'array':
       //check to see if there are non-numeric keys
       $keys = false;
       foreach(array_keys($var) as $key){
         if(!is_numeric($key)){
           $keys = true;
           break;
         }
       }
       if (!$keys) {
         foreach($var as $v) {
           $output[] = spajax_to_js($v);
         }
         return '[ '.implode(', ', $output) .' ]';
       }
       // if it's an array with keys, continue to object treatment
     case 'object':
       $output = array();
       foreach ($var as $k => $v) {
         if ($k{0} == '#'){
           $ok = substr($k, 1);
         }
         else {
           $ok = $k;
         }
         $output[] = $ok .': '. spajax_to_js($v, $k);
       }
       return '{ '. implode(', ', $output) .' }';
     default:
       return 'null';
   }
}



On Jan 24, 2006, at 1:59 PM, Jeff Griffiths wrote:

> I submitted a bug a while ago while doing some drupal dev for work:
>
> http://drupal.org/node/42794
>
> It would be cool if this was looked at prior to 4.7 final so that the
> released drupal / JSON implementation is more useful, and in
> particular functions like PHP-JSON. This will allow people
> implementing AJAX / JSON stuff in drupal to optionally compile and use
> PHP-JSON if they want the performance, without code changes.
>
> cheers, JeffG



More information about the development mailing list