LATEST VERSION: 8.2.6 - CHANGELOG
Pivotal GemFire® v8.2

Implement the IGFSerializable Interface

Implement the IGFSerializable Interface

To store your own data types in the cache, you implement the GemFire IGFSerializable interface.

Examples follow the procedure.

Procedure

  1. Implement the ToData function that serializes your data:
    void ToData(DataOutput output)

    The ToData function is responsible for copying all of the data fields for the object to the object stream. The DataOutput class represents the output stream and provides methods for writing the primitives in a network byte order. For more about this, see the online API documentation for DataOutput.

  2. Implement the FromData function that consumes a data input stream and repopulates the data fields for the object:
    void fromData (DataInput& input)

    The DataInput class represents the input stream and provides methods for reading input elements. The FromData function must read the elements of the input stream in the same order that they were written by ToData. For more about this, see the online API documentation for DataInput.

  3. Implement the ClassId function to return an integer which is unique for your class (in the set of all of your user-defined classes).

Simple BankAccount Class

This example shows a simple class, BankAccount, that encapsulates two ints: customerId and accountId:
public class BankAccount
{
   private int m_customerId;
   private int m_accountId;
   public int Customer
   {
      get
      {
         return m_customerId;
      }
   }
   public int Account
   {
      get
      {
         return m_accountId;
      }
   }
   public BankAccount(int customer, int account)
   {
      m_customerId = customer;
      m_accountId = account;
   }
}

Implementing a Serializable Class

To make BankAccount serializable, you implement the IGFSerializable interface as shown in this example:
public class BankAccount : IGFSerializable
   {
   private int m_customerId;
   private int m_accountId;
   public int Customer
   {
      get
      {
         return m_customerId;
      }
   }
   public int Account
   {
      get
      {
         return m_accountId;
      }
   }
   public BankAccount(int customer, int account)
   {
      m_customerId = customer;
      m_accountId = account;
   }
   // Our TypeFactoryMethod
   public static IGFSerializable CreateInstance()
   {
      return new BankAccount(0, 0);
   }
   #region IGFSerializable Members
   public void ToData(DataOutput output)
   {
      output.WriteInt32(m_customerId);
      output.WriteInt32(m_accountId);
   }
   public IGFSerializable FromData(DataInput input)
   {
      m_customerId = input.ReadInt32();
      m_accountId = input.ReadInt32();
      return this;
   }
   public UInt32 ClassId
   {
      get
      {
         return 11;
      }
   }
   public UInt32 ObjectSize
   {
      get
      {
         return (UInt32)(sizeof(Int32) + sizeof(Int32));
      }
   }
}