Serialize Enum Coherence Example

Serialize Enum Coherence Example

Serializing Enum on Oracle coherence is little confusing because coherence does not supports Enum. So how will you serialize and deserialize data to Oracle coherence? In fact it’s very easy and you can serialize Enum as String and while deserialize it convert it back to Enum. Please see example below:

  • EnumTest.java:
public enum EnumTest {
	
	JavaHonk

}
  • EnumTestModel.java:
public class EnumTestModel {
	
	private EnumTest enumTest;

	public EnumTestModel(EnumTest enumTest) {
		this.enumTest = enumTest;
	}

	public EnumTest getEnumTest() {
		return enumTest;
	}

	public void setEnumTest(EnumTest enumTest) {
		this.enumTest = enumTest;
	}

}
  • EnumTestModelSerializer.java:
import java.io.IOException;

import com.tangosol.io.pof.PofReader;
import com.tangosol.io.pof.PofSerializer;
import com.tangosol.io.pof.PofWriter;

public class EnumTestModelSerializer implements PofSerializer{
	
	@Override
	public Object deserialize(PofReader pofReader) throws IOException {
		
		int count = 0;
		
		EnumTest enumTest = EnumTest.valueOf(pofReader.readString(count++));
		
		pofReader.readRemainder();
		
		return new EnumTestModel(enumTest);
	}

	@Override
	public void serialize(PofWriter pofWriter, Object o) throws IOException {
		
		EnumTestModel enumTestModel = (EnumTestModel) o;
		
		int count = 0;
		
		pofWriter.writeString(count++, enumTestModel.getEnumTest().name());
		
		pofWriter.writeRemainder(null);
		
	}
}

Reference:

Leave a Reply

Your email address will not be published. Required fields are marked *