5

I would like to use protobuf with a C++ project I'm working on. However, I don't like to work with the auto-generated classes protoc creates and prefer to stick with the POCOs I already have. This is because the POCOs are already in use in other parts of the code and I want to be able to switch the serialization mechanism with ease later on. But manually writing converters between POCOs and protobuf message classes seems tedious and wrong.

I want to know if there's a way to use protobuf to create a serializer - an auto-generated class that will be able to serialize and deserialize my POCOs, without bugging me with internals.

Thanks.

Vadim
  • 2,847
  • 15
  • 18
  • Can you just write your POCOs directly to the wire/disk? Of course this won't work if they contain pointers.... – John Zwinck Jul 07 '13 at 06:09
  • My POCOs contain primitive types, strings, pointers to other POCOs and vectors of the above. Pointers will need to be handled as well (for example by serializing the pointed object as well and referencing it with some id). Writing directly is serializing, I can do that manually, I'm just trying to find a way to avoid it. – Vadim Jul 07 '13 at 06:24
  • Avro (http://avro.apache.org/) may be closer to what you want (or it would be in java). Also with Protocol Buffers you are not restricted to the official version; you can write your own code generator. There is one other C/C++ version https://sourceforge.net/projects/spbc/files/spbc/spbc-1.0.1/ listed. It has not been changed in 4 years so will be out of date – Bruce Martin Jul 07 '13 at 07:21
  • There are other c versions of protocol buffers listed in http://code.google.com/p/protobuf/wiki/ThirdPartyAddOns – Bruce Martin Jul 07 '13 at 07:28
  • Bruce Martin - Like you said, Java is irrelevant. Writing my own code generator is exactly what I wish to avoid and the reason I've asked this question. Simple Protobuf does not seem relevant (or documented, or maintained) and I don't see anything useful in the 3rd party addons page. Thanks anyway. – Vadim Jul 07 '13 at 08:58
  • There is no such thing as a POCO in C++. Java's POJOs are called POD in C++. (Or rather: PODs are called POJO in Java, as PODs existed before Java was invented) – Sjoerd Jul 07 '13 at 12:11
  • @Sjoerd, I stand corrected. I assumed that POCO stands for Plain Old C\C++ Object, while it stands from Plain Old CLR Object. Look at - http://stackoverflow.com/questions/725348/poco-vs-dto "POCO describes an approach to programming (good old fashioned object oriented programming), where DTO is a pattern that is used to "transfer data" using objects". Thanks, but I don't think this makes any difference. – Vadim Jul 07 '13 at 14:26

1 Answers1

1

First, you may like Cap'n Proto better, it was created by one of Google's former Google Protocol Buffer maintainers. Worth looking into, anyway.

But otherwise, you really need to consider why you're using Google Protocol Buffers.

If you want to achieve the forward and backward compatibility, and to be able to open, then edit, then save an object that possibly a different person created, with a different version of your protocol buffer declaration, and then sent along to yet another person with an even different version of the declaration... then you need to just bite the bullet and use the generated C++ from the Google Protocol Buffer Compiler.

It's really not just a serialization format. It's specifically designed to make it easy living with different versions of your serialization, over time.

If you don't need that flexibility, and you don't like the generated code, you may want to consider a different serialization tool.

Matt Cruikshank
  • 2,932
  • 21
  • 24
  • Hey. Thanks. Capn'n Proto looks interesting and I'll take a look at it. It's just not relevant to the work at hand (no support for Java, the product is too new to be used in production etc). I want to use protobuf because of the following reasons - It's fast and compact and it has inherit C++ and Java support. Versions are not really an issue. I don't mind the generated code, in fact - I don't care at all what's being generated. I just want the generated code to work on my POCOs or POJOs. I can write something like this myself and I just wanted to make sure this is not already done. – Vadim Aug 02 '13 at 07:38
  • Google Protocol Buffers will always generate code (which is why you need to care), and will never work on your POCOs or POJOs. So, it may not be right for you. I'm unaware of any serialization library that will work on your POCOs. – Matt Cruikshank Aug 02 '13 at 14:23