|
From: Thyamad c. <th...@us...> - 2005-12-03 19:13:53
|
Update of /cvsroot/thyapi/thyapi/thyutils In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9807/thyutils Modified Files: thyjsonprotocol.js Log Message: Commiting file additions and modification from SVN revision 2388 to 2389... Changes made by vinicius on 2005-12-03 20:59:32 +0100 (Sat, 03 Dec 2005) corresponding to SVN revision 2389 with message: Changes in ThyJSONProtocol to work in Internet Explorer Index: thyjsonprotocol.js =================================================================== RCS file: /cvsroot/thyapi/thyapi/thyutils/thyjsonprotocol.js,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** thyjsonprotocol.js 27 Oct 2005 02:36:09 -0000 1.2 --- thyjsonprotocol.js 3 Dec 2005 19:13:36 -0000 1.3 *************** *** 19,23 **** * Class: thyJSONProtocol * ! * This class implements the JSON specific methods. * */ --- 19,25 ---- * Class: thyJSONProtocol * ! * This class implements the JSON-RPC specific methods. ! * ! * See http://json-rpc.org/specs.xhtml * */ *************** *** 41,54 **** * * method - The name of the method to be called ! * params - The params to be passed to this method * */ p.encode = function (method, params) { ! params = params || {}; ! params['run'] = method; ! return params.toSource(); } /** * Method: decode --- 43,63 ---- * * method - The name of the method to be called ! * params - The params to be passed to this method. Always a javascript object * */ p.encode = function (method, params) { ! var call_array = {}; ! ! call_array['method'] = method; ! call_array['params'] = params; ! //id is not needed in ThyAPI, because one xmlhttprequest object is ! //created for each call to the server. So, the method that will receive ! //the response is the only method associated with the ! ! return this._encodeValue(call_array); } + /** * Method: decode *************** *** 66,70 **** try {*/ ! var ret = eval(response.text); /* } catch(err) --- 75,79 ---- try {*/ ! var ret = eval('('+response.text+')'); /* } catch(err) *************** *** 72,76 **** throw({result: 'Invalid JSON data', location: 'thyJSONProtocol.decode'}); }*/ ! return ret; } --- 81,100 ---- throw({result: 'Invalid JSON data', location: 'thyJSONProtocol.decode'}); }*/ ! ! //error ! if (typeof(ret) != 'object') ! { ! throw({result: 'Invalid JSON data', location: 'thyJSONProtocol.decode'}); ! return null; ! } ! ! if (ret['error']) ! { ! throw({result: ret['error'], location: 'thyJSONProtocol.decode'}); ! return null; ! } ! ! ! return ret['result']; } *************** *** 84,87 **** --- 108,112 ---- { //http://www.aminus.org/blogs/index.php/fumanchu/2005/05/18/content_type_text_json + //http://groups.yahoo.com/group/json-rpc/message/125 return 'text/x-json'; } *************** *** 98,99 **** --- 123,196 ---- } + + /** + * Method: _encodeValue + * + * Encodes a javascript element into a json string + */ + p._encodeValue = function(element) + { + var typeof_element = typeof(element); + + if (element == null) + { + return 'null'; + } + + switch (typeof_element) + { + case 'object': + return this._encodeObject(element); + case 'array': + return this._encodeArray(element); + case 'boolean': + return element.toString(); + case 'number': + return element.toString(); + case 'string': + return this._encodeString(element); + case 'date': + return this._encodeDate(element); + case 'function': + // FIXME: Is this necessary? + return '""'; + } + } + + p._encodeObject = function(element) + { + var v=[]; + for(attr in element) + { + if(typeof (element[attr]) != "function") + { + v.push('"' + attr + '": ' + this._encodeValue(element[attr])); + } + } + return "{" + v.join(", ") + "}"; + } + + p._encodeArray = function(element) + { + var v = []; + for(var i=0;i<element.length;i++) + { + v.push(this._encodeValue(element[i])) ; + } + return "[" + v.join(", ") + "]"; + } + + p._encodeString = function(element) + { + var s = '"' + element.replace(/(["\\])/g, '\\$1') + '"'; + s = s.replace(/(\n)/g,"\\n"); + return s; + } + + //viniciuscb: as in the writing of this there was not found a pattern for + //date definition, the return will be an unix timestamp + p._encodeDate = function(element) + { + return Date.UTC(element.getFullYear(),element.getMonth(),element.getDay(),element.getHours(),element.getMinutes(),element.getSeconds())/1000; + } + |