2 Replies Latest reply on May 16, 2007 3:24 PM by bldrbldr

    AS3 serialization of Java enum

    Mongolian Beef Level 1
      I'm trying to write a custom AS3 code that implements IExternalizable that can serialize/deserialize a Java enum (for use with FDS or GDS). I tried writing it but think its incorrect since I get nulls all the time. Below is my code:

      package com.test.core.item {

      import flash.utils.IDataInput;
      import flash.utils.IDataOutput;

      [Bindable]
      public class ItemTypesBaseBase extends IExternalizable {

      private var _name:String = null;

      public function set name(value:String):void {
      _name = value;
      }
      public function get name():String {
      return _name;
      }

      public override function readExternal(input:IDataInput):void {
      _name = input.readObject() as String;
      }

      public override function writeExternal(output:IDataOutput):void {
      output.writeObject(_name);
      }
      }
      }

      Any help would be appreciated. Thanks.

      LH
        • 1. AS3 serialization of Java enum
          VarioPegged Level 2
          Your class declaration syntax is wrong if you want to implement IExternalizable. It should be:

          [Bindable]
          public class ItemTypesBaseBase implements IExternalizable

          Also, why are you overriding the functions readExternal and writeExternal when ItemTypesBaseBase isn't extending a super class?
          • 2. Re: AS3 serialization of Java enum
            bldrbldr
            Here's my example solution:
            Java Code:
            ---------------

            import java.io.Externalizable;
            import java.io.IOException;
            import java.io.ObjectInput;
            import java.io.ObjectOutput;

            public enum LevelOfMeasurement implements Externalizable {
            NOMINAL,
            ORDINAL,
            INTERVAL,
            RATIO;

            public void writeExternal(ObjectOutput output) throws IOException {
            output.writeUTF(name());
            }

            public void readExternal(ObjectInput input) throws IOException, ClassNotFoundException {
            input.readUTF(); // discard the name
            }
            }


            --------------
            AS Code
            --------------
            import flash.utils.IExternalizable;

            [RemoteClass(alias="LevelOfMeasurement")]
            public class LevelOfMeasurement implements IExternalizable {
            public static const NOMINAL:LevelOfMeasurement = new LevelOfMeasurement("NOMINAL");
            public static const ORDINAL:LevelOfMeasurement = new LevelOfMeasurement("ORDINAL");
            public static const INTERVAL:LevelOfMeasurement = new LevelOfMeasurement("INTERVAL");
            public static const RATIO:LevelOfMeasurement = new LevelOfMeasurement("RATIO");

            private var privateName:String;

            public function LevelOfMeasurement(str:String = null) { privateName = str; }
            public function toString():String { return privateName; }
            public function get name():String { return privateName; }
            public function readExternal(input:flash.utils.IDataInput):void {
            privateName = input.readUTF();
            }
            public function writeExternal(output:flash.utils.IDataOutput):void {
            output.writeUTF(privateName);
            }
            }
            -----------
            The big downside is that == operator does not work in AS. If you figure that one out, let me know.