|
1 YUI.add('json-stringify', function (Y, NAME) { |
|
2 |
|
3 /** |
|
4 * Provides Y.JSON.stringify method for converting objects to JSON strings. |
|
5 * |
|
6 * @module json |
|
7 * @submodule json-stringify |
|
8 * @for JSON |
|
9 * @static |
|
10 */ |
|
11 var COLON = ':', |
|
12 _JSON = Y.config.global.JSON; |
|
13 |
|
14 Y.mix(Y.namespace('JSON'), { |
|
15 /** |
|
16 * Serializes a Date instance as a UTC date string. Used internally by |
|
17 * stringify. Override this method if you need Dates serialized in a |
|
18 * different format. |
|
19 * |
|
20 * @method dateToString |
|
21 * @param d {Date} The Date to serialize |
|
22 * @return {String} stringified Date in UTC format YYYY-MM-DDTHH:mm:SSZ |
|
23 * @deprecated Use a replacer function |
|
24 * @static |
|
25 */ |
|
26 dateToString: function (d) { |
|
27 function _zeroPad(v) { |
|
28 return v < 10 ? '0' + v : v; |
|
29 } |
|
30 |
|
31 return d.getUTCFullYear() + '-' + |
|
32 _zeroPad(d.getUTCMonth() + 1) + '-' + |
|
33 _zeroPad(d.getUTCDate()) + 'T' + |
|
34 _zeroPad(d.getUTCHours()) + COLON + |
|
35 _zeroPad(d.getUTCMinutes()) + COLON + |
|
36 _zeroPad(d.getUTCSeconds()) + 'Z'; |
|
37 }, |
|
38 |
|
39 /** |
|
40 * <p>Converts an arbitrary value to a JSON string representation.</p> |
|
41 * |
|
42 * <p>Objects with cyclical references will trigger an exception.</p> |
|
43 * |
|
44 * <p>If a whitelist is provided, only matching object keys will be |
|
45 * included. Alternately, a replacer function may be passed as the |
|
46 * second parameter. This function is executed on every value in the |
|
47 * input, and its return value will be used in place of the original value. |
|
48 * This is useful to serialize specialized objects or class instances.</p> |
|
49 * |
|
50 * <p>If a positive integer or non-empty string is passed as the third |
|
51 * parameter, the output will be formatted with carriage returns and |
|
52 * indentation for readability. If a String is passed (such as "\t") it |
|
53 * will be used once for each indentation level. If a number is passed, |
|
54 * that number of spaces will be used.</p> |
|
55 * |
|
56 * @method stringify |
|
57 * @param o {MIXED} any arbitrary value to convert to JSON string |
|
58 * @param w {Array|Function} (optional) whitelist of acceptable object |
|
59 * keys to include, or a replacer function to modify the |
|
60 * raw value before serialization |
|
61 * @param ind {Number|String} (optional) indentation character or depth of |
|
62 * spaces to format the output. |
|
63 * @return {string} JSON string representation of the input |
|
64 * @static |
|
65 */ |
|
66 stringify: function () { |
|
67 return _JSON.stringify.apply(_JSON, arguments); |
|
68 }, |
|
69 |
|
70 /** |
|
71 * <p>Number of occurrences of a special character within a single call to |
|
72 * stringify that should trigger promotion of that character to a dedicated |
|
73 * preprocess step for future calls. This is only used in environments |
|
74 * that don't support native JSON, or when useNativeJSONStringify is set to |
|
75 * false.</p> |
|
76 * |
|
77 * <p>So, if set to 50 and an object is passed to stringify that includes |
|
78 * strings containing the special character \x07 more than 50 times, |
|
79 * subsequent calls to stringify will process object strings through a |
|
80 * faster serialization path for \x07 before using the generic, slower, |
|
81 * replacement process for all special characters.</p> |
|
82 * |
|
83 * <p>To prime the preprocessor cache, set this value to 1, then call |
|
84 * <code>Y.JSON.stringify("<em>(all special characters to |
|
85 * cache)</em>");</code>, then return this setting to a more conservative |
|
86 * value.</p> |
|
87 * |
|
88 * <p>Special characters \ " \b \t \n \f \r are already cached.</p> |
|
89 * |
|
90 * @property charCacheThreshold |
|
91 * @static |
|
92 * @default 100 |
|
93 * @type {Number} |
|
94 */ |
|
95 charCacheThreshold: 100 |
|
96 }); |
|
97 |
|
98 |
|
99 }, '@VERSION@', {"requires": ["yui-base"]}); |