New server module - JSON server!

Events happening in the community are now at Drupal community events on www.drupal.org.
Posted by dmitrig01 on July 12, 2007 at 10:19pm

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

Posted by robloach on July 13, 2007 at 9:40pm

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

Sweet!

Posted by snelson on July 15, 2007 at 2:42am

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...

Posted by dmitrig01 on July 17, 2007 at 4:43am

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...

Posted by dmitrig01 on July 17, 2007 at 4:44am

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

Echo Example

Posted by robloach on July 17, 2007 at 7:20pm

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

Posted by dmitrig01 on July 22, 2007 at 1:53am

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

Oops!

Posted by dmitrig01 on August 20, 2007 at 3:18am

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:

AltStyle によって変換されたページ (->オリジナル) /