LATEST VERSION: 9.0.6 - CHANGELOG
Pivotal GemFire® Native Client v9.0

Extend the PDX Autoserializer

This example code demonstrates how to extend the autoserializer to customize serialization.

Extending the Autoserializer

public class AutoSerializerEx : ReflectionBasedAutoSerializer
{
   public override object WriteTransform(FieldInfo fi, Type type, object originalValue) {
      if (fi.FieldType.Equals(Type.GetType("System.Guid"))) {
        return originalValue.ToString();
      } else if (fi.FieldType.Equals(Type.GetType("System.Decimal"))) {
        return originalValue.ToString();
      } else
        return base.WriteTransform(fi, type, originalValue);
    }

    public override object ReadTransform(FieldInfo fi, Type type, object serializeValue) {
      if (fi.FieldType.Equals(Type.GetType("System.Guid"))) {
        Guid g = new Guid((string)serializeValue);
        return g;
      } else if (fi.FieldType.Equals(Type.GetType("System.Decimal"))) {
        return Convert.ToDecimal((string)serializeValue);
      } else
        return base.ReadTransform(fi, type, serializeValue);
    }

   public override FieldType GetFieldType(FieldInfo fi, Type type) {
      if (fi.FieldType.Equals(Type.GetType("System.Guid")) ||
              fi.FieldType.Equals(Type.GetType("System.Decimal")))
        return FieldType.STRING;
      return base.GetFieldType(fi, type);
   }

   public override bool IsIdentityField(FieldInfo fi, Type type) {
      if (fi.Name == "_identityField")
        return true;
      return base.IsIdentityField(fi, type);
   }

   public override string GetFieldName(FieldInfo fi, Type type) {
      if (fi.Name == "_nameChange")
        return fi.Name + "NewName";
      return fi.Name;
    }

   public override bool IsFieldIncluded(FieldInfo fi, Type type)
   {
      if (fi.Name == "_notInclude")
        return false;
      return base.IsFieldIncluded(fi, type);
    }
}