/    Sign up×
Community /Pin to ProfileBookmark

perl style data dumper for javascript objects

Hey there – this is my first post here, this seems like the place to post javascripts.

I’m not sure whether I’ve gone out on a limb to reinvent the wheel, but I’ve produced a data dumper, very similar to the one in Perl ([URL=http://search.cpan.org/author/ILYAM/Data-Dumper-2.121/Dumper.pm]here[/URL]).

The main function that the user should be interested in is a prototype of Object called dump. It can be fed an optional associative array of options (examples below). The method recursively looks through the object and returns the content of the object (excluding functions)

I’ve attached the script (as a txt file), but I’ve uploaded it to my site as well, so you can use the example below – replace testArray with any object/string/array/number you like

[code=php]
<SCRIPT LANGUAGE=”JavaScript” src=”http://www.petercracknell.com/include/js/dump.js”></SCRIPT>
<SCRIPT LANGUAGE=”JavaScript” src=”dump.js”></SCRIPT>
<SCRIPT LANGUAGE=”JavaScript” TYPE=”text/javascript”>
function myFancyObject(asd, sdf, sdf) {
this.xyzxyz = {
longword: 4,
c: [3,5,6],
d: { w: [3,5] }
};
this.abc = [1, 2, 3]
}

var testArray = new Array(new myFancyObject(), 4, [4,5]);

document.write(‘<pre>’);
document.writeln(testArray.dump());
document.writeln(testArray.dump({space: ‘_’}));
document.writeln(testArray.dump({object_detail: false}));
document.writeln(testArray.dump({wide_spacing: false}));
document.writeln(testArray.dump({quote_string: ““”}));
document.write(‘</pre>’);
</SCRIPT>
[/code]

The script also contains Object.duplicate, Object.isArray, Object.isNumber, Object.isString, which I think are all relatively self-explanatory.

I’m interested in peoples feedback – I consider these Object functions finished, can anyone see any useful additions? Can anyone find any bugs, problems?

Finally has this been done before/bettered? I did search google, but couldn’t find one.

Pete

[upl-file uuid=fb1ba81a-fd2d-4869-937c-fe26463ab8a9 size=7kB]dump.txt[/upl-file]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Jeff_MottSep 18.2003 — Finally has this been done before/bettered? I did search google, but couldn't find one.[/quote]There's actually one built-in to JavaScript. The [font=courier]toSource()[/font] method http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/ix.html#T. Though I don't think Microsoft or Opera has implemented it yet.
Copy linkTweet thisAlerts:
@p3tecauthorSep 19.2003 — Cool, so eventually there should be some proper data dumping, but I take it that'll be a while before that gets to be the norm.

My code is only really for developers - I can't believe no one else has written anything similar!?! I use the equivalent in perl on a daily basis at work and couldn't imagine developing without it (obviously there are differences between the language and its usage).

Now (I at least) have a tool to see what's really going on with my objects/data - in my current project (3d modeller, still in alpha), I have a frame that is constantly pumped the data dumpers output, so I know what's going on.

Any comments/suggestions?

Pete
×

Success!

Help @p3tec spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...