[z3-checkins] r27084 - z3/jsonserver/branch/merge/browser

reebalazs at codespeak.net reebalazs at codespeak.net
Thu May 11 17:35:39 CEST 2006


Author: reebalazs
Date: Thu May 11 17:35:38 2006
New Revision: 27084

Modified:
   z3/jsonserver/branch/merge/browser/json.js
Log:
Implement JSON-RPC 1.1

Modified: z3/jsonserver/branch/merge/browser/json.js
==============================================================================
--- z3/jsonserver/branch/merge/browser/json.js	(original)
+++ z3/jsonserver/branch/merge/browser/json.js	Thu May 11 17:35:38 2006
@@ -123,6 +123,46 @@
 
 var _dummyRequestManager = new _DummyRequestManager();
 
+function JSONSig(params, allowSupplement) {
+	// Params are those submitted to the rpc method
+	// Params can contain _JsonNamedWrapper and _JsonSupplement items
+	// they must be sorted out
+	// and finally the result is converted to the 1.1 marshall format
+	// that only needs to be jsonized after
+	this.supplementWrapper = null;
+	var args = new Array();
+	for(var i=0;i<params.length;i++){
+		var arg = params[i];
+		if (arg.isSupplement) {
+			if (! allowSupplement) {
+				throw new json.JSONRPCError("Supplement args are not allowed in this call.");
+			}
+			if (this.supplementWrapper != null) {
+				throw new json.JSONRPCError("Multiple supplement args, only one is supported");
+			}
+			this.supplementWrapper = arg;
+		} else {
+			args.push(arg);
+		}
+	}
+	// now handle the last arg.
+	// If it is an object, means we have parameters.
+	if (args.length >= 1 && typeof(args[args.length-1]) == 'object' && ! args[args.length-1].toJSON) {
+		// we have named parms!
+		this.args = {};
+		var kw = args[args.length-1];
+		for (var key in kw) {
+			this.args[key] = kw[key];
+		}
+		for (var i=0; i<args.length-1; i++) {
+			this.args[i.toString()] = args[i];
+		}
+	} else {
+		// just unnamed parms
+		this.args = args;
+	}
+}
+
 function JSONRPCMethod(url, methodName, callback, errHandler, timeout, supplementData, requestId, 
 			requestManager, user, pass) {
     this.methodName = methodName;
@@ -138,33 +178,21 @@
     var self = this;
 
     var fn = function(){
-		var supplementWrapper = null;
-        var args = new Array();
-        for(var i=0;i<arguments.length;i++){
-		    var arg = arguments[i];
-			if (arg.isSupplement) {
-				if (supplementWrapper != null) {
-            		throw new json.JSONRPCError("Multiple supplement args, only one is supported");
-				}
-				supplementWrapper = arg;
-			} else {
-            	args.push(arg);
-			}
-        }
+		var sig = new JSONSig(arguments, true);
 		var requestId = self.requestId;
 		var supplementData = self.supplementData;
-		if (supplementWrapper != null) {
-			var _id = supplementWrapper.getRequestId();
+		if (sig.supplementWrapper != null) {
+			var _id = sig.supplementWrapper.getRequestId();
 			if (_id != null) {
 				requestId = _id
-				}
-			var _data = supplementWrapper.getData();
+			}
+			var _data = sig.supplementWrapper.getData();
 			if (_data != null) {
 				supplementData = _data
-				}
 			}
+		}
         if(self.callback) {
-            var data = self.jsonRequest(requestId, self.methodName, args);
+            var data = self.jsonRequest(requestId, self.methodName, sig);
 			var do_postdata = function() {
 				self.postData(self.url, self.user, self.password, data, timeout, function(resp){
 					var res = null;
@@ -190,7 +218,7 @@
 			}
 			self.requestManager.notifyServer(do_postdata, self.url + '/' + self.methodName);
         } else {
-            var data = self.jsonRequest(requestId, self.methodName, args);
+            var data = self.jsonRequest(requestId, self.methodName, sig);
             var resp = self.postData(self.url, self.user, self.password, data, timeout);
             return self.handleResponse(resp);
         }
@@ -216,10 +244,10 @@
     }
 }
 
-JSONRPCMethod.prototype.jsonRequest = function(id, methodName, args){
+JSONRPCMethod.prototype.jsonRequest = function(id, methodName, sig){
     var ji = toJSON(id);
     var jm = toJSON(methodName);
-    var ja = toJSON(args);
+    var ja = toJSON(sig.args);
     return '{"id":' + ji + ', "method":' + jm + ', "params":' + ja + "}";
 }
 
@@ -229,11 +257,8 @@
 }
 
 JSONRPCMethod.prototype.notify = function(){
-    var args=new Array();
-    for(var i=0;i<arguments.length;i++){
-        args.push(arguments[i]);
-    }
-    var data = this.jsonRequest(null, this.methodName, args);
+	var sig = new JSONSig(arguments, false);
+    var data = this.jsonRequest(null, this.methodName, sig);
     this.postData(this.url, this.user, this.password, data, null, function(resp){});
 }
 


More information about the z3-checkins mailing list