/* * JSON-RPC JavaScript client * * $Id: jsonrpc.js,v 1.3 2005/03/15 01:37:15 jamesgbritt Exp $ * * Copyright (c) 2003-2004 Jan-Klaas Kollhof * Copyright (c) 2005 Michael Clark, Metaparadigm Pte Ltd * * This code is based on Jan-Klaas' JavaScript o lait library (jsolait). * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public (LGPL) * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details: http://www.gnu.org/ * */ // TODO: Add async // TODO: Add HTTP auth (user, password) Object.prototype.toJSON = function Object_toJSON() { var v = []; for(attr in this) { if(this[attr] == null) v.push("\"" + attr + "\": null"); else if(typeof this[attr] == "function"); // skip else v.push("\"" + attr + "\": " + this[attr].toJSON()); } return "{" + v.join(", ") + "}"; } String.prototype.toJSON = function String_toJSON() { return "\"" + this.replace(/([\"\\])/g, "\\$1") + "\""; } Number.prototype.toJSON = function Number_toJSON() { return this.toString(); } Boolean.prototype.toJSON = function Boolean_toJSON() { return this.toString(); } Date.prototype.toJSON = function Date_toJSON() { this.valueOf().toString(); } Array.prototype.toJSON = function Array_toJSON() { var v = []; for(var i=0; i