Closed GoogleCodeExporter closed 8 years ago
Original comment by darron.schall
on 29 Dec 2006 at 7:49
A possible fix to this issue would be the following code. Note that this code
has
not been extensively tested, but seems to work under preliminary analysis:
/**
* Converts an object to it's JSON string equivalent
*
* @param o The object to convert
* @return The JSON string representation of <code>o</code>
*/
private function objectToString( o:Object ):String
{
// create a string to store the object's jsonstring value
var s:String = "";
// determine if o is a class instance or a plain object
var classInfo:XML = describeType( o );
if ( classInfo.@name.toString() == "Object" )
{
// the value of o[key] in the loop below - store this
// as a variable so we don't have to keep looking up o[key]
// when testing for valid values to convert
var value:Object;
// loop over the keys in the object and add their converted
// values to the string
for ( var key:String in o )
{
// assign value to a variable for quick lookup
value = o[key];
// don't add function's to the JSON string
if ( value is Function )
{
// skip this key and try another
continue;
}
// when the length is 0 we're adding the first item so
// no comma is necessary
if ( s.length > 0 ) {
// we've already added an item, so add the comma separator
s += ","
}
s += escapeString( key ) + ":" + convertToString( value );
}
}
else // o is a class instance
{
// Loop over all of the variables in the class and serialize them
// along with their values.
for each ( var v:XML in classInfo..variable )
{
// When the length is 0 we're adding the first item so
// no comma is necessary
if ( s.length > 0 ) {
// We've already added an item, so add the comma separator
s += ","
}
s += escapeString( v.@name.toString() ) + ":"
+ convertToString( o[ v.@name ] );
}
}
return "{" + s + "}";
}
Original comment by darron.schall
on 29 Dec 2006 at 8:17
Along with looking for the variables in the case of a class instance, we also
need to
look for accessors:
for each ( var v:XML in classInfo..*.( name() == "variable" || name() ==
"accessor" ) )
Original comment by darron.schall
on 2 Jan 2007 at 8:58
Original comment by mikechambers
on 14 Feb 2007 at 6:03
Fixed in revision 23.
Original comment by darron.schall
on 21 Feb 2007 at 3:15
Original issue reported on code.google.com by
darron.schall
on 29 Dec 2006 at 7:49