amf serialization between as3 and java

I was cleaning out my blog and I found this old post in draft form. I don't know if anyone other than me will find it interesting, and yet I'm posting it! But I don't think the internet minds. The internet is nice like that.

~
Ok another inside baseball post for all those flash/java coders in my blog readership*. So, you say you need to pass objects back and forth between Java and AS3. You say you've tried using Xstream XML serialization, but it's just too slow? Let me tell you, you definitely want to be using AMF. It's Adobe's native object transfer format, and it is wicked fast**.

Ok so that means you want to use BlazeDS on the java side, and some kind of native deserializer*** on the client side. That sounds pretty simple! But since I know you're a vry srs software engineer, I know that you need your objects to be strongly typed and type-safe, so that you can plug them into the rest of your client and server logic. Well, to do that you need to carefully line up all of your classes on the java side and the flash side so that they are the same, and then register each AS3 class with a call to registerClassAlias. Ok still not so bad.

Next, forget about using AS3 enums. The AMF deserializer basically won't let you do it, and you'll lose the benefit of having an enum class to begin with. You can squirm and complain, but you're not getting out of this one: Java enums work fine. AS3 enums do not work. You must use primitive strings. You will lose enum type checking and compile time safety. DEAL.

Now, you're pretty close, but there are a couple more things to take note of. If you're working in vanilla actionscript 3, you don't have the mx packages linked in. That means that when BlazeDS sees a Java List, and it sends you something called an ArrayCollection, flash will barf. So you need to set the legacyCollection flag on the SerializationContext object before you serialize your java objects.


*pfft.
**about 40 times faster than our (admittedly unoptimized) as3 implementation of xstream. 40 times is a lot of times to be faster by.
***pronounced de-cereal-izer, which is also a great word. I think that's what you call someone who eats all the crumbs at the bottom of the 3 nearly-empty cereal boxes in the pantry.

No comments:

Post a Comment