Parsing straight from a proto file w/o compiling (C++)

89 views
Skip to first unread message

Brett Smith

unread,
Jun 11, 2019, 9:43:49 AM6/11/19
to Protocol Buffers
Hello all, I'm brand new to protobufs and I've been following the AddressBook example given by Google (https://developers.google.com/protocol-buffers/docs/cpptutorial). I've been able to read and write protobufs but I've been wondering if it is possible to parse a proto file without  compiling it beforehand and if you are still able to access the elements of that file. If it is possible, would someone be able to give me some assistance on how to do this? I have attached the proto file that I have been working with for reference.
addressbook.proto

Adam Cozzette

unread,
Jun 11, 2019, 10:46:48 AM6/11/19
to Brett Smith, Protocol Buffers
Do you want to parse a .proto file or a file containing a serialized protobuf message?

On Tue, Jun 11, 2019 at 6:43 AM Brett Smith <bsmi...@umbc.edu> wrote:
Hello all, I'm brand new to protobufs and I've been following the AddressBook example given by Google (https://developers.google.com/protocol-buffers/docs/cpptutorial). I've been able to read and write protobufs but I've been wondering if it is possible to parse a proto file without  compiling it beforehand and if you are still able to access the elements of that file. If it is possible, would someone be able to give me some assistance on how to do this? I have attached the proto file that I have been working with for reference.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/53bbda9d-3898-47f2-af46-89093fd7c788%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brett Smith

unread,
Jun 11, 2019, 10:50:13 AM6/11/19
to Protocol Buffers

I want to be able to parse just the normal .proto file (the one I attached in the original post).

On Tuesday, June 11, 2019 at 10:46:48 AM UTC-4, Adam Cozzette wrote:
Do you want to parse a .proto file or a file containing a serialized protobuf message?

On Tue, Jun 11, 2019 at 6:43 AM Brett Smith <bsmi...@umbc.edu> wrote:
Hello all, I'm brand new to protobufs and I've been following the AddressBook example given by Google (https://developers.google.com/protocol-buffers/docs/cpptutorial). I've been able to read and write protobufs but I've been wondering if it is possible to parse a proto file without  compiling it beforehand and if you are still able to access the elements of that file. If it is possible, would someone be able to give me some assistance on how to do this? I have attached the proto file that I have been working with for reference.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prot...@googlegroups.com.

Adam Cozzette

unread,
Jun 11, 2019, 12:03:40 PM6/11/19
to Brett Smith, Protocol Buffers
The easiest way to do that is to invoke protoc and pass --descriptor_set_out=descriptors.out. That will cause protoc to parse the file for you and output it as a serialized FileDescriptorSet proto. You can then parse the FileDescriptorSet and from there it's easy to examine the structure of the .proto file.

To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.

To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.

Brett Smith

unread,
Jun 11, 2019, 12:07:10 PM6/11/19
to Protocol Buffers
Is that something you can do during runtime or does protoc need to parse the file beforehand?

Adam Cozzette

unread,
Jun 11, 2019, 12:17:03 PM6/11/19
to Brett Smith, Protocol Buffers
Either way should work, you could either shell out to protoc at runtime or do that beforehand. Alternatively you could avoid calling protoc at all but just look at its implementation and do the same thing it does to parse the .proto file.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.

Brett Smith

unread,
Jun 11, 2019, 12:28:57 PM6/11/19
to Protocol Buffers
Yeah, I would like to avoid using protoc if possible, but the reference pages to protobufs are a bit confusing to me, so figuring out how to manually parse has been an issue for me.


On Tuesday, June 11, 2019 at 12:17:03 PM UTC-4, Adam Cozzette wrote:
Either way should work, you could either shell out to protoc at runtime or do that beforehand. Alternatively you could avoid calling protoc at all but just look at its implementation and do the same thing it does to parse the .proto file.

On Tue, Jun 11, 2019 at 9:07 AM Brett Smith <bsmi...@umbc.edu> wrote:
Is that something you can do during runtime or does protoc need to parse the file beforehand?

On Tuesday, June 11, 2019 at 9:43:49 AM UTC-4, Brett Smith wrote:
Hello all, I'm brand new to protobufs and I've been following the AddressBook example given by Google (https://developers.google.com/protocol-buffers/docs/cpptutorial). I've been able to read and write protobufs but I've been wondering if it is possible to parse a proto file without  compiling it beforehand and if you are still able to access the elements of that file. If it is possible, would someone be able to give me some assistance on how to do this? I have attached the proto file that I have been working with for reference.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prot...@googlegroups.com.

Ahmet Can ÜN

unread,
Jun 11, 2019, 4:23:22 PM6/11/19
to Protocol Buffers
With google::protobuf::compiler::Importer class you can parse the proto file at run time and get
the filedescriptors.

You can even encode/decode proto messages at runtime without compiling .proto files.


Brett Smith

unread,
Jun 12, 2019, 8:06:58 AM6/12/19
to Protocol Buffers
Would you happen to have example code on how to do this? I've looked at the reference pages and am still confused on how to implement this.

Ahmet Can ÜN

unread,
Jun 23, 2019, 12:10:34 PM6/23/19
to Protocol Buffers
Hi Brett,

Sorry it took me so long to respond.

I've attached a .cpp and and a .proto file. Just put these two files under the same directory, then compile and run parse.cpp.

After this point the implementation will depend on what you want to achieve.
parse.cpp
sample.proto
Reply all
Reply to author
Forward
0 new messages