[903] | 1 | JSONTools = {
|
---|
| 2 | "toStr" : function(pType)
|
---|
| 3 | {
|
---|
| 4 | switch ( pType.constructor )
|
---|
| 5 | {
|
---|
| 6 | case Object : return this.object(pType); break;
|
---|
| 7 | case Array : return this.array(pType); break;
|
---|
| 8 | case String : return this.string(pType); break;
|
---|
| 9 | case Number : return this.number(pType); break;
|
---|
| 10 | case Boolean : return this.boolean(pType); break;
|
---|
| 11 | case Date : return this.date(pType); break;
|
---|
| 12 | }
|
---|
| 13 | },
|
---|
| 14 | "object" : function(pType)
|
---|
| 15 | {
|
---|
| 16 | var a = ['{'],
|
---|
| 17 | b,
|
---|
| 18 | k,
|
---|
| 19 | v,
|
---|
| 20 | _this = this;
|
---|
| 21 | function p(s)
|
---|
| 22 | {
|
---|
| 23 | if ( b )
|
---|
| 24 | a.push(',');
|
---|
| 25 | a.push(_this.toStr(k), ':', s);
|
---|
| 26 | b = true;
|
---|
| 27 | }
|
---|
| 28 | for ( k in pType )
|
---|
| 29 | {
|
---|
| 30 | if ( pType.hasOwnProperty(k) )
|
---|
| 31 | {
|
---|
| 32 | v = pType[k];
|
---|
| 33 | switch ( typeof v )
|
---|
| 34 | {
|
---|
| 35 | case 'object':
|
---|
| 36 | if (v)
|
---|
| 37 | p(this.toStr(v));
|
---|
| 38 | else
|
---|
| 39 | p("null");
|
---|
| 40 | break;
|
---|
| 41 |
|
---|
| 42 | case 'string':
|
---|
| 43 | case 'number':
|
---|
| 44 | case 'boolean':
|
---|
| 45 | p(this.toStr(v));
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 | a.push('}');
|
---|
| 50 | return a.join('');
|
---|
| 51 | },
|
---|
| 52 | "array" : function(pType)
|
---|
| 53 | {
|
---|
| 54 | var a = ['['],
|
---|
| 55 | b,
|
---|
| 56 | i,
|
---|
| 57 | l = pType.length,
|
---|
| 58 | v;
|
---|
| 59 | function p(s)
|
---|
| 60 | {
|
---|
| 61 | if ( b )
|
---|
| 62 | a.push(',');
|
---|
| 63 | a.push(s);
|
---|
| 64 | b = true;
|
---|
| 65 | }
|
---|
| 66 | for ( i = 0; i < l; i += 1 )
|
---|
| 67 | {
|
---|
| 68 | v = pType[i];
|
---|
| 69 | switch ( typeof v )
|
---|
| 70 | {
|
---|
| 71 | case 'object':
|
---|
| 72 | if ( v )
|
---|
| 73 | {
|
---|
| 74 | p(this.toStr(v));
|
---|
| 75 | } else {
|
---|
| 76 | p("null");
|
---|
| 77 | }
|
---|
| 78 | break;
|
---|
| 79 | case 'string':
|
---|
| 80 | case 'number':
|
---|
| 81 | case 'boolean':
|
---|
| 82 | p(this.toStr(v));
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | a.push(']');
|
---|
| 86 | return a.join('');
|
---|
| 87 | },
|
---|
| 88 | "string" : function(pType)
|
---|
| 89 | {
|
---|
| 90 | var m = {
|
---|
| 91 | '\b': '\\b',
|
---|
| 92 | '\t': '\\t',
|
---|
| 93 | '\n': '\\n',
|
---|
| 94 | '\f': '\\f',
|
---|
| 95 | '\r': '\\r',
|
---|
| 96 | '"' : '\\"',
|
---|
| 97 | '\\': '\\\\'
|
---|
| 98 | };
|
---|
| 99 | if ( /[\\\x00-\x1f]/.test(pType) )
|
---|
| 100 | {
|
---|
| 101 | return '"' + pType.replace(/([\x00-\x1f\\])/g, function(a, b)
|
---|
| 102 | {
|
---|
| 103 | var c = m[b];
|
---|
| 104 | if (c) {
|
---|
| 105 | return c;
|
---|
| 106 | }
|
---|
| 107 | c = b.charCodeAt();
|
---|
| 108 | return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
|
---|
| 109 | }) + '"';
|
---|
| 110 | }
|
---|
| 111 | return '"' + pType + '"';
|
---|
| 112 | },
|
---|
| 113 | "boolean" : function(pType)
|
---|
| 114 | {
|
---|
| 115 | return String(pType);
|
---|
| 116 | },
|
---|
| 117 | "date" : function(pType)
|
---|
| 118 | {
|
---|
| 119 | function f(n)
|
---|
| 120 | {
|
---|
| 121 | return n < 10 ? '0' + n : n;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | return '"' + pType.getFullYear() + '-' +
|
---|
| 125 | f(pType.getMonth() + 1) + '-' +
|
---|
| 126 | f(pType.getDate()) + 'T' +
|
---|
| 127 | f(pType.getHours()) + ':' +
|
---|
| 128 | f(pType.getMinutes()) + ':' +
|
---|
| 129 | f(pType.getSeconds()) + '"';
|
---|
| 130 | },
|
---|
| 131 | "number" : function(pType)
|
---|
| 132 | {
|
---|
| 133 | return isFinite(pType) ? String(pType) : "null";
|
---|
| 134 | }
|
---|
| 135 | } |
---|