Helen, Jobe - thanks for your replies!

Jobe: excellent, that comes very close and does help. 
The only issue I had was that the for-in loop messes up the order of the
items in the Array. While for one-dimensional Arrays it's just reversed
and can be 'cured' easily by a call to Array.reverse(), with
multi-dimensional Arrays the order of the for-in loop is a non-intuitive
mess.

Taking your ideas and starting over I came up with this:

/*
        Accepts a complex object and returns its String representation
        Handy for saving complex data structures as Strings 
        The object may contain nested Variables of the following types: 
        Object, Array, String, Number, Boolean
        
        Example:
        o = {a:1, b:'Beta', c:{c1:["c1_0", "c1_1", "c1_2"], c2:true}};
        var s:String = stringify(o);
        trace(s);
        //      Output:
        //  {a:1, b:"Beta", c:{c1:["c1_0", "c1_1", "c1_2"], c2:true}}
*/

function stringify(o:Object):String {
        s = arguments[1] ? s : '';
        var isArray:Boolean = o instanceof Array ? true : false;
        s += isArray ? '[' : '{';
        if(isArray) {
                for(var i=0, len=o.length; i<len; i++){
                        if(i){ s += ", "}
                        s+= (typeof o[i] == "object") ?  stringify(o[i],
"") : (typeof(o[i]) == 'string' ? '"'+o[i]+'"' : o[i]);
                }
        }else{
                var c = '';
                for(var p in o){
                        s += c + p +":"+((typeof o[p] == "object") ?
stringify(o[p], "") : (typeof(o[p]) == 'string' ? '"'+o[p]+'"' : o[p]));
                        c = ", ";
                }
        }
        s += isArray ? ']' : '}';
        return s;
}

It's not much more readable though ;-)

Thanks again!
Andreas Weber


-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Jobe
Makar
Sent: Mittwoch, 7. Dezember 2005 17:14
To: Flashcoders mailing list
Subject: Re: [Flashcoders] universal toString() function

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" <[email protected]>
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
> [email protected]
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> 


_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to