public interface PdxSerializer
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 final String name; public final int userId; public User(String name, int userId) { this.name = name; this.userId = userId; } } 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 Object fromData(Class<?> clazz, PdxReader in) { if (User.class.isAssignableFrom(clazz)) { return new User(in.readString("name"), in.readInt("userId")); } else { return null; } } }
Modifier and Type | Method and Description |
---|---|
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.
|
boolean toData(Object o, PdxWriter out)
true
; otherwise it must return
false
in which case it will be serialized using standard serialization.o
- the object to consider serializing as a PDXout
- the PdxWriter
to use to serialize the objecttrue
if the method serialized the object; otherwise false
Object fromData(Class<?> clazz, PdxReader in)
clazz
- the Class of the object that should be deserializedin
- the reader to use to obtain the field datanull
indicates that this PdxSerializer does not
know how to deserialize the given class.