/*
  Copyright (c) 2004 Jan-Klaas Kollhof
  
  This file is part of the JavaScript o lait library(jsolait).
  
  jsolait is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.
 
  This software 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.
 
  You should have received a copy of the GNU Lesser General Public License
  along with this software; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/**
    Module for converting JavaScript objects to JSON (json.org).
    This is petty much creating JS source code for the objects.
    To customize JSON marshalling just overwrite the _toJSON method in your class.
*/
Module("json", "0.1.0", function(thisMod){
    
    /**
        Turns JSON code into JavaScript objects.
        @param src  The source as a String.
    */
    thisMod.unmarshall=function(src){
        var obj;
        eval("obj = " + src);
        return obj;
    }
    
    /**
        Turns an object into JSON.
        This is the same as calling obj._toJSON();
        @param obj  The object to marshall.
    */
    thisMod.marshall=function(obj){
        return obj._toJSON();
    }
    
    /**
        Converts an object to JSON.
    */
    Object.prototype._toJSON = function(){
        var v=[];
        for(attr in this){
            if(typeof this[attr] != "function"){
                v.push('"' + attr + '": ' + this[attr]._toJSON());
            }
        }
        return "{" + v.join(", ") + "}";
    }
    
    /**
        Converts a String to JSON.
    */
    String.prototype._toJSON = function(){
        return '"' + this.replace(/(["\\])/g, '\\$1') + '"';
    }
    
    /**
        Converts a Number to JSON.
    */
    Number.prototype._toJSON = function(){
        return this.toString();
    }
    
    /**
        Converts a Boolean to JSON.
    */
    Boolean.prototype._toJSON = function(){
        return this.toString();
    }
    
    /**
        Converts a Date to JSON.
        Date representation is not defined in JSON.
    */
    Date.prototype._toJSON= function(){
        var padd=function(s, p){
            s=p+s
            return s.substring(s.length - p.length)
        }
        var y = padd(this.getUTCFullYear(), "0000");
        var m = padd(this.getUTCMonth() + 1, "00");
        var d = padd(this.getUTCDate(), "00");
        var h = padd(this.getUTCHours(), "00");
        var min = padd(this.getUTCMinutes(), "00");
        var s = padd(this.getUTCSeconds(), "00");
        
        var isodate = y +  m  + d + "T" + h +  ":" + min + ":" + s
        
        return '"' + isodate + '"';
    }
    
    /**
        Converts an Array to JSON.
    */
    Array.prototype._toJSON = function(){
        var v = [];
        for(var i=0;i<this.length;i++){
            v.push(this[i]._toJSON()) ;
        }
        return "[" + v.join(", ") + "]";
    }
})

