jsonrpc_lib.js
Summary
No overview generated for 'jsonrpc_lib.js'
Method Summary
|
static Object
|
jsonrpc_decode(jsonrpc_val, options)
Takes a json value in jsonrpcval object format
and translates it into native javascript types.
|
static false | jsonrpcresp | jsonrpcmsg | jsonrpcval
|
jsonrpc_decode_json(<string> json_val, <object> options)
Convert the json representation of a jsonrpc method call, jsonrpc method response
or single json value into the appropriate object (deserialize)
|
static Object
|
jsonrpc_encode(js_val, options)
Takes native javascript types and encodes them into jsonrpc object format.
|
function json_encode_entities(data, src_encoding, dest_encoding)
{
if (data == undefined)
{
return '';
}
return data.replace('\\', '\\\\').replace('"', '\\"').replace('/', '\\/').replace('\t', '\\t').replace('\n', '\\n').replace('\r', '\\r').replace('\b', '\\b').replace('\v', '\\v').replace('\f', '\\f');
}
function json_parse(data, return_jsvals, src_encoding, dest_encoding)
{
if (return_jsvals == undefined)
{
return_jsvals = false;
}
if (src_encoding == undefined)
{
src_encoding = 'UTF-8';
}
if (dest_encoding == undefined)
{
dest_encoding = 'ISO-8859-1';
}
_xh['isf_reason'] = 'non-native JSON parsing not yet implemented.';
return false;
}
function json_parse_native(data)
{
try
{
var out = eval('(' + data + ')');
_xh['value'] = out;
return true;
}
catch (e)
{
_xh['isf_reason'] = 'JSON parsing failed';
return false;
}
}
function jsonrpc_parse_resp(data, return_jsvals, use_native_parsing)
{
if (return_jsvals == undefined)
{
return_jsvals = false;
}
if (use_native_parsing == undefined)
{
use_native_parsing = true;
}
_xh['isf'] = 0;
_xh['isf_reason'] = '';
if (use_native_parsing)
{
var ok = json_parse_native(data);
}
else
{
var ok = json_parse(data, return_jsvals);
}
if (ok)
{
if (typeof(_xh['value']) != 'object' || _xh['value']['result'] === undefined
|| _xh['value']['error'] === undefined || _xh['value']['id'] === undefined)
{
_xh['isf_reason'] = 'JSON parsing did not return correct jsonrpc response object';
return false;
}
var d_error = _xh['value']['error'];
_xh['id'] = _xh['value']['id'];
if (d_error != null)
{
_xh['isf'] = 1;
if (typeof(d_error) == 'object' && d_error['faultCode'] !== undefined
&& d_error['faultString'] !== undefined)
{
if(d_error['faultCode'] == 0)
{
d_error['faultCode'] = -1;
}
_xh['value'] = d_error;
}
else
{
if (return_jsvals)
{
_xh['value'] = {'faultCode': -1, 'faultString': var_export(_xh['value']['error'])};
}
else
{
_xh['value'] = {'faultCode': -1, 'faultString': serialize_jsonrpcval(jsonrpc_encode(_xh['value']['error']))};
}
}
}
else
{
if (!return_jsvals)
_xh['value'] = jsonrpc_encode(_xh['value']['result']);
else
_xh['value'] = _xh['value']['result'];
}
return true;
}
else
{
return false;
}
}
function jsonrpc_client(path, server, port, method)
{
this.no_multicall = true;
this.return_type = 'jsonrpcvals';
this.init(path, server, port, method);
}
jsonrpc_client.prototype = new xmlrpc_client();
function jsonrpcmsg(meth, pars, id)
{
this.id = null;
this.params = [];
this.content_type = 'application/json';
if(id !== undefined)
{
this.id = id;
}
this.init(meth, pars);
}
jsonrpcmsg.prototype = new xmlrpcmsg();
jsonrpcmsg.prototype.parseResponse = function (data, headers_processed, return_type)
{
var headers = '';
if (headers_processed === undefined)
{
headers_processed = false;
}
else if (typeof(headers_processed) == 'string')
{
headers = headers_processed;
headers_processed = true;
}
if (return_type === undefined)
{
return_type = 'jsonrpcvals';
}
if (this.debug)
{
xmlrpc_debug_log('<PRE>---GOT---\n' + htmlentities(data) + '\n---END---\n</PRE>');
}
if (data == '')
{
xmlrpc_error_log('XML-RPC: jsonrpcmsg::parseResponse: no response received from server.');
var r = new jsonrpcresp(0, xmlrpcerr['no_data'], xmlrpcstr['no_data']);
return r;
}
_xh = {headers: [], cookies: []};
var raw_data = data;
if (headers != '')
{
var r = this.parseResponseHeaders(headers, true);
}
else if (data.slice(0, 4) == 'HTTP')
{
var r = this.ParseResponseHeaders(data, headers_processed);
if (typeof(r) !== 'string')
{
r.raw_data = data;
return r;
}
else
{
data = r;
}
}
if (this.debug)
{
var start = data.indexOf('/* SERVER DEBUG INFO (BASE64 ENCODED):');
if (start != -1)
{
start += 39;
var end = data.indexOf('*/', start);
var comments = data.slice(start, end-1);
xmlrpc_debug_log('<PRE>---SERVER DEBUG INFO (DECODED)---\n\t'+htmlentities(base64_decode(comments).replace(/\n/g, '\n\t'))+'\n---END---\n</PRE>');
}
}
data = data.replace(/^\s/, '').replace(/\s$/, '');
var pos = data.lastIndexOf('}');
if (pos >= 0)
{
data = data.slice(0, pos+17);
}
if (return_type == 'json')
{
var r = new jsonrpcresp(data, 0, '', 'json');
r.hdrs = _xh['headers'];
r._cookies = _xh['cookies'];
r.raw_data = raw_data;
return r;
}
if (!jsonrpc_parse_resp(data, return_type=='jsvals'))
{
if (this.debug)
{
}
var r = new jsonrpcresp(0, xmlrpcerr['invalid_return'],
xmlrpcstr['invalid_return'] + ' ' + _xh['isf_reason']);
}
else
{
var v = _xh['value'];
if (this.debug)
{
xmlrpc_debug_log("<PRE>---PARSED---\n");
xmlrpc_debug_log(var_export(v));
xmlrpc_debug_log("\n---END---</PRE>");
}
if(_xh['isf'])
{
var r = new jsonrpcresp(0, v['faultCode'], v['faultString']);
}
else
{
var r = new jsonrpcresp(v, 0, '', return_type);
}
r.id = _xh['id'];
}
r.hdrs = _xh['headers'];
r._cookies = _xh['cookies'];
r.raw_data = raw_data;
return r;
}
jsonrpcmsg.prototype.createPayload = function (charset_encoding)
{
this.payload = '{\n"method": "' + json_encode_entities(this.methodname, '', charset_encoding) + '",\n"params": [ ';
for(var i = 0; i < this.params.length; ++i)
{
this.payload += '\n ' + serialize_jsonrpcval(this.params[i], charset_encoding) + ',';
}
this.payload = this.payload.slice(0, -1) + '\n],\n"id": ' + (this.id == null ? 'null' : this.id) + '\n}\n';
}
function jsonrpcresp(val, fcode, fstr, valtyp)
{
this.id = null;
this.init(val, fcode, fstr, valtyp);
}
jsonrpcresp.prototype = new xmlrpcresp();
jsonrpcresp.prototype.serialize = function (charset_encoding)
{
this.payload = serialize_jsonrpcresp(this, this.id, charset_encoding);
return this.payload;
}
function jsonrpcval(val, type)
{
this.init(val, type);
}
jsonrpcval.prototype = new xmlrpcval();
jsonrpcval.prototype.serialize = function (charset_encoding)
{
return serialize_jsonrpcval(this, charset_encoding);
}
function jsonrpc_decode(jsonrpc_val, options)
{
switch(jsonrpc_val.kindOf())
{
case 'scalar':
return jsonrpc_val.scalarVal();
case 'array':
var size = jsonrpc_val.arraySize();
var arr = [];
for(var i = 0; i < size; ++i)
{
arr[arr.length] = jsonrpc_decode(jsonrpc_val.arrayMem(i), options);
}
return arr;
case 'struct':
if ((options != undefined && options['decode_js_objs']) && jsonrpc_val._js_class != '')
{
var obj = new jsonrpc_val._js_class;
}
else
{
var obj = {};
}
for(var key in jsonrpc_val.me)
{
obj[key] = jsonrpc_decode(jsonrpc_val.me[key], options);
}
return obj;
case 'msg':
var paramcount = jsonrpc_val.getNumParams();
var arr = [];
for(var i = 0; i < paramcount; ++i)
{
arr[arr.lenght] = jsonrpc_val(jsonrpc_val.getParam(i));
}
return arr;
}
}
function jsonrpc_encode(js_val, options)
{
var type = typeof js_val;
switch(type)
{
case 'string':
var jsonrpc_val = new jsonrpcval(js_val, 'string');
break;
case 'number':
var num = new Number(js_val);
if (num == parseInt(num))
{
var jsonrpc_val = new jsonrpcval(js_val, 'int');
}
else
{
var jsonrpc_val = new jsonrpcval(js_val, 'double');
}
break;
case 'boolean':
var jsonrpc_val = new jsonrpcval(js_val, 'boolean');
break;
case 'object':
if (js_val === null)
{
var jsonrpc_val = new jsonrpcval(null, 'null');
}
else
if (js_val.toJsonRpcVal)
{
var jsonrpc_val = js_val.toJsonRpcVal();
}
else
if (js_val instanceof Array)
{
var arr = [];
for(var i = 0; i < js_val.length; ++i)
{
arr[arr.length] = jsonrpc_encode(js_val[i], options);
}
var jsonrpc_val = new jsonrpcval(arr, 'array');
}
else
{
var arr = {};
for(var attr in js_val)
{
if (typeof js_val[attr] != 'function')
{
arr[attr] = jsonrpc_encode(js_val[attr], options);
}
}
var jsonrpc_val = new jsonrpcval(arr, 'struct');
}
break;
default:
var jsonrpc_val = new jsonrpcval();
break;
}
return jsonrpc_val;
}
function jsonrpc_decode_json(json_val, options)
{
_xh = {};
if (!json_parse_native(json_val))
{
xmlrpc_error_log(_xh['isf_reason']);
return false;
}
else
{
if (typeof(_xh['value']) == 'object' )
{
if ( _xh['value']['result'] !== undefined
&& _xh['value']['error'] !== undefined && _xh['value']['id'] !== undefined)
{
if (_xh['value']['error'] != null)
{
if (typeof(_xh['value']['error']) == 'object' && _xh['value']['error']['faultCode'] !== undefined
&& _xh['value']['error']['faultString'] !== undefined)
{
if (_xh['value']['error']['faultCode'] == 0)
{
_xh['value']['error']['faultCode'] = -1;
}
}
else
{
_xh['value'] = {'faultCode': -1, 'faultString': var_export(_xh['value']['error'])};
}
var r = new jsonrpcresp(0, _xh['value']['faultCode'], _xh['value']['faultString']);
}
else
{
var r = new jsonrpcresp(jsonrpc_encode(_xh['value']['result']));
}
r.id = _xh['value']['id'];
return r;
}
else if ( _xh['value']['method'] !== undefined
&& _xh['value']['params'] !== undefined && _xh['value']['id'] !== undefined)
{
var r = new jsonrpcmsg(_xh['value']['method'], null, _xh['value']['id']);
for (var i = 0; i < _xh['value']['params'].length; i++)
r.addParam(jsonrpc_encode(_xh['value']['params'][i]));
return r;
}
}
return jsonrpc_encode(_xh['value']);
}
}
function serialize_jsonrpcresp (resp, id, charset_encoding)
{
var result = '{\n"id": ' + (id == undefined ? 'null' : id) + ', ';
if(resp.errno)
{
result += '"error": { "faultCode": ' + resp.errno + ', "faultString": "' + json_encode_entities(resp.errstr, null, charset_encoding) + '" }, "result": null';
}
else
{
if(typeof resp.val != 'object' || !(resp.val instanceof xmlrpcval))
{
if(typeof resp.val == 'string' && resp.valtyp == 'json')
{
result += '"error": null, "result": ' + resp.val;
}
else
{
}
}
else
{
result += '"error": null, "result": ' +
serialize_jsonrpcval(resp.val, charset_encoding);
}
}
result += '\n}';
return result;
}
function serialize_jsonrpcval (value, charset_encoding)
{
var rs = '';
switch(value.mytype)
{
case 1:
rs += '"' + json_encode_entities(value.me, null, charset_encoding) + '"';
break;
case 4:
if(isFinite(value.me))
{
rs += value.me.toFixed();
}
else
{
rs += '0';
}
break;
case 5:
if(isFinite(value.me) && value.me !== null)
{
rs += value.me.toString();
var num = new Number(value.me);
if(num == parseInt(num))
{
rs += '.0';
}
}
else
{
rs += '0';
}
break;
case 6:
rs += (value.me ? 'true' : 'false');
break;
case 7:
rs += '"' + value.me + '"';
break;
case 8:
rs += '"' + base64_encode(value.me) + '"';
break;
case 9:
rs += "null";
break;
case 2:
rs += "[";
len = (value.me.length);
if(len)
{
for(var i = 0; i < len-1; ++i)
{
rs += serialize_jsonrpcval(value.me[i], charset_encoding);
rs += ",";
}
rs += serialize_jsonrpcval(value.me[i], charset_encoding);
}
rs += "]";
break;
case 3:
for(var val in value.me)
{
rs += ',"' + json_encode_entities(val, null, charset_encoding) + '":';
rs += serialize_jsonrpcval(value.me[val], charset_encoding);
}
rs = '{' + rs.slice(1) + '}';
break;
}
return rs;
}
Documentation generated by
JSDoc on Tue Jun 26 14:36:04 2007