2
Copyright (c) 2008, Yahoo! Inc. All rights reserved.
3
Code licensed under the BSD License:
4
http://developer.yahoo.net/yui/license.txt
7
YUI.add('json-stringify', function(Y) {
10
* Provides Y.JSON.stringify method for converting objects to JSON strings.
12
* @submodule json-stringify
16
var isA = Y.Lang.isArray;
18
Y.JSON = Y.JSON || {};
22
* Regex used to capture characters that need escaping before enclosing
23
* their containing string in quotes.
24
* @property _SPECIAL_CHARS
28
_SPECIAL_CHARS : /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
31
* Character substitution map for common escapes and special characters.
48
* Serializes a Date instance as a UTC date string. Used internally by
49
* stringify. Override this method if you need Dates serialized in a
51
* @method dateToString
52
* @param d {Date} The Date to serialize
53
* @return {String} stringified Date in UTC format YYYY-MM-DDTHH:mm:SSZ
56
dateToString : function (d) {
57
function _zeroPad(v) {
58
return v < 10 ? '0' + v : v;
61
return '"' + d.getUTCFullYear() + '-' +
62
_zeroPad(d.getUTCMonth() + 1) + '-' +
63
_zeroPad(d.getUTCDate()) + 'T' +
64
_zeroPad(d.getUTCHours()) + ':' +
65
_zeroPad(d.getUTCMinutes()) + ':' +
66
_zeroPad(d.getUTCSeconds()) + 'Z"';
70
* Converts an arbitrary value to a JSON string representation.
71
* Cyclical object or array references are replaced with null.
72
* If a whitelist is provided, only matching object keys will be included.
73
* If a depth limit is provided, objects and arrays at that depth will
74
* be stringified as empty.
76
* @param o {MIXED} any arbitrary object to convert to JSON string
77
* @param w {Array|Function} (optional) whitelist of acceptable object
78
* keys to include, or a replacer function to modify the
79
* raw value before serialization
80
* @param d {number} (optional) depth limit to recurse objects/arrays
81
* (practical minimum 1)
82
* @return {string} JSON string representation of the input
86
stringify : function (o,w,d) {
88
var m = Y.JSON._CHARS,
89
str_re = Y.JSON._SPECIAL_CHARS,
90
rep = typeof w === 'function' ? w : null,
91
pstack = []; // Processing stack used for cyclical ref protection
93
if (rep || typeof w !== 'object') {
97
// escape encode special characters
98
var _char = function (c) {
100
m[c]='\\u'+('0000'+(+(c.charCodeAt(0))).toString(16)).slice(-4);
105
// Enclose the escaped string in double quotes
106
var _string = function (s) {
107
return '"' + s.replace(str_re, _char) + '"';
110
// Use the configured date conversion
111
var _date = Y.JSON.dateToString;
113
// Worker function. Fork behavior on data type and recurse objects and
114
// arrays per the configured depth.
115
var _stringify = function (h,key,d) {
116
var o = typeof rep === 'function' ? rep.call(h,key,h[key]) : h[key],
118
i,len,j, // array iteration
119
k,v, // object iteration
120
a; // composition array for performance over string concat
123
if (t === 'string') {
127
// native boolean and Boolean instance
128
if (t === 'boolean' || o instanceof Boolean) {
132
// native number and Number instance
133
if (t === 'number' || o instanceof Number) {
134
return isFinite(o) ? String(o) : 'null';
138
if (o instanceof Date) {
143
if (t === 'object') {
148
// Check for cyclical references
149
for (i = pstack.length - 1; i >= 0; --i) {
150
if (pstack[i] === o) {
155
// Add the object to the processing stack
156
pstack[pstack.length] = o;
160
// Only recurse if we're above depth config
164
for (i = o.length - 1; i >= 0; --i) {
165
a[i] = _stringify(o,i,d-1) || 'null';
170
// If whitelist provided, take only those keys
171
k = isA(w) ? w : Y.Object.keys(w||o);
173
for (i = 0, j = 0, len = k.length; i < len; ++i) {
174
if (typeof k[i] === 'string') {
175
v = _stringify(o,k[i],d-1);
177
a[j++] = _string(k[i]) + ':' + v;
184
// remove the array from the stack
187
return isA(o) ? '['+a.join(',')+']' : '{'+a.join(',')+'}';
190
return undefined; // invalid input
193
// Default depth to POSITIVE_INFINITY
194
d = d >= 0 ? d : 1/0;
197
return _stringify({'':o},'',d);