Hi Andreas,

Give this a shot I think it will do what you need. Its a little messy since I just typed it really quick. Hope it helps:

//---Some contrived complex structure to trace
var myOb = new Object();
myOb.name = "jobe"
myOb.animals = ["free", "gulliver"];
myOb.someArr = [{email:"[EMAIL PROTECTED]", vocation:"programmer", male:true, hasKids:false}, 30];
//------
checkIt();
function checkIt() {
var output:String = inspect(myOb, "");
trace(output);
}
function inspect(ob, str, parent) {
if (ob instanceof Array) {
 str += "[";
} else if (ob instanceof Object) {
 str += "{";
}
var firstone:Boolean = true;
for (var i in ob) {
 var type = typeof ob[i];
 if (!firstone) {
  str += ", ";
 } else {
  firstone = false;
 }
 if (type == "object") {
  var val = inspect(ob[i], "", ob);
  if (ob instanceof Array) {
   str += val;
  } else {
   str += i+": "+val;
  }
 } else {
  if (ob instanceof Array) {
   str += interpretValue(ob[i])
  } else if (ob instanceof Object) {
   str += i+":"+interpretValue(ob[i]);
  }
 }
}
if (ob instanceof Array) {
 str += "]";
} else if (ob instanceof Object) {
 str += "}";
}
return str;
}
function interpretValue(val) {
if (Number(val) === val){
 //number
 return val;
} else if (val == true || val == false) {
 return val;
} else {
 return "'"+val+"'";
}
return val;
}


Jobe Makar
http://www.electrotank.com
http://www.electro-server.com
phone: 919-609-0408
mobile: 919-610-5754
fax: 919-341-8104
----- Original Message ----- From: "Andreas Weber" <[EMAIL PROTECTED]>
To: "Flashcoders" <flashcoders@chattyfig.figleaf.com>
Sent: Wednesday, December 07, 2005 10:14 AM
Subject: [Flashcoders] universal toString() function


I'm quite sure that this must be around somewhere, but this time I didn't
have any luck searching the archives...

What I'm looking for is similar to a deep-copy/clone method (e.g.
Arul/Tatsuo
http://chattyfig.figleaf.com/mailman/htdig/flashcoders/2004-March/106149.htm
l) but instead of getting a clone of the object in return, I'd like to get a
String representation of the object.

Example of the desired functionality:

o = {a:1, b:2, c:{c1:['a','b','c'], c2:true}};
var s:String = universalToString(o);
trace(s);

Output:   {a:1, b:2, c:{c1:['a','b','c'], c2:true}}

In my case the object will not contain any methods, just (deeply nested)
'vanilla' Objects, Arrays, Strings, Numbers and Booleans.

Thanks for any pointers!

--------------
Andreas Weber
motiondraw.com



_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders



_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to