AJAX+JSON RPC

Moritz Warning moritzwarning at _nospam_web.de
Wed Mar 12 16:54:35 PDT 2008


I thought some might be interested in a flexible JSON RPC
(AJAX with JSON instead of XML).

http://web-gmui.svn.sourceforge.net/viewvc/web-gmui/trunk/json/JsonCore.d?
revision=201&view=markup

class Foo
{
   Foo[] getFoos() { return [new Foo, new Foo]; }

   char[] getName() { return "myname"; }

   uint getId(uint i) { return i; }

   void advanced(char[] a, char[] b) {}
}

void main(char[][] args)
{
   //don't need to be called, only instantiated.
   add!("Foo.getId");
   add!("Foo.getName", "name"); //we set an optional alias
   add!("Foo.getFoos");
   add!("Foo.advanced");

   Foo foo = new Foo;

   //the string we could get from some client side JavaScript
   char[] input = "getFoos{getId(13),name}";

   //the actual parsing
   char[] output = Parse!(Foo).parse(o, foo, input);

  /*output is now
  * `{0 : {getId : 13, name : "myname"}, 1 : { getId : 13, name : 
"myname"}}`);
   */
}

The call Syntax is not JSON compatible, but the returned data is.
add! accepts functions, class methods and global variables.
An optional alias can also be set.
You can call functions/methods with strings and numbers and arrays of 
those.
Some examples:


`getFoos[getId(13),name]`
=>
`[{getId : 13, name : "myname"}, { getId : 13, name : "myname"}]`.


`advanced("x","y"),name()bar`
=>
`{advanced: null, bar : "myname"}`.


More information about the Digitalmars-d-announce mailing list