Search code examples
javaandroidserializationprotocol-buffersparcelable

Serialize (or Parse) a Protobuf Object


I have a Protobuf java object and I need to save it (in a bundle in android for example).

So need to have a Serializable or Parcelable object, how can I get this object from my Protobuf object. Or is there any other way to save it and get the object later?


Solution

  • Protobuf is create for serialize purpose so I recommend you no need to serialize it again to a Serializable or Parcelable object. Instead of, serialize it to byte array and put byte array to to bundle.

    byte[] toByteArray();
    

    ref: https://developers.google.com/protocol-buffers/docs/javatutorial#parsing-and-serialization