Serialize HashMap Coherence

Serialize HashMap Coherence

If you are using Oracle coherence and trying to serialize and desrialize HashMap into the cache please use below:

  • To show the full example I will create JavaHonkModel.java with one Map attribute:
import java.util.Map;

public class JavaHonkModel {
	
	private Map<String, String> map;

	public JavaHonkModel(Map<String, String> map) {
		this.map = map;
	}

	public Map<String, String> getMap() {
		return map;
	}

	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	
	

}
  • SerializeHashMap.java: To serialize HashMap please use below:
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

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

public class SerializeHashMap implements PofSerializer{
	
	@SuppressWarnings("unchecked")
	@Override
	public Object deserialize(PofReader pofReader) throws IOException {
		
		int count = 0;
		
		Map<String, String> map = pofReader.readMap(count++, new HashMap<String, String>());	
		
		pofReader.readRemainder();
		
		return new JavaHonkModel(map);

	}

	@Override
	public void serialize(PofWriter pofWriter, Object o) throws IOException {
		
		JavaHonkModel JavaHonkModel = (JavaHonkModel) o;
		
		int count = 0;
		
		pofWriter.writeMap(count++, JavaHonkModel.getMap());
		
		pofWriter.writeRemainder(null);
		
	}
}

Reference:

Leave a Reply

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