code: public static byte[] objectToBytes(Object object) throws IOException {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ObjectOutputStream os = new ObjectOutputStream(baos);
      os.writeObject(object);
      return baos.toByteArray();
    }
docs: Converts a serializable object to a byte array.