2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('json-stringify', function (Y, NAME) {
11 * Provides Y.JSON.stringify method for converting objects to JSON strings.
14 * @submodule json-stringify
19 _JSON = Y.config.global.JSON;
21 Y.mix(Y.namespace('JSON'), {
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
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
33 dateToString: function (d) {
34 function _zeroPad(v) {
35 return v < 10 ? '0' + v : v;
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';
47 * <p>Converts an arbitrary value to a JSON string representation.</p>
49 * <p>Objects with cyclical references will trigger an exception.</p>
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>
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>
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
73 stringify: function () {
74 return _JSON.stringify.apply(_JSON, arguments);
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
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>
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
95 * <p>Special characters \ " \b \t \n \f \r are already cached.</p>
97 * @property charCacheThreshold
102 charCacheThreshold: 100
106 }, '3.13.0', {"requires": ["yui-base"]});