//**************************************************************
//
// jQuery.json 1.0 
// Copyright (c) 2008 ÐìÒã
// http://www.ajaxinfo.cn
//**************************************************************
(function($){
    $.extend({
        toJsonString:function(value,filter){            
            return $.json.stringify(value,filter);
        },
        parseJson:function(value,filter){
            return $.json.parse(value, filter);
        },
        addJson:function(val){
            return $.json.add(val);
        }
    })
    $.jsonConvert = { 
        toJSON : function (key) {
            function f(n) {
                return n < 10 ? '0' + n : n;
            }
            return this.getUTCFullYear()   + '-' +
                 f(this.getUTCMonth() + 1) + '-' +
                 f(this.getUTCDate())      + 'T' +
                 f(this.getUTCHours())     + ':' +
                 f(this.getUTCMinutes())   + ':' +
                 f(this.getUTCSeconds())   + 'Z';
        },
        cx : /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
        escapeable : /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
        gap:'',
        indent:'',
        meta : {
                '\b': '\\b',
                '\t': '\\t',
                '\n': '\\n',
                '\f': '\\f',
                '\r': '\\r',
                '"' : '\\"',
                '\\': '\\\\'
            },
        rep:'',
        quote:function(string){
            $.jsonConvert.escapeable.lastIndex = 0;
            return $.jsonConvert.escapeable.test(string) ?
                '"' + string.replace($.jsonConvert.escapeable, function (a) {
                    var c = $.jsonConvert.meta[a];
                    if (typeof c === 'string') {
                        return c;
                    }
                    return '\\u' + ('0000' +
                            (+(a.charCodeAt(0))).toString(16)).slice(-4);
                }) + '"' :
                '"' + string + '"';
        },
        str:function(key, holder) {                
            var i,
                k,
                v,
                length,
                mind = $.jsonConvert.gap,
                partial,
                value = holder[key];                   
            if (value && typeof value === 'object' && typeof value.toJSON === 'function') {     
                value = value.toJSON(key);
            }                
            if (typeof $.jsonConvert.rep === 'function') {                          
                value = $.jsonConvert.rep.call(holder, key, value);
            }                            
            switch (typeof value) {
            case 'string':                                        
                return $.jsonConvert.quote(value);
            case 'number':                                
                return isFinite(value) ? String(value) : 'null';
            case 'boolean':
            case 'null':                    
                return String(value);
            case 'object':                                  
                if (!value) {
                    return 'null';
                }                    
                $.jsonConvert.gap += $.jsonConvert.indent;
                partial = [];                    
                if (typeof value.length === 'number' && !(value.propertyIsEnumerable('length'))) {
                    length = value.length;
                    for (i = 0; i < length; i += 1) {
                        partial[i] = $.jsonConvert.str(i, value) || 'null';
                    }
                    v = partial.length === 0 ? '[]' :
                        $.jsonConvert.gap ? '[\n' + $.jsonConvert.gap +
                                partial.join(',\n' + $.jsonConvert.gap) + '\n' +
                                    mind + ']' :
                              '[' + partial.join(',') + ']';
                    $.jsonConvert.gap = mind;
                    return v;
                }
                if ($.jsonConvert.rep && typeof $.jsonConvert.rep === 'object') {
                    length = $.jsonConvert.rep.length;
                    for (i = 0; i < length; i += 1) {
                        k = $.jsonConvert.rep[i];
                        if (typeof k === 'string') {
                            v = $.jsonConvert.str(k, value, $.jsonConvert.rep);
                            if (v) {
                                partial.push($.jsonConvert.quote(k) + ($.jsonConvert.gap ? ': ' : ':') + v);
                            }
                        }
                    }
                } else {
                    for (k in value) {
                        if (Object.hasOwnProperty.call(value, k)) {
                            v = $.jsonConvert.str(k, value, $.jsonConvert.rep);                                
                            if (v) {                                    
                                partial.push($.jsonConvert.quote(k) + ($.jsonConvert.gap ? ': ' : ':') + v);
                            }
                        }
                    }
                }                    
                v = partial.length === 0 ? '{}' :
                    $.jsonConvert.gap ? '{\n' + $.jsonConvert.gap +
                            partial.join(',\n' + $.jsonConvert.gap) + '\n' +
                            mind + '}' :
                          '{' + partial.join(',') + '}';
                $.jsonConvert.gap = mind;                    
                return v;
            }
        }
     }
    $.json = {
        stringify: function (value, replacer, space) {            
            var i;
            gap = '';
            indent = '';
            if (typeof space === 'number') {
                for (i = 0; i < space; i += 1) {
                    indent += ' ';
                }
            } else if (typeof space === 'string') {
                indent = space;
            }            
            $.jsonConvert.rep = replacer;            
            if (replacer && typeof replacer !== 'function' && (typeof replacer !== 'object' || typeof replacer.length !== 'number')) {
                throw new Error('JSON.stringify');
            }
            return $.jsonConvert.str('',{'':value});
        },
        parse: function (text, reviver) {
            var j;
            function walk(holder, key) {
                var k, v, value = holder[key];
                if (value && typeof value === 'object') {
                    for (k in value) {
                        if (Object.hasOwnProperty.call(value, k)) {
                            v = walk(value, k);
                            if (v !== undefined) {
                                value[k] = v;
                            } else {
                                delete value[k];
                            }
                        }
                    }
                }
                return reviver.call(holder, key, value);
            }               
            $.jsonConvert.cx.lastIndex = 0;
            if ($.jsonConvert.cx.test(text)) {
                text = text.replace($.jsonConvert.cx, function (a) {
                    return '\\u' + ('0000' +
                            (+(a.charCodeAt(0))).toString(16)).slice(-4);
                });
            }
            if (/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {            
                j = eval('(' + text + ')');                
                return typeof reviver === 'function' ? walk({'': j}, '') : j;
            }            
            throw new SyntaxError('JSON.parse');
        },
        add:function(val){
            switch(typeof val){
                case 'string':
                    $(val).each(function(i){
                        alert($(this).attr('type'))
                    })
                    return 'string';
                case 'object':
                    return 'object';
                case 'boolean':
                case 'number':
                    return 'number';
                case 'null':
                    return String(val);
            }
            throw new SyntaxError('JSON.add');
        }
    }
})(jQuery);
