json-stringify.js revision 80c2303f32a65a670907dba765dfde42dc319087
/**
* Provides Y.JSON.stringify method for converting objects to JSON strings.
* @module json
* @submodule json-stringify
* @for JSON
* @static
*/
STRING = 'string',
NUMBER = 'number',
BOOLEAN = 'boolean',
OBJECT = 'object',
ARRAY = 'array',
REGEXP = 'regexp',
ERROR = 'error',
NULL = 'null',
DATE = 'date',
EMPTY = '',
OPEN_O = '{',
CLOSE_O = '}',
OPEN_A = '[',
CLOSE_A = ']',
COMMA = ',',
COMMA_CR = ",\n",
CR = "\n",
COLON = ':',
COLON_SP = ': ',
QUOTE = '"',
rep = isFunction(w) ? w : null,
pstack = [], // Processing stack used for cyclical ref protection
if (rep || typeof w !== 'object') {
w = undefined;
}
if (format) {
// String instances and primatives are left alone. String objects
// coerce for the indenting operations.
// force numeric indent values between {0,100} per the spec
// This also converts Number instances to primative
}
// turn off formatting for 0 or empty string
}
// escape encode special characters
function _char(c) {
if (!m[c]) {
}
return m[c];
}
// Enclose the escaped string in double quotes
function _string(s) {
}
// Check for cyclical references
function _cyclicalTest(o) {
if (pstack[i] === o) {
throw new Error("JSON.stringify. Cyclical reference");
}
}
return false;
}
function _indent(s) {
}
// Add the object to the processing stack
var a = [],
i, j, len, k, v;
if (arr) { // Array
a[i] = _stringify(o,i) || NULL;
}
} else { // Object
// If whitelist provided, take only those keys
if (typeof k[i] === STRING) {
v = _stringify(o,k[i]);
if (v) {
}
}
}
}
// remove the array from the stack
return arr ?
} else {
return arr ?
}
}
// Worker function. Fork behavior on data type and recurse objects.
function _stringify(h,key) {
t = type(o);
if (t === OBJECT) {
o = o.valueOf();
t = type(o);
}
}
switch (t) {
case REGEXP : // intentional fall through
case ERROR : // intentional fall through
default : return undefined;
}
}
// process the input
},
test;
// Test for fully functional native implementation
try {
test === "[\nxnull,\nx{\nxx\"b\":2\nx}\n]") {
}
}
catch (e) {} // defer to JS implementation
}
/**
* Regex used to capture characters that need escaping before enclosing
* their containing string in quotes.
* @property _SPECIAL_CHARS
* @type {RegExp}
* @private
*/
_SPECIAL_CHARS : /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
/**
* Character substitution map for common escapes and special characters.
* @property _CHARS
* @type {Object}
* @static
* @private
*/
_CHARS : {
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
},
/**
* Serializes a Date instance as a UTC date string. Used internally by
* stringify. Override this method if you need Dates serialized in a
* different format.
* @method dateToString
* @param d {Date} The Date to serialize
* @return {String} stringified Date in UTC format YYYY-MM-DDTHH:mm:SSZ
* @static
*/
dateToString : function (d) {
function _zeroPad(v) {
return v < 10 ? '0' + v : v;
}
return QUOTE + d.getUTCFullYear() + '-' +
_zeroPad(d.getUTCMonth() + 1) + '-' +
_zeroPad(d.getUTCDate()) + 'T' +
_zeroPad(d.getUTCHours()) + COLON +
_zeroPad(d.getUTCMinutes()) + COLON +
_zeroPad(d.getUTCSeconds()) + 'Z' + QUOTE;
},
/**
* Converts an arbitrary value to a JSON string representation.
* Cyclical object or array references are replaced with null.
* If a whitelist is provided, only matching object keys will be included.
* 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.
* @method stringify
* @param o {MIXED} any arbitrary object 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
* @static
* @public
*/
stringify : stringify
});
}, '@VERSION@' );