0
votes

In google protocol buffer, there exists a textual version of message. When parsing this textual message, can we define ourselves the callback functions in order that we could store the information parsed into our own data structure? For example, if we have defined .proto:

message A {
    required string name = 1;
    optional string value =2;
    repeated B bList =3;
}

message B {
    required string name =1;
    optional string value =2;
}

And we have textformat message:

A {
    name: "x"
    value: "123"
    B {
        name: "y"
        value: "987"
    }
    B {
        name: "z"
        value: "965"
    }
}

The protobuf compiler generates the corresponding class named "A", class named "B". The parser can parse this text format into the instance of A. However, if user want to defined our own version of class "A", or there exists a version of A used before. Now as we would like to replace the old exchange format by google protocol buffer, we are willing to parse the google protocol buffer text format version directly into the old data structure. If not, we will have to first of all have the generated data structure (class "A") filled then adapt the generated data structure to the legacy data structure. It occupies two times the memory than necessary. It can be much less efficient than we wanted. The traditional method used for integrating a parser is to have a parser that can callback self-defined functors to be accustomed to the new data structure. So, does there exist a way to inject the self-defined callback function into the text format parser?

1
If you are so concerned about memory usage, the Google's C++ protobuf library is probably not your best choice anyway. It focuses more on speed than on conserving RAM, which is quite plentiful on modern non-embedded systems. - jpa

1 Answers

1
votes

No, the protobuf TextFormat implementation does not support such extensions.

That said, TextFormat (in at least C++, Java, and Python) is implemented as a self-contained module that operates only on public interfaces (mainly, the reflection interface). You can easily clone it and then make your own modifications to the format, or even write a whole new module in the same style that implements any arbitrary format. For example, many people have written JSON parsers / encoders based on Protobuf reflection, using the TextFormat implementation as a guide.