Serialize Using the PdxSerializable Class
Domain classes need to inherit the PdxSerializable
abstract class to serialize and de-serialize the object. When you write objects using PDX serialization, they are distributed to the server tier in PDX serialized form.
When you run queries against the objects on the servers, only the fields you specify are deserialized. A domain class should serialize and de-serialize all its member fields in the same order in its toData
and fromData
method.
Use this procedure to program your domain object for PDX serialization using the PdxSerializable
abstract class.
In your domain class, implement
PdxSerializable
. Example:class PdxObject: public PdxSerializable
Program the
toData
function to serialize your object as required by your application.If you also use PDX serialization in Java or .NET for the object, serialize the object in the same way for each language. Serialize the same fields in the same order and mark the same identity fields.
Program the
fromData
method to read your data fields from the serialized form into the object’s fields.In your
fromData
implementation, use the same name as you did intoData
and call the read operations in the same order as you called the write operations in yourtoData
implementation.Optionally, program your domain object’s hashCode and equality methods.
Use the
markIdentityField
method to indicate that the given field name should be included in hashCode and equality checks of this object on a server.The fields that are marked as identity fields are used to generate the hashCode and equality methods of PdxInstance. Because of this, the identity fields should themselves either be primitives, or implement hashCode and equals.
If no fields are set as identity fields, then all fields will be used in hashCode and equality checks. The identity fields should make marked after they are written using a
write
* method.
PdxSerializable Example
class PdxObject: public PdxSerializable {
private:
uint32_t m_id;
char* m_str;
public:
PdxObject(){};
PdxObject(uint32_t id, char* str);
virtual ~PdxObject();
uint32_t getID() {
return m_id;
}
char* getStr(){
return m_str;
}
virtual void toData(PdxWriterPtr pw) const;
virtual void fromData(PdxReaderPtr pr);
CacheableStringPtr toString() const;
virtual char* getClassName() const;
static Cacheable* createDeserializable() {
return new PdxObject();
}
};
PdxObject::PdxObject(uint32_t i, char* str) {
m_id = i;
m_str = str;
}
PdxObject::~PdxObject() {
}
void PdxObject::toData( PdxWriterPtr pw ) const {
pw->writeInt("id", m_id);
pw->markIdentityField("id");
pw->writeString("str", m_str);
}
void PdxObject::fromData( PdxReaderPtr pr )
{
m_id = pr->readInt("id");
m_str = pr->readString("str");
}
char* getClassName() const{
{
return "com.example.PdxType";
}
CacheableStringPtr PdxObject::toString() const {
char idbuf[1024];
sprintf(idbuf,"PdxObject: [ ID=%d ]",m_id);
return CacheableString::create( idbuf );
}