title: Serializing protocol buffers to XML?
body: <p>The protocol buffers <a href="http://code.google.com/apis/protocolbuffers/docs/javatutorial.html" rel="nofollow">Java tutorial</a> states:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>And if you look at <a href="http://code.google.com/apis/protocolbuffers/docs/reference/java/com/google/protobuf/Message.html" rel="nofollow">com.google.protobuf.Message</a> it says:</p>
  
  <blockquote>
    <p>The biggest added features [of the Message class over the MessageLite class] are introspection and reflection.</p>
  </blockquote>
  
  <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>&lt;com.x.MyProtos_-MyMessage resolves-to="com.google.protobuf.GeneratedMessageLite$SerializedForm"&gt;
      &lt;messageClassName&gt;com.x.MyProtos$MyMessage&lt;/messageClassName&gt;
      &lt;asBytes&gt;CjkKDkJXQkUwMDAzNzkzMTA3EgsZAAAAAAAA8D8gASIGEJYBGMIDKhIpuB6F61G4nj8xuB6F61G4
  Xnj8KMQoGQURBQkliEgsZAAAAAAAA8D8gASIGEJYBGMIDKhIpuB6F61G4nj8xuB6F61G4nj8qAyDQ
  Dw==&lt;/asBytes&gt;
  </code></pre>
  
  <p>[ 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 ]</p>
  
  <p>The only existing solution for serializing protocol buffers to XML (such that the result is somewhat human readable) that I found was <a href="http://code.google.com/p/protobuf-java-format/" rel="nofollow">protobuf-java-format</a>.</p>
  
  <p>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.</p>
  
  <p>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)?</p>
  
  <p>Or worked out how to take advantage of the "reflection" features of protocol buffers with one of the popular existing Java XML serialization libraries?</p>
  
  <p>Regards,</p>
  
  <p>/George</p>
  
  <p>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.</p>