Class DataSerializer


  • public abstract class DataSerializer
    extends Object
    Provides static helper methods for reading and writing non-primitive data when working with a DataSerializable. For instance, classes that implement DataSerializable can use the DataSerializer in their toData and fromData methods:
     public class Employee implements DataSerializable {
       private int id;
       private String name;
       private Date birthday;
       private Company employer;
    
       public void toData(DataOutput out) throws IOException {
         out.writeInt(this.id);
         out.writeUTF(this.name);
         DataSerializer.writeDate(this.birthday, out);
         DataSerializer.writeObject(this.employer, out);
       }
    
       public void fromData(DataInput in) throws IOException, ClassNotFoundException {
    
         this.id = in.readInt();
         this.name = in.readUTF();
         this.birthday = DataSerializer.readDate(in);
         this.employer = (Company) DataSerializer.readObject(in);
       }
     }
    
     

    Instances of DataSerializer are used to data serialize objects (such as instances of standard Java classes or third-party classes for which the source code is not available) that do not implement the DataSerializable interface.

    The following DataSerializer data serializes instances of Company. In order for the data serialization framework to consult this custom serializer, it must be registered with the framework.

    public class CompanySerializer extends DataSerializer {
    
      static {
        DataSerializer.register(CompanySerializer.class);
      }
    
      /**
     May be invoked reflectively if instances of Company are
     distributed to other VMs.
    /
      public CompanySerializer() {
    
      }
    
      public Class[] getSupportedClasses() {
        return new Class[] { Company.class };
      }
      public int getId() {
        return 42;
      }
    
      public boolean toData(Object o, DataOutput out)
        throws IOException {
        if (o instanceof Company) {
          Company company = (Company) o;
          out.writeUTF(company.getName());
    
          // Let's assume that Address is java.io.Serializable
          Address address = company.getAddress();
          writeObject(address, out);
          return true;
    
        } else {
          return false;
        }
      }
    
      public Object fromData(DataInput in)
        throws IOException, ClassNotFoundException {
    
        String name = in.readUTF();
        Address address = (Address) readObject(in);
        return new Company(name, address);
      }
    }
     
    Just like Instantiators, a DataSerializer may be sent to other members of the distributed system when it is registered. The data serialization framework does not require that a DataSerializer be Serializable, but it does require that it provide a zero-argument constructor.
    Since:
    GemFire 3.5
    See Also:
    writeObject(Object, DataOutput), readObject(java.io.DataInput)