The protocol buffers Java tutorial states:
One key feature provided by protocol message classes is reflection. [...] One very useful way to use reflection is for converting protocol messages to and from other encodings, such as XML or JSON.
And if you look at com.google.protobuf.Message it says:
The biggest added features [of the Message class over the MessageLite class] are introspection and reflection.
This would seem to suggest that protocol buffers are ready for use with the many existing Java reflection based serialization libraries, but actually I don't think they mean reflection in the traditional Java sense of the word.
E.g. if I attempt to serialize one of my protocol buffer messages with XStream (a popular library for serializing Java objects to XML using reflection), I get:
<com.x.MyProtos_-MyMessage resolves-to="com.google.protobuf.GeneratedMessageLite$SerializedForm">
<messageClassName>com.x.MyProtos$MyMessage</messageClassName>
<asBytes>CjkKDkJXQkUwMDAzNzkzMTA3EgsZAAAAAAAA8D8gASIGEJYBGMIDKhIpuB6F61G4nj8xuB6F61G4
Xnj8KMQoGQURBQkliEgsZAAAAAAAA8D8gASIGEJYBGMIDKhIpuB6F61G4nj8xuB6F61G4nj8qAyDQ
Dw==</asBytes>
[ I notice the XML mentions GeneratedMessageLite, i.e. a subclass of MessageLite, even though the instance that was serialized was an instanceof com.google.protobuf.Message ]
The only existing solution for serializing protocol buffers to XML (such that the result is somewhat human readable) that I found was protobuf-java-format.
This produces nice output - however as it doesn't output the tag values, i.e. the numeric ids of the fields. So it doesn't look like the resulting XML would be very robust when it comes to deserialization, i.e. as far as I understand, with the standard protocol buffer serialization things will continue working fine if you change the field names, but keep the tag values the same, and then try to deserialize a byte sequence that was serialized before these changes.
Has anyone come across an XML serialization solution that keeps most of the attractive features of protocol buffers (with just the penalty of slower serialization time and larger resulting messages)?
Or worked out how to take advantage of the "reflection" features of protocol b开发者_如何转开发uffers with one of the popular existing Java XML serialization libraries?
Regards,
/George
PS if you're wondering why I want to serialize to XML it's because I want a cheap (in programming terms) way to manually edit messages under certain circumstances.
Protostuff is another Protobuf-compatible library that supports serializing to JSON in "numeric" mode.
http://code.google.com/p/protostuff/wiki/JsonSerialization
Given your PS message, I'd go a different route than trying to serialize in to/from XML. puke I'd find the Java equivalent of the following.
I'd look at the google/protobuf/io/printer.h to generate UTF-8, human readable objects, and then would use the google/protobuf/io/tokenizer.h interface to read back in the manipulated protobuf object. Much easier and Protobuf does almost all of the work for you.
精彩评论