views:

187

answers:

2

Hi there,

currently I'm translating an app from C# into ActionScript (Silverlight to Flex) and I need to serialize some XML content in ActionScript.

Now, unfortunately I need the variables to have an other name than the nodes in the XML file. I tried a workaround with getters and setters. It worked for setting the variables, but I failed at the getters, because when you make the variable private the native AS3 'SimpleXMLEncoder' ignores it.

The best solution would be, if I could give aliases to the variables. Thats the way it worked in C#. Just like this:

[DataMember(Name = "some")]
public someVariable Type { get; set; }

Is there any way to do something similar in ActionScript? I only found this one, which is for classes:

[RemoteClass(alias="user")]

I hope I described my first question well, and my english is ok. Thank you. :)

A: 

Not sure if there's a way to do it with the class itself, but could you add a method to your class that outputs a new object for use with XML serialization, and pass that to the SimpleXMLEncoder? In the object you return you'd set things to the names you'd want them to be, regardless of the actual variable names in the class.

Herms
Thanks. I hope I got you right.Creating a method for every class is not really an option here. I want to solve it in a more dynamic way. Firstly because my supervisor told me so. Secondly 'cause there are many Objects which would be hard to create and maintenance.The next Problem is, that the names of the xml nodes contain '-' which is not allowed in names of variables. So I would have to write one serializer per class.
plexus
+2  A: 

Hello plexus, it's funny because I just finished writing one of these for work after seeing how handy it was it C#. We may be releasing it as a library soon, but until then I can point you in the direction of the SpiceLib, specifically there section on using custom metadata here.

What we did was define our own metadata (attributes) tags called 'DataMember' and then created a serializer that would translate the Typed classes into Dynamic Objects such as:

[DataMember(Name="some")]
public var someVariable:Type;


//in Type, no MetaTags means we use the given accessor

public var someName:String;

//becomes {some:{someName:""}}

We then run this Dynamic Object through either the Adobe JSON encoder, or use the Flex XML serializer.

Hope this helps, will let you know if and when we release our library.

that's probably a more generally-useful way than what I suggested. Didn't know you could set up custom metadata tags.
Herms
But I guess you can't set them up without the SpiceLib Library, or can you?
plexus
sure, in fact I'm not using spicelib in my implementation. It's rather long winded for a comment box but essentially when you compile on the command line you needed to append the line `keep-as3-metadata DataMember` you then can use some inbuilt (but shoddy) reflection tools to get an XML description of a class at runtime `var classInfo:XML = describeType(new Sprite())`, in this xml you can find the xml node containing the metadata and parse appropriatly
Yesterday I figured out how to use custom metadata tags, I also added the 'keep metadata' line to the command line. Thats helps me a lot! Thanks. :)I guess "new Sprite()" should represent the object I want to parse. Maybe I'll try to figure that out too, since I didn't get it. ;)
plexus