JSON Server adds unwanted backslashes

Events happening in the community are now at Drupal community events on www.drupal.org.
windsurf's picture

I'm using JSON in Flash (because I'm also using JSON for iPhone and wanted to keep things consistent rather than mixing amf and JSON). I noticed that when I started using the JSON Server for the Services module that strings with apostrophes were getting slashes where they didn't belong. Check out the return differences between these:

<?php
//outputs didn't
echo json_encode("didn't");

//outputs didn\'t
echo drupal_to_js("didn't");
?>

In Flash if you're using as3corelib's JSON.decode() method you'll retain the unwanted slashes - which was a problem I was having. I assume the same problem will arise in the iPhone environment as well.

The JSON Server uses drupal_to_js to return a JSON string rather than the native php json_encode. I'm not sure if this is to remove dependency on the php JSON library or for increased compatibility with regards to HTML (AJAX) consumption but it isn't want we want when the consumer of the JSON output is Flash or Objective-C (iPhone).

Solution:
This might break an AJAX consumer if my assumption above is correct but it fixes Flash consumers:

In json_server.module do a search and replace, changing all instances of drupal_to_js to json_encode

Hope this saves someone some time.

Cheers,

Andrew

Services

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: