Created
April 28, 2009 06:26
-
-
Save alandipert/102985 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_json_encode: function(inVal, out) { | |
out = out || new Array(); | |
var undef; // undefined | |
switch (typeof inVal) { | |
case 'object': | |
if (!inVal) { | |
out.push('null'); | |
} else { | |
if (inVal.constructor == Array) { | |
var testVal = inVal.length; | |
var compVal = 0; | |
for (var key in inVal) compVal++; | |
if (testVal != compVal) { | |
out.push('{'); | |
i = 0; | |
for (var key in inVal) { | |
if (i++ > 0) out.push(',\n'); | |
out.push('"'); | |
out.push(key); | |
out.push('":'); | |
jQuery.golf._json_encode(inVal[key], out); | |
} | |
out.push('}'); | |
} else { | |
out.push('['); | |
for (var i = 0; i < inVal.length; ++i) { | |
if (i > 0) out.push(',\n'); | |
jQuery.golf._json_encode(inVal[i], out); | |
} | |
out.push(']'); | |
} | |
} else if (typeof inVal.toString != 'undefined') { | |
out.push('{'); | |
var first = true; | |
for (var i in inVal) { | |
var curr = out.length; | |
if (!first) out.push(',\n'); | |
jQuery.golf._json_encode(i, out); | |
out.push(':'); | |
jQuery.golf._json_encode(inVal[i], out); | |
if (out[out.length - 1] == undef) | |
{ | |
out.splice(curr, out.length - curr); | |
} else { | |
first = false; | |
} | |
} | |
out.push('}'); | |
} | |
} | |
return out; | |
case 'unknown': | |
case 'undefined': | |
case 'function': | |
out.push(undef); | |
return out; | |
case 'string': | |
out.push('"'); | |
out.push( | |
inVal.replace(/(["\\])/g, '\$1') | |
.replace(/\r/g, '') | |
.replace(/\n/g, '\n') | |
); | |
out.push('"'); | |
return out; | |
default: | |
out.push(String(inVal)); | |
return out; | |
} | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment