function _serialize_object_key(key) {
return /^[a-zA-Z_\$][a-zA-Z_\$0-9]*$/.test(key) ? key : JSON.stringify(key);
}
function _serialize_object(obj, opts, current_indent) {
var arr = [], inner_indent = current_indent + opts.indent_inc;
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
arr.push(inner_indent + _serialize_object_key(key) + ': ' + _json_like_stringify(obj[key], opts, inner_indent));
}
}
return '{\n' + arr.join(',\n') + '\n' + current_indent + '}';
}
function _serialize_array(arr, opts, current_indent) {
var inner_indent = current_indent + opts.indent_inc;
return '[\n' + inner_indent + arr.map(inner_map).join(',\n' + inner_indent) + '\n' + current_indent + ']';
function inner_map(x) {
return _json_like_stringify(x, opts, inner_indent);
}
}
function _json_like_stringify(x, opts, current_indent) {
switch (typeof x) {
case "number":
case "string":
case "boolean":
default:
return JSON.stringify(x);
case "object":
if (x === null) return 'null';
if (Array.isArray(x)) return _serialize_array(x, opts, current_indent);
return _serialize_object(x, opts, current_indent);
}
}
function json_like_stringify(x, indent) {
if (indent === undefined) indent = 2;
var indent_inc = '';
for (var i = 0; i < indent; i++) indent_inc += ' ';
return _json_like_stringify(x, {indent_inc:indent_inc}, '');
}
$('pre').text(json_like_stringify({a:[1,2,3],b:{c:{d:[6,7,"asdasda\"fgdfgdfg",true,null, [1, 2, 3, {' ':' '}, 5, 6, 7, 8, 9, 10, 11]]}}}));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre></pre>