0

I have this Java code (source):

// Deserialize
Person person = new Person();
InputStream in;
XmlIOUtil.mergeFrom(in, person, Person.getSchema());

// Do stuff...

// Serialize back into XML
OutputStream out;
XmlIOUtil.writeTo(out, person, Person.getSchema());

Let's say my XML code contains unknown fields, that not are in the schema (i.e. the XML provided is generated by a newer version of the software).

Is there a nice, clean way to preserve these fields when I serialize them again? I know Protocol Buffers preserves unknown fields.

One solution would be to copy the original XML into a buffer, then merge the newly serialized XML with the original, but that seems overly complicated.

Sundae
  • 724
  • 1
  • 8
  • 27

2 Answers2

0

According to a post on the Protostuff forum, preserving unknown fields seems not to be supported:

I assume the Protobuf serialization (that is part of Protostuff) is capable of handling anything the Google provided library does. If I choose to use Protostuff, are extensions & unknown fields available?

No and no. Unknown fields are ignored.

Community
  • 1
  • 1
Sundae
  • 724
  • 1
  • 8
  • 27
0

Here's a snippet of example output from the protoc compiler:

void hello_message::SerializeWithCachedSizes(
    ::google::protobuf::io::CodedOutputStream* output) const {
  // @@protoc_insertion_point(serialize_start:trustportal.crypt.rpc.hello_message)
  // required string welcome_message = 1;
  if (has_welcome_message()) {
    ::google::protobuf::internal::WireFormat::VerifyUTF8StringNamedField(
      this->welcome_message().data(), this->welcome_message().length(),
      ::google::protobuf::internal::WireFormat::SERIALIZE,
      "welcome_message");
    ::google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(
      1, this->welcome_message(), output);
  }

  if (!unknown_fields().empty()) {
    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
        unknown_fields(), output);
  }
  // @@protoc_insertion_point(serialize_end:trustportal.crypt.rpc.hello_message)
}

It seems to imply to me that unknown fields will be correctly serialised.

Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
  • Yes, the Protocol Buffers compiler preserves unknown fields, as I also noted in my question. However, **Protostuff** seems not to, to the best of my knowledge. – Sundae Oct 26 '14 at 10:07