Java: object to byte[] and byte[] to object converter (for Tokyo Cabinet)

Viewed 175910

I need to convert objects to a byte[] to be stored in the Tokyo Cabinet key-value store. I also need to unbyte the byte[] to an Object when reading from the key-value store.

Are there any packages out there that will help me with this task? Or would the best solution to implement it myself?

6 Answers

You can use ObjectMapper

        ObjectMapper objectMapper = new ObjectMapper();
        ObjectClass object = objectMapper.readValue(data, ObjectClass.class);

If you do not want to serialize you can use object mapper

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());

ByteArrayOutputStream out = new ByteArrayOutputStream();
objectMapper.writeValue(out,obj);
byte[] data = out.toByteArray();

When should we implement Serializable interface?

Related