LSF.Net.JSON = {
    
    encode : function (data) {
    	var encoder = new LSF.Net.JSON.encoder (data, 25);
    	return encoder.encode (data);
    },

    decode : function (string) {
    	return string && string.length > 0 ? eval ("(" + string + ")") : undefined;
    },
        
    request : function (url, data, callback, method) {
    	return LSF.Callback.Helper (callback, function (newCallback) {
    		return LSF.Net.Ajax.request (url, data === null ? null : LSF.Net.JSON.encode (data), newCallback, method);
    	}, LSF.Net.JSON.decode);
    }
}

LSF.Net.JSON.encoder = new Class ({
	
	__construct : function (source, maxDepth) {
		this.source = source;
		this.maxDepth = maxDepth || 25;
		this.depth = 0;
		this.pieces = [];
		this.piecesLength = 0;
	},
	
	escapeSymbols : {
		'\b' : '\\b',
		'\f' : '\\f',
		'\n' : '\\n',
		'\r' : '\\r',
		'\t' : '\\t'
	},
		
	addPiece : function (piece) {
		this.pieces[this.piecesLength++] = piece;
	},
				
	parseSource : function (cVar) {
		this.depth++;
		if (this.depth > this.maxDepth) return this.addPiece ('null');
		
		switch (typeof cVar) {
	        case 'boolean': return this.addPiece (String (cVar));
	        case 'number': return this.addPiece (isFinite (cVar) ? +cVar : 'null');
	        case 'string':
	        	var len = cVar.length;
	        	this.addPiece ('"');
	        	for (var i = 0; i < len; i++) {
	        		var ch = cVar.charAt (i);
	        		if (ch < ' ') {
	        			var escSymbol = this.escapeSymbols [ch];
	        			if (escSymbol) {
	        				this.addPiece (escSymbol);
	        			} else {
	        				ch = ch.charCodeAt();
	                        this.addPiece (
	                        	'\\u00' + 
	                        	Math.floor(ch / 16).toString(16) +
	                            (ch % 16).toString(16)
	                        );
	        			}
	        		} else {
	                    if (ch == '\\' || ch == '"') this.addPiece ("\\");
	                    this.addPiece (ch);
	        		}
	        	}
	        	return this.addPiece ('"');
	        case 'object':
	        	if (!cVar) return this.addPiece ('null');
	        	if (cVar instanceof Array) {
	        		var len = cVar.length;
	        		this.addPiece ('[');
	        		for (var i = 0; i < len; i++) {
	        			if (i) this.addPiece (',');
	        			this.parseSource (cVar[i]);
	        			this.depth--;
	        		}
	        		return this.addPiece (']');
	        	}
	        	if (typeof cVar.toString == 'undefined') return this.addPiece ('null');
	        	this.addPiece ('{');
				var isFirst = true;
				for (var i in cVar) {
					var obj = cVar[i];
					var oType = typeof obj;
					if (/*cVar.hasOwnProperty (obj) &&*/ oType != 'undefined' && oType != 'function') {
						if (isFirst) isFirst = false; else this.addPiece (',');
						this.parseSource (i);
	        			this.depth--;
						this.addPiece (':');
						this.parseSource (obj);
	        			this.depth--;
					}
				}
	        	return this.addPiece ('}');
	        	
		}
		return this.addPiece ('null');
	},
	
	encode : function() {		
		this.parseSource (this.source);
		return this.pieces.join ('');
	}

});

LSF.quickAccess ('json_encode', LSF.Net.JSON.encode);
LSF.quickAccess ('json_decode', LSF.Net.JSON.decode);
LSF.quickAccess ('json_request', LSF.Net.JSON.request);
