 Apache Geode
  
    CHANGELOG
  Apache Geode
  
    CHANGELOG
  
        
  Implementing PdxSerializable in Your Domain Object
For a domain object with source that you can modify, implement the PdxSerializable interface in the object and use its methods to serialize and deserialize the object’s fields.
- In your domain class, implement - PdxSerializable, importing the required- org.apache.geode.pdxclasses.- For example: - import org.apache.geode.pdx.PdxReader; import org.apache.geode.pdx.PdxSerializable; import org.apache.geode.pdx.PdxWriter; public class PortfolioPdx implements PdxSerializable { ...
- If your domain class does not have a zero-arg constructor, create one for it. - For example: - public PortfolioPdx(){}
- Program - PdxSerializable.toData.-  Write each standard Java data field of your domain class using the PdxWriterwrite methods. Geode automatically providesPdxWriterto thetoDatamethod forPdxSerializableobjects.
-  Call the PdxWritermarkIdentifyFieldmethod for each field you want to have Geode use to identify your object. Put this after the field’s write method. Geode uses this information to compare objects for operations like distinct queries. If you do not set as least one identity field, then theequalsandhashCodemethods will use all PDX fields to compare objects and consequently, will not perform as well. It is important that the fields used by yourequalsandhashCodeimplementations are the same fields that you mark as identity fields.
- For a particular version of your class, you need to consistently write the same named field each time. The field names or number of fields must not change from one instance to another for the same class version.
- For best performance, do fixed width fields first and then variable length fields. - Example - toDatacode:- // PortfolioPdx fields private int id; private String pkid; private Map<String, PositionPdx> positions; private String type; private String status; private String[] names; private byte[] newVal; private Date creationDate; ... public void toData(PdxWriter writer) { writer.writeInt("id", id) // The markIdentifyField call for a field must // come after the field's write method .markIdentityField("id") .writeDate("creationDate", creationDate) //fixed length field .writeString("pkid", pkid) .writeObject("positions", positions) .writeString("type", type) .writeString("status", status) .writeStringArray("names", names) .writeByteArray("newVal", newVal) }
 
-  Write each standard Java data field of your domain class using the 
- Program - PdxSerializable.fromDatato read your data fields from the serialized form into the object’s fields using the- PdxReaderread methods.- Provide the same names that you did in - toDataand call the read operations in the same order as you called the write operations in your- toDataimplementation.- Geode automatically provides - PdxReaderto the- fromDatamethod for- PdxSerializableobjects.- Example - fromDatacode:- public void fromData(PdxReader reader) { id = reader.readInt("id"); creationDate = reader.readDate("creationDate"); pkid = reader.readString("pkid"); position1 = (PositionPdx)reader.readObject("position1"); position2 = (PositionPdx)reader.readObject("position2"); positions = (Map<String, PositionPdx>)reader.readObject("positions"); type = reader.readString("type"); status = reader.readString("status"); names = reader.readStringArray("names"); newVal = reader.readByteArray("newVal"); arrayNull = reader.readByteArray("arrayNull"); arrayZeroSize = reader.readByteArray("arrayZeroSize"); }
What to do next
-   As needed, configure and program your Geode applications to use PdxInstancefor selective object deserialization. See Programming Your Application to Use PdxInstances.