0

I am trying to serialize an object into a byte array using protocol buffers at runtime. To achieve this I need to parse a string containing the .proto file content(created by mapping java class to proto) and compile it using protoc to generate a message and serialize it.

Is this approach doable? and if so are there any references that I can follow to implement this?

My main requirement is to serialize an object into a byte array using protocol buffers without generating a .proto file. The main method would look something like this.

    Student s1 = new Student('name', 'id');

    proto pr = protoFromClass(Student);
    byte[] serializedPr = pr.serialize(s1);
  • The tool to convert .proto files into the appropriate Java classes is written in C++, so I don't think there's a way to achieve this without either calling native code *or* using a third-party protobuf implementation (I don't know if such a thing exists). – Joachim Sauer Jan 12 '21 at 09:40
  • [This question](https://stackoverflow.com/questions/18836727/protocol-buffer-objects-generated-at-runtime) might give some useful pointers, but it's not quite a duplicate, I think. And [this library](https://github.com/os72/protobuf-dynamic) seems to be such a third-party library that I mentioned above. I have absolutely no knowledge if it's any good. – Joachim Sauer Jan 12 '21 at 09:41
  • @JoachimSauer Is it possible to compile .proto and keep the output in-memory. Without generating a file? – Tharinda Dilshan Piyadasa Jan 12 '21 at 09:44

0 Answers0