Search code examples
javaserializationmapdb

Serialize and Deeserialize object in mapDb - Java


i'm trying to serialaze and deserialize an object to store it in mapDb.

I managed to serialize the Object using this snippet:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(bos);
os.writeObject(u);
result = bos.toString();

after that I stored "result" in mapDb. everything seemed to work like a charm.

Unfortunately I run in some issues while trying to deserialize it.

Here the snippet:

byte[] b = null;  
b = str.getBytes();
InputStream ac = new ByteArrayInputStream(b);
Object a= ac.read();

str is the serialized object coming from mapDB treated as a string. After that i "casted" it as a byteArray. I used this approach because I had some issues while fetching data from mapDb as Objects.

So, I'm asking you, how can I fix this problem. Beacuse Object "a" is an instance of java.lang.Integer, instead of the class desired, so deselrialization isn't working.


Solution

  • MapDB has build-in serialization, is that working?