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