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 requiredorg.apache.geode.pdx
classes.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
PdxWriter
write methods. Geode automatically providesPdxWriter
to thetoData
method forPdxSerializable
objects. - Call the
PdxWriter
markIdentifyField
method 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 theequals
andhashCode
methods will use all PDX fields to compare objects and consequently, will not perform as well. It is important that the fields used by yourequals
andhashCode
implementations 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
toData
code:// 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.fromData
to read your data fields from the serialized form into the object’s fields using thePdxReader
read methods.Provide the same names that you did in
toData
and call the read operations in the same order as you called the write operations in yourtoData
implementation.Geode automatically provides
PdxReader
to thefromData
method forPdxSerializable
objects.Example
fromData
code: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
PdxInstance
for selective object deserialization. See Programming Your Application to Use PdxInstances.