다음은 BlazeDS에서 기본적으로 지원되는 직렬화이다.
Serializing between ActionScript and Java
하지만 이런 기본 직렬화과정을 사용하지 않고 직렬화 자체를 커스터마이징(customizing)할 수 있다. 가령 클라이언트(예,Flash 애플리케이션)에서는 아이디, 이름, 속성, 가격의 정보가 중요하지만 서버(예,자바)에서는 재고(inventory)가 중요한 경우가 있다. 이런 경우에는 Flex와 Java쪽의 직렬화하는 클래스를 다음과 같이 디자인 할 수 있겠다.
// Product.as package samples.externalizable { import flash.utils.IExternalizable; import flash.utils.IDataInput; import flash.utils.IDataOutput; [RemoteClass(alias="samples.externalizable.Product")] public class Product implements IExternalizable { public function Product(name:String=null) { this.name = name; } public var id:int; public var name:String; public var properties:Object; public var price:Number; public function readExternal(input:IDataInput):void { name = input.readObject() as String; properties = input.readObject(); price = input.readFloat(); } public function writeExternal(output:IDataOutput):void { output.writeObject(name); output.writeObject(properties); output.writeFloat(price); } } }
// Product.java package samples.externalizable; import java.io.Externalizable; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; import java.util.Map; /** * This Externalizable class requires that clients sending and * receiving instances of this type adhere to the data format * required for serialization. */ public class Product implements Externalizable { private String inventoryId; public String name; public Map properties; public float price; public Product() { } /** * Local identity used to track third party inventory. This property is * not sent to the client because it is server-specific. * The identity must start with an 'X'. */ public String getInventoryId() { return inventoryId; } public void setInventoryId(String inventoryId) { if (inventoryId != null && inventoryId.startsWith("X")) { this.inventoryId = inventoryId; } else { throw new IllegalArgumentException("3rd party product inventory identities must start with 'X'"); } } /** * Deserializes the client state of an instance of ThirdPartyProxy * by reading in String for the name, a Map of properties * for the description, and * a floating point integer (single precision) for the price. */ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { // Read in the server properties from the client representation. name = (String)in.readObject(); properties = (Map)in.readObject(); price = in.readFloat(); setInventoryId(lookupInventoryId(name, price)); } /** * Serializes the server state of an instance of ThirdPartyProxy * by sending a String for the name, a Map of properties * String for the description, and a floating point * integer (single precision) for the price. Notice that the inventory * identifier is not sent to external clients. */ public void writeExternal(ObjectOutput out) throws IOException { // Write out the client properties from the server representation out.writeObject(name); out.writeObject(properties); out.writeFloat(price); } private static String lookupInventoryId(String name, float price) { String inventoryId = "X" + name + Math.rint(price); return inventoryId; } }
위 코드는 ActionScript의 flash.utils.IExternalizable 인터페이스와 Java의 java.io.Externalizable 인터페이스를 이용해 기본직렬화를 무시하고 이들 인터페이스에 정의된 readExternal와 writeExternal 메소드를 호출하여 직렬화 자체를 커스터마이징 할 수 있다는 것을 의미한다. Externalizable 인터페이스를 구현한 클래스에 정의된 메소드가 기본 직렬화보다 우선순위가 높다는 것을 기억하면 되겠다.
Flex의 ArrayCollection을 다시 한번 보기 바란다. 이 클래스는 flash.utils.IExternalizable를 구현했다.
mx.collections.ArrayCollection
꼭 이런 경우만은 아니다. 쓸데없는 데이터의 크기를 줄이기 위한 방법도 해당한다. 예를들어 ActionScript 코드를 보면 다음과 같다.
class Example implements IExternalizable { public var one:Boolean; public var two:Boolean; public var three:Boolean; public var four:Boolean; public var five:Boolean; public var six:Boolean; public var seven:Boolean; public var eight:Boolean; public function writeExternal(output:IDataOutput) { var flag:int = 0; if (one) flag |= 1; if (two) flag |= 2; if (three) flag |= 4; if (four) flag |= 8; if (five) flag |= 16; if (six) flag |= 32; if (seven) flag |= 64; if (eight) flag |= 128; output.writeByte(flag); } public function readExternal(input:IDataInput) { var flag:int = input.readByte(); one = (flag & 1) != 0; two = (flag & 2) != 0; three = (flag & 4) != 0; four = (flag & 8) != 0; five = (flag & 16) != 0; six = (flag & 32) != 0; seven = (flag & 64) != 0; eight = (flag & 128) != 0; } }
데이터 통신을 위해 쓸데없이 Boolean객체를 주고 받을 필요없다. 위 코드처럼 직렬화를 커스터마이징한다면 송수신 데이터 자체의 크기도 줄일 수 있다. 이는 매우 유용하다.
참고글
flash.utils.IExternalizable
커스텀 직렬화의 사용
ActionScript 3.0 데이터 유형 및 직렬화(serialization)
Serializing between ActionScript and Java
AMF 3 스팩
AS3 BitmapData AMF solution using IExternalizable
Flex and PHP: remoting with Zend AMF
글쓴이 : 지돌스타(http://blog.jidolstar.com/644)
'비공개 > Adobe Flex, ActionScript 3.0' 카테고리의 다른 글
[Flash/ActionScript 3.0] stageWidth, stageHeight 가 0(zero)인 것을 해결하는 방법 (6) | 2010.02.10 |
---|---|
[Flash/ActionScript]Wonderfl의 멋진 공개 플래시 코드 (8) | 2010.01.07 |
Papervison3D와 Away3DLite를 위한 GeodesicSphere (5) | 2010.01.04 |
[Flex/AIR]BitmapData를 PNG나 JPG로 변환하여 ByteArray로 서버에 전송하는 방법 (42) | 2009.12.30 |
철자(Spell) 체크 엔진 Squiggly (Flash/Flex/AIR) (0) | 2009.12.28 |