json-stringify-debug.js revision 10d8bafc5c24f3a4285cf6060a1935ba5cfc4b85
1585N/A * Provides Y.JSON.stringify method for converting objects to JSON strings. 1585N/A * @submodule json-stringify // Regex used to capture characters that need escaping before enclosing // their containing string in quotes. // Character substitution map for common escapes and special characters. _COMMON_LENGTH = _COMMON.length, // In-process optimization for special character escapes that haven't
yet // been promoted to _COMMON // Per-char counter to determine if it's worth fast tracking a special // character escape sequence. // Utility function used to determine how to serialize a variable. return _allowable[t] ||
// number, string, boolean, undefined (o ?
OBJECT :
NULL) :
// object, array, null, misc natives// Escapes a special character to a safe Unicode representation // === to avoid this conditional for the remainder of the current operation // Enclose escaped strings in quotes // Preprocess the string against common characters to avoid function // overhead associated with replacement via function. // original function replace for the not-as-common set of chars // Adds the provided space to the beginning of every line in the input string // JavaScript implementation of stringify (see API declaration of stringify) // Ensure whitelist keys are unique (bug 2110391) // Per the spec, strings are truncated to 10 characters and numbers // are converted to that number of spaces (max 10) // Per the ECMA 5 spec, toJSON is applied before the replacer is // called. Also per the spec, Date.prototype.toJSON has been added, so // Date instances should be serialized prior to exposure to the // replacer. I disagree with this decision, but the spec is the spec. case DATE :
// intentional fallthrough. Pre-replacer Dates are // serialized in the toJSON stage. Dates here would // have been produced by the replacer. // Check for cyclical references in nested objects throw new Error(
"JSON.stringify. Cyclical reference");
// Add the object to the processing stack // If whitelist provided, take only those keys // remove the array from the stack // Double check basic native functionality. This is primarily to catch broken // early JSON API implementations in Firefox 3.1 beta1 and beta2. * Leverage native JSON stringify if the browser has a native * implementation. In general, this is a good idea. See the Known Issues * section in the JSON user guide for caveats. The default value is true * for browsers with native JSON support. * @property useNativeStringify * Serializes a Date instance as a UTC date string. Used internally by * stringify. Override this method if you need Dates serialized in a * @param d {Date} The Date to serialize * @return {String} stringified Date in UTC format YYYY-MM-DDTHH:mm:SSZ * @deprecated Use a replacer function return v <
10 ?
'0' + v : v;
* <p>Converts an arbitrary value to a JSON string representation.</p> * <p>Objects with cyclical references will trigger an exception.</p> * <p>If a whitelist is provided, only matching object keys will be * included. Alternately, a replacer function may be passed as the * second parameter. This function is executed on every value in the * input, and its return value will be used in place of the original value. * This is useful to serialize specialized objects or class instances.</p> * <p>If a positive integer or non-empty string is passed as the third * parameter, the output will be formatted with carriage returns and * indentation for readability. If a String is passed (such as "\t") it * will be used once for each indentation level. If a number is passed, * that number of spaces will be used.</p> * @param o {MIXED} any arbitrary value to convert to JSON string * @param w {Array|Function} (optional) whitelist of acceptable object * keys to include, or a replacer function to modify the * raw value before serialization * @param ind {Number|String} (optional) indentation character or depth of * spaces to format the output. * @return {string} JSON string representation of the input * <p>Number of occurrences of a special character within a single call to * stringify that should trigger promotion of that character to a dedicated * preprocess step for future calls. This is only used in environments * that don't support native JSON, or when useNativeStringify is set to * <p>So, if set to 50 and an object is passed to stringify that includes * strings containing the special character \x07 more than 50 times, * subsequent calls to stringify will process object strings through a * faster serialization path for \x07 before using the generic, slower, * replacement process for all special characters.</p> * <p>To prime the preprocessor cache, set this value to 1, then call * <code>Y.JSON.stringify("<em>(all special characters to * cache)</em>");</code>, then return this setting to a more conservative * <p>Special characters \ " \b \t \n \f \r are already cached.</p> * @property charCacheThreshold