Index: rest_server.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/rest_server/Attic/rest_server.module,v retrieving revision 1.1.2.1.2.2 diff -u -r1.1.2.1.2.2 rest_server.module --- rest_server.module 12 Mar 2009 17:21:07 -0000 1.1.2.1.2.2 +++ rest_server.module 27 Aug 2009 01:13:59 -0000 @@ -74,8 +74,21 @@ $result = rest_server_server_error($e->getMessage()); } - drupal_set_header('Content-type: text/xml'); - print rest_server_to_xml($result); + // Default to xml. + if (!isset($request['format'])) { + $request['format'] = 'xml'; + } + + switch ($request['format']) { + case 'json': + drupal_set_header('Content-type: application/json'); + print rest_server_to_js($result); + break; + case 'xml': + drupal_set_header('Content-type: text/xml'); + print rest_server_to_xml($result); + break; + } } /** @@ -92,6 +105,24 @@ } /** + * Convert data array to JSON representation. + * + * This function was copied from Drupal 7 code, and make use of PHP 5 JSON + * function. It makes sense to require PHP 5 here for performance since the XML + * encoder already requires PHP 5+ SimpleXML objects. + * + * @param $var + * Array or object with data to convert. + * + * @return + * String, JSON representation. + */ +function rest_server_to_js($var) { + // json_encode() does not escape <, > and &, so we do it with str_replace(). + return str_replace(array("<", ">", "&"), array('\x3c', '\x3e', '\x26'), json_encode($var)); +} + +/** * Convert data array to xml representation. * * @param $data