By default, private properties are not written when an object is serialized : ByteArray « Development « Flash / Flex / ActionScript






By default, private properties are not written when an object is serialized

 
package {
    import flash.display.Sprite;
    import flash.net.*;
    import flash.utils.ByteArray;

    public class CustomTypeExample extends Sprite {
        public function CustomTypeExample() {
            registerClassAlias("ExampleType", ExampleType);
            var example1:ExampleType = new ExampleType(1, 2);
            var byteArray:ByteArray = new ByteArray();
            byteArray.writeObject(example1);
            byteArray.position = 0;
            var example2:ExampleType = byteArray.readObject() as ExampleType;
            trace(example2.getA());
            trace(example2.getB());
        }
    }
}


    class ExampleType implements flash.utils.IExternalizable {

        private var _a:Number;
        private var _b:Number;

        public function ExampleType(a:Number = -1, b:Number = -1) {
            if(a != -1) {
                _a = a;
            }
            if(b != -1) {
                _b = b;
            }
        }

        public function getA():Number {
            return _a;
        }

        public function getB():Number {
            return _b;
        }

        public function writeExternal(output:flash.utils.IDataOutput):void {
            output.writeFloat(_a);
            output.writeFloat(_b);
        }

        public function readExternal(input:flash.utils.IDataInput):void {
            _a = input.readFloat();
            _b = input.readFloat();
        }
    }

        








Related examples in the same category

1.Creating a Byte Array
2.Write array to ByteArray
3.Embedding Files as Binary Data
4.Embedding XML at compile time
5.Common Uses of Byte Arrays