PdxSerializer (Apache Geode 2.0.2)
Description: declaration: package: org.apache.geode.pdx, interface: PdxSerializer

Package org.apache.geode.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 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;
         }
       }
     }

Since:
  GemFire 6.6

- ## Method Summary

  Modifier and Type
  Method
  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.

- ## Method Details

  - ### 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.