|
5 | 5 | * This document is licensed as free software under the terms of the
|
6 | 6 | * MIT License: http://www.opensource.org/licenses/mit-license.php
|
7 | 7 | *
|
8 |
| - * Brantley Harris technically wrote this plugin, but it is based somewhat |
9 |
| - * on the JSON.org website's http://www.json.org/json2.js, which proclaims: |
| 8 | + * Brantley Harris wrote this plugin. It is based somewhat on the JSON.org |
| 9 | + * website's http://www.json.org/json2.js, which proclaims: |
10 | 10 | * "NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.", a sentiment that
|
11 |
| - * I uphold. I really just cleaned it up. |
| 11 | + * I uphold. |
12 | 12 | *
|
13 |
| - * It is also based heavily on MochiKit's serializeJSON, which is |
14 |
| - * copywrited 2005 by Bob Ippolito. |
| 13 | + * It is also influenced heavily by MochiKit's serializeJSON, which is |
| 14 | + * copyrighted 2005 by Bob Ippolito. |
15 | 15 | */
|
16 | 16 |
|
17 |
| -(function($) { |
18 |
| - function toIntegersAtLease(n) |
19 |
| - // Format integers to have at least two digits. |
20 |
| - { |
21 |
| - return n < 10 ? '0' + n : n; |
22 |
| - } |
| 17 | +(function($) { |
| 18 | + /** jQuery.toJSON( json-serializble ) |
| 19 | + Converts the given argument into a JSON respresentation. |
23 | 20 |
|
24 |
| - Date.prototype.toJSON = function(date) |
25 |
| - // Yes, it polutes the Date namespace, but we'll allow it here, as |
26 |
| - // it's damned usefull. |
27 |
| - { |
28 |
| - return this.getUTCFullYear() + '-' + |
29 |
| - toIntegersAtLease(this.getUTCMonth()) + '-' + |
30 |
| - toIntegersAtLease(this.getUTCDate()); |
31 |
| - }; |
| 21 | + If an object has a "toJSON" function, that will be used to get the representation. |
| 22 | + Non-integer/string keys are skipped in the object, as are keys that point to a function. |
32 | 23 |
|
33 |
| - var escapeable = /["\\\x00-\x1f\x7f-\x9f]/g; |
34 |
| - var meta = { // table of character substitutions |
35 |
| - '\b': '\\b', |
36 |
| - '\t': '\\t', |
37 |
| - '\n': '\\n', |
38 |
| - '\f': '\\f', |
39 |
| - '\r': '\\r', |
40 |
| - '"' : '\\"', |
41 |
| - '\\': '\\\\' |
42 |
| - }; |
43 |
| - |
44 |
| - $.quoteString = function(string) |
45 |
| - // Places quotes around a string, inteligently. |
46 |
| - // If the string contains no control characters, no quote characters, and no |
47 |
| - // backslash characters, then we can safely slap some quotes around it. |
48 |
| - // Otherwise we must also replace the offending characters with safe escape |
49 |
| - // sequences. |
| 24 | + json-serializble: |
| 25 | + The *thing* to be converted. |
| 26 | + **/ |
| 27 | + $.toJSON = function(o) |
50 | 28 | {
|
51 |
| - if (escapeable.test(string)) |
52 |
| - { |
53 |
| - return '"' + string.replace(escapeable, function (a) |
54 |
| - { |
55 |
| - var c = meta[a]; |
56 |
| - if (typeof c === 'string') { |
57 |
| - return c; |
58 |
| - } |
59 |
| - c = a.charCodeAt(); |
60 |
| - return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16); |
61 |
| - }) + '"'; |
62 |
| - } |
63 |
| - return '"' + string + '"'; |
64 |
| - }; |
65 |
| - |
66 |
| - $.toJSON = function(o, compact) |
67 |
| - { |
68 |
| - var type = typeof(o); |
| 29 | + if (JSON && JSON.stringify) |
| 30 | + return JSON.stringify(o); |
69 | 31 |
|
70 |
| - if (type == "undefined") |
71 |
| - return "undefined"; |
72 |
| - else if (type == "number" || type == "boolean") |
73 |
| - return o + ""; |
74 |
| - else if (o === null) |
| 32 | + var type = typeof(o); |
| 33 | + |
| 34 | + if (o === null) |
75 | 35 | return "null";
|
| 36 | + |
| 37 | + if (type == "undefined") |
| 38 | + return undefined; |
76 | 39 |
|
77 |
| - // Is it a string? |
78 |
| - if (type == "string") |
79 |
| - { |
| 40 | + if (type == "number" || type == "boolean") |
| 41 | + return o + ""; |
| 42 | + |
| 43 | + if (type == "string") |
80 | 44 | return $.quoteString(o);
|
81 |
| - } |
82 |
| - |
83 |
| - // Does it have a .toJSON function? |
84 |
| - if (type == "object" && typeof o.toJSON == "function") |
85 |
| - return o.toJSON(compact); |
86 |
| - |
87 |
| - // Is it an array? |
88 |
| - if (type != "function" && typeof(o.length) == "number") |
| 45 | + |
| 46 | + if (type == 'object') |
89 | 47 | {
|
90 |
| - var ret = []; |
91 |
| - for (var i = 0; i < o.length; i++) { |
92 |
| - ret.push( $.toJSON(o[i], compact) ); |
| 48 | + if (typeof o.toJSON == "function") |
| 49 | + return $.toJSON( o.toJSON() ); |
| 50 | + |
| 51 | + if (o.constructor === Date) |
| 52 | + { |
| 53 | + var month = o.getUTCMonth() + 1; |
| 54 | + if (month < 10) month = '0' + month; |
| 55 | + |
| 56 | + var day = o.getUTCDate(); |
| 57 | + if (day < 10) day = '0' + day; |
| 58 | + |
| 59 | + var year = o.getUTCFullYear(); |
| 60 | + |
| 61 | + var hours = o.getUTCHours(); |
| 62 | + if (hours < 10) hours = '0' + hours; |
| 63 | + |
| 64 | + var minutes = o.getUTCMinutes(); |
| 65 | + if (minutes < 10) minutes = '0' + minutes; |
| 66 | + |
| 67 | + var seconds = o.getUTCSeconds(); |
| 68 | + if (seconds < 10) seconds = '0' + seconds; |
| 69 | + |
| 70 | + var milli = o.getUTCMilliseconds(); |
| 71 | + if (milli < 100) milli = '0' + milli; |
| 72 | + if (milli < 10) milli = '0' + milli; |
| 73 | + |
| 74 | + return '"' + year + '-' + month + '-' + day + 'T' + |
| 75 | + hours + ':' + minutes + ':' + seconds + |
| 76 | + '.' + milli + 'Z"'; |
93 | 77 | }
|
94 |
| - if (compact) |
| 78 | + |
| 79 | + if (o.constructor === Array) |
| 80 | + { |
| 81 | + var ret = []; |
| 82 | + for (var i = 0; i < o.length; i++) |
| 83 | + ret.push( $.toJSON(o[i]) ); |
| 84 | + |
95 | 85 | return "[" + ret.join(",") + "]";
|
96 |
| - else |
97 |
| - return "[" + ret.join(", ") + "]"; |
98 |
| - } |
99 |
| - |
100 |
| - // If it's a function, we have to warn somebody! |
101 |
| - if (type == "function") { |
102 |
| - throw new TypeError("Unable to convert object of type 'function' to json."); |
103 |
| - } |
| 86 | + } |
104 | 87 |
|
105 |
| - // It's probably an object, then. |
106 |
| - var ret = []; |
107 |
| - for (var k in o) { |
108 |
| - var name; |
109 |
| - type = typeof(k); |
| 88 | + var pairs = []; |
| 89 | + for (var k in o) { |
| 90 | + var name; |
| 91 | + var type = typeof k; |
| 92 | + |
| 93 | + if (type == "number") |
| 94 | + name = '"' + k + '"'; |
| 95 | + else if (type == "string") |
| 96 | + name = $.quoteString(k); |
| 97 | + else |
| 98 | + continue; //skip non-string or number keys |
110 | 99 |
|
111 |
| - if (type == "number") |
112 |
| - name = '"' + k + '"'; |
113 |
| - else if (type == "string") |
114 |
| - name = $.quoteString(k); |
115 |
| - else |
116 |
| - continue; //skip non-string or number keys |
| 100 | + if (typeof o[k] == "function") |
| 101 | + continue; //skip pairs where the value is a function. |
117 | 102 |
|
118 |
| - var val = $.toJSON(o[k], compact); |
119 |
| - if (typeof(val) != "string") { |
120 |
| - // skip non-serializable values |
121 |
| - continue; |
122 |
| - } |
| 103 | + var val = $.toJSON(o[k]); |
123 | 104 |
|
124 |
| - if (compact) |
125 |
| - ret.push(name + ":" + val); |
126 |
| - else |
127 |
| - ret.push(name + ": " + val); |
| 105 | + pairs.push(name + ":" + val); |
| 106 | + } |
| 107 | + |
| 108 | + return "{" + pairs.join(", ") + "}"; |
128 | 109 | }
|
129 |
| - return "{" + ret.join(", ") + "}"; |
130 |
| - }; |
131 |
| - |
132 |
| - $.compactJSON = function(o) |
133 |
| - { |
134 |
| - return $.toJSON(o, true); |
135 |
| - }; |
136 |
| - |
| 110 | + } |
| 111 | + |
| 112 | + /** jQuery.evalJSON(src) |
| 113 | + Evaluates a given piece of json source. |
| 114 | + **/ |
137 | 115 | $.evalJSON = function(src)
|
138 |
| - // Evals JSON that we know to be safe. |
139 | 116 | {
|
140 | 117 | return eval("(" + src + ")");
|
141 |
| - }; |
| 118 | + } |
142 | 119 |
|
| 120 | + /** jQuery.secureEvalJSON(src) |
| 121 | + Evals JSON in a way that is *more* secure. |
| 122 | + **/ |
143 | 123 | $.secureEvalJSON = function(src)
|
144 |
| - // Evals JSON in a way that is *more* secure. |
145 | 124 | {
|
146 | 125 | var filtered = src;
|
147 | 126 | filtered = filtered.replace(/\\["\\\/bfnrtu]/g, '@');
|
|
153 | 132 | else
|
154 | 133 | throw new SyntaxError("Error parsing JSON, source is not valid.");
|
155 | 134 | };
|
| 135 | + |
| 136 | + /** jQuery.quoteString(string) |
| 137 | + Returns a string-repr of a string, escaping quotes intelligently. |
| 138 | + Mostly a support function for toJSON. |
| 139 | + |
| 140 | + Examples: |
| 141 | + >>> jQuery.quoteString("apple") |
| 142 | + "apple" |
| 143 | + |
| 144 | + >>> jQuery.quoteString('"Where are we going?", she asked.') |
| 145 | + "\"Where are we going?\", she asked." |
| 146 | + **/ |
| 147 | + $.quoteString = function(string) |
| 148 | + { |
| 149 | + if (_escapeable.test(string)) |
| 150 | + { |
| 151 | + return '"' + string.replace(_escapeable, function (a) |
| 152 | + { |
| 153 | + var c = _meta[a]; |
| 154 | + if (typeof c === 'string') return c; |
| 155 | + c = a.charCodeAt(); |
| 156 | + return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16); |
| 157 | + }) + '"' |
| 158 | + } |
| 159 | + return '"' + string + '"'; |
| 160 | + } |
| 161 | + |
| 162 | + var _escapeable = /["\\\x00-\x1f\x7f-\x9f]/g; |
| 163 | + |
| 164 | + var _meta = { |
| 165 | + '\b': '\\b', |
| 166 | + '\t': '\\t', |
| 167 | + '\n': '\\n', |
| 168 | + '\f': '\\f', |
| 169 | + '\r': '\\r', |
| 170 | + '"' : '\\"', |
| 171 | + '\\': '\\\\' |
| 172 | + } |
156 | 173 | })(jQuery);
|
0 commit comments