1
0
Fork 0

fixed JSON formatting

This commit is contained in:
Jan Steemann 2014-03-07 09:04:44 +01:00
parent 4b8dc85280
commit 2c1cd44558
1 changed files with 1 additions and 68 deletions

View File

@ -158,74 +158,7 @@
}, },
FormatJSON: function (oData, sIndent) { FormatJSON: function (oData, sIndent) {
var self = this; return JSON.stringify(oData, undefined, 2);
var sHTML, iCount;
if (sIndent === undefined) {
sIndent = "";
}
var sIndentStyle = " ";
var sDataType = self.RealTypeOf(oData);
if (sDataType === "array") {
if (oData.length === 0) {
return "[]";
}
sHTML = "[";
} else {
iCount = 0;
$.each(oData, function() {
iCount++;
return;
});
if (iCount === 0) { // object is empty
return "{}";
}
sHTML = "{";
}
iCount = 0;
$.each(oData, function(sKey, vValue) {
if (iCount > 0) {
sHTML += ",";
}
if (sDataType === "array") {
sHTML += ("\n" + sIndent + sIndentStyle);
} else {
sHTML += ("\n" + sIndent + sIndentStyle + JSON.stringify(sKey) + ": ");
}
// display relevant data type
switch (self.RealTypeOf(vValue)) {
case "array":
case "object":
sHTML += self.FormatJSON(vValue, (sIndent + sIndentStyle));
break;
case "boolean":
case "number":
sHTML += vValue.toString();
break;
case "null":
sHTML += "null";
break;
case "string":
sHTML += "\"" + vValue.replace(/\\/g, "\\\\").replace(/"/g, "\\\"") + "\"";
break;
default:
sHTML += ("TYPEOF: " + typeof vValue);
}
// loop
iCount++;
});
// close object
if (sDataType === "array") {
sHTML += ("\n" + sIndent + "]");
} else {
sHTML += ("\n" + sIndent + "}");
}
// return
return sHTML;
}, },
RealTypeOf: function (v) { RealTypeOf: function (v) {