New server module - JSON server!

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

http://drupal.org/project/json_server
Update: I am now writing about it!
The JSON server is a way to integrate services with JSON. You call a simple function, Drupal.service, to execute a service for you, in the following way:

Drupal.service('service.name',
  {api_key: "123461823762348756293", sessid: "sdfjahsldjfhlaksuertybsi", extra_parameter: "asdfasdf", other_parameter: ["nodes", "are", "good"]},
  function(status, data) {
    if(status == false) {
      alert("FATAL ERROR!!!!!");
    }
    else {
      alert(data);
    }
  }
);

This makes ajaxy things really easy to do. I could build a website that showed me a view with all nodes in it with a couple of calls, and it'd be pretty easy. It would go like this:

theSite = function() {
  Drupal.service('views.getView',
    {view_name: "front_page", fields: ["title", "nid"]},
    function(status, data) {
      if(status == false) {
        alert("Fatal error: could not load content");
      }
      else {
        $('.back').unbind('click');
        $('#content-area').html("<ul>");
        for(i in data) {
          $('#content-area').append("<li><a class=\"node-load\" href=\"#\" name=\""+ data[i].nid +"\">"+ data[i].title +"</a></li>");
        }
        $('#content-area').append("</ul>");
        $('node-load').click(function() {       
          Drupal.service('node.load',
            {nid: $(this).attr("name"), fields: ["title", "body"]},
            function(status, data) {
              if(status == false) {
                alert("Fatal error: could not load content");
              }
              else {
                $('.node-load').unbind('click');
                $('#content_area').html("<h1>"+ data.title +"</h1><br /><p>"+ data.body +"</p><br /><a href=\"back\">Back</a>");
                $('.back').bind('click', theSite);
              }
            }
          );
        });
      }
    }
  );
}
$(document).ready(theSite);

Note: completely untested. However, this shows the basics of the JSON server.
What this would do (in theory) is make a list of nodes, and when you click on the name of a node, it would load and the full node would be shown, along with a back button that would take you back to the view.

Comments

Very cool

robloach's picture

I hope this gets more people interested in Drupal Web Services.

Sweet!

snelson's picture

I can't believe this is all it takes to parse json:

<?php
function drupal_parse_json($v) {
  if(
$v{0} == '"') {
   
$v = substr($v, 1, -1);
  }
  elseif(
$v{0} == '{') {
   
$var = explode(",", substr($v, 1, -2));
   
$v = array();
    foreach(
$var as $value) {
     
$va = explode(":", $value);
     
$v[] = drupal_parse_json($va[1]);
    }
  }
  return
$v;
}
?>

Does this handle all cases? I think I'd like to put that function into Services so that we can use json to pass complex arguments like arrays and objects in the service browser. I'll probably just name it something different ... it'd be nice to get it into Drupal for v6, unless they already have something going. We already have drupal_to_js, why not drupal_from_js()?

Good work,
Scott

problem...

dmitrig01's picture

there is one slight problem, which is I don't exactly know how perms work in services... since anyone can call Drupal.service('node.delete'... from firebug... I am wondering how perms work in services module.

aaaand...

dmitrig01's picture

I'm not sure it handles all cases, but it handles the cases that Drupal.toJson sends to it ;)

Echo Example

robloach's picture

The following is an example of using the Echo Service with the JSON server:

Drupal.service('echo.echo',
  {message: "Hello from JSON and Drupal!"},
  function(error, data) {
    if(error == false) {
      alert(data['message']);
    }
    else {
      alert("Fatal Error!");
    }
  }
);

I get an alert box saying "Hello from JSON and Drupal!" when I run this code through FireBug's console. Note that in the return, the first argument returns true when there's an error, and false when there isn't an error.

That's good

dmitrig01's picture

False = no error!!
that's correct.
the parameter is named error, after all...
if it were named noerror, they would be reversed

Oops!

dmitrig01's picture

Didn't realize I was naming it wrong in my examples. Fixed it

Services

Group organizers

Group categories

Group notifications

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