Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function DumpObject(obj)
- {
- var od = new Object;
- var result = "";
- var len = 0;
- for (var property in obj)
- {
- var value = obj[property];
- if (typeof value == 'string')
- value = "'" + value + "'";
- else if (typeof value == 'object')
- {
- if (value instanceof Array)
- {
- value = "[ " + value + " ]";
- }
- else
- {
- var ood = DumpObject(value);
- value = "{ " + ood.dump + " }";
- }
- }
- result += "'" + property + "' : " + value + ", ";
- len++;
- }
- od.dump = result.replace(/, $/, "");
- od.len = len;
- return od;
- }
- function DumpObjectIndented(obj, indent)
- {
- var result = "";
- if (indent == null) indent = "";
- for (var property in obj)
- {
- var value = obj[property];
- if (typeof value == 'string')
- value = "'" + value + "'";
- else if (typeof value == 'object')
- {
- if (value instanceof Array)
- {
- // Just let JS convert the Array to a string!
- value = "[ " + value + " ]";
- }
- else
- {
- // Recursive dump
- // (replace " " by "t" or something else if you prefer)
- var od = DumpObjectIndented(value, indent + " ");
- // If you like { on the same line as the key
- //value = "{n" + od + "n" + indent + "}";
- // If you prefer { and } to be aligned
- value = "n" + indent + "{n" + od + "n" + indent + "}";
- }
- }
- result += indent + "'" + property + "' : " + value + ",n";
- }
- return result.replace(/,n$/, "");
- }
- var myArray = ['e', {pluribus: 'unum'}];
- var text = JSON.stringify(myArray, null, 't'); //you can specify a number instead of 't' and that many spaces will be used for indentation...
- [
- "e",
- {
- "pluribus": "unum"
- }
- ]
- jsDump.parse([
- window,
- document,
- { a : 5, '1' : 'foo' },
- /^[ab]+$/g,
- new RegExp('x(.*?)z','ig'),
- alert,
- function fn( x, y, z ){
- return x + y;
- },
- true,
- undefined,
- null,
- new Date(),
- document.body,
- document.getElementById('links')
- ])
- [
- [Window],
- [Document],
- {
- "1": "foo",
- "a": 5
- },
- /^[ab]+$/g,
- /x(.*?)z/gi,
- function alert( a ){
- [code]
- },
- function fn( a, b, c ){
- [code]
- },
- true,
- undefined,
- null,
- "Fri Feb 19 2010 00:49:45 GMT+0300 (MSK)",
- <body id="body" class="node"></body>,
- <div id="links">
- ]
- JSON.stringify({f:function(){}}) // "{}"
- JSON.stringify(document.body) // TypeError: Converting circular structure to JSON
- // Usage: dump(object)
- function dump(object, pad){
- var indent = 't'
- if (!pad) pad = ''
- var out = ''
- if (object.constructor == Array){
- out += '[n'
- for (var i=0; i<object.length; i++){
- out += pad + indent + dump(object[i], pad + indent) + 'n'
- }
- out += pad + ']'
- }else if (object.constructor == Object){
- out += '{n'
- for (var i in object){
- out += pad + indent + i + ': ' + dump(object[i], pad + indent) + 'n'
- }
- out += pad + '}'
- }else{
- out += object
- }
- return out
- }
- var temp = Array.prototype.toJSON;
- delete Array.prototype.toJSON;
- $('result').value += JSON.stringify(profile_base, null, 2);
- Array.prototype.toJSON = temp;
- function dumpObject(obj, indent)
- {
- var CR = "<br />", SPC = " ", result = "";
- if (indent == null) indent = "";
- for (var property in obj)
- {
- var value = obj[property];
- if (typeof value == 'string')
- {
- value = "'" + value + "'";
- }
- else if (typeof value == 'object')
- {
- if (value instanceof Array)
- {
- // Just let JS convert the Array to a string!
- value = "[ " + value + " ]";
- }
- else
- {
- var od = dumpObject(value, indent + SPC);
- value = CR + indent + "{" + CR + od + CR + indent + "}";
- }
- }
- result += indent + "'" + property + "' : " + value + "," + CR;
- }
- return result;
- }
- function pp(object, depth, embedded) {
- typeof(depth) == "number" || (depth = 0)
- typeof(embedded) == "boolean" || (embedded = false)
- var newline = false
- var spacer = function(depth) { var spaces = ""; for (var i=0;i<depth;i++) { spaces += " "}; return spaces }
- var pretty = ""
- if ( typeof(object) == "undefined" ) { pretty += "undefined" }
- else if ( typeof(object) == "boolean" ||
- typeof(object) == "number" ) { pretty += object.toString() }
- else if ( typeof(object) == "string" ) { pretty += """ + object + """ }
- else if ( object == null) { pretty += "null" }
- else if ( object instanceof(Array) ) {
- if ( object.length > 0 ) {
- if (embedded) { newline = true }
- var content = ""
- for each (var item in object) { content += pp(item, depth+1) + ",n" + spacer(depth+1) }
- content = content.replace(/,ns*$/, "").replace(/^s*/,"")
- pretty += "[ " + content + "n" + spacer(depth) + "]"
- } else { pretty += "[]" }
- }
- else if (typeof(object) == "object") {
- if ( Object.keys(object).length > 0 ){
- if (embedded) { newline = true }
- var content = ""
- for (var key in object) {
- content += spacer(depth + 1) + key.toString() + ": " + pp(object[key], depth+2, true) + ",n"
- }
- content = content.replace(/,ns*$/, "").replace(/^s*/,"")
- pretty += "{ " + content + "n" + spacer(depth) + "}"
- } else { pretty += "{}"}
- }
- else { pretty += object.toString() }
- return ((newline ? "n" + spacer(depth) : "") + pretty)
- }
- js> pp({foo:"bar", baz: 1})
- { foo: "bar",
- baz: 1
- }
- js> var taco
- js> pp({foo:"bar", baz: [1,"taco",{"blarg": "moo", "mine": "craft"}, null, taco, {}], bleep: {a:null, b:taco, c: []}})
- { foo: "bar",
- baz:
- [ 1,
- "taco",
- { blarg: "moo",
- mine: "craft"
- },
- null,
- undefined,
- {}
- ],
- bleep:
- { a: null,
- b: undefined,
- c: []
- }
- }
Add Comment
Please, Sign In to add comment