GemFire 6.6.1

com.gemstone.gemfire.pdx
Interface PdxSerializer

All Known Implementing Classes:
ReflectionBasedAutoSerializer

public interface PdxSerializer

The PdxSerializer interface allows domain classes to be serialized and deserialized as PDXs without modification of the domain class. It only requires that the domain class have a public zero-arg constructor and that it provides read and write access to the PDX serialized fields.

GemFire allows a single PdxSerializer to be configured on a cache using setPdxSerializer or client setPdxSerializer. It can also be configured in cache.xml using the pdx-serializer element. The same PdxSerializer should be configured on each member of a distributed system that can serialize or deserialize PDX data.

The toData method is used for serialization; fromData is used for deserialization. The order in which fields are serialized and deserialized in these methods for the same class must match. For the same class toData and fromData must write the same fields each time they are called.

If you can modify your domain class then use PdxSerializable instead.

Simple example:

public class User {
  public String name;
  public int userId;

  public User() {
  }
}

public class MyPdxSerializer implements PdxSerializer {
  public boolean toData(Object o, PdxWriter out) {
    if (o instanceof User) {
      User u = (User)o;
      out.writeString("name", u.name);
      out.writeInt("userId", u.userId);
      return true;
    } else {
      return false;
    }
  }
  public boolean fromData(Object o, PdxReader in) {
    if (o instanceof User) {
      User u = (User)o;
      u.name = in.readString("name");
      u.userId = in.readInt("userId");
      return true;
    } else {
      return false;
    }
  }
}
 

Since:
6.6

Method Summary
 Object fromData(Class<?> clazz, PdxReader in)
          This method is given an class that should be instantiated and deserialized using the given reader.
 boolean toData(Object o, PdxWriter out)
          This method is given an object to serialize as a PDX using the given writer.
 

Method Detail

toData

boolean toData(Object o,
               PdxWriter out)
This method is given an object to serialize as a PDX using the given writer. If it chooses to do the serialization then it must return true; otherwise it must return false in which case it will be serialized using standard serialization.

Parameters:
o - the object to consider serializing as a PDX
out - the PdxWriter to use to serialize the object
Returns:
true if the method serialized the object; otherwise false

fromData

Object fromData(Class<?> clazz,
                PdxReader in)
This method is given an class that should be instantiated and deserialized using the given reader.

Parameters:
clazz - the Class of the object that should be deserialized
in - the reader to use to obtain the field data
Returns:
the deserialized object. null indicates that this PdxSerializer does not know how to deserialize the given class.

GemFire 6.6.1

Copyright © 1997-2011 VMware, Inc. All rights reserved.