bivas / protobuf-java-format

Provide serialization and de-serialization of different formats based on Google’s protobuf Message. Enables overriding the default (byte array) output to text based formats such as XML, JSON and HTML.
BSD 3-Clause "New" or "Revised" License
153 stars 97 forks source link

Support for havig attributes in XML formatter? #61

Open mfathi91 opened 10 months ago

mfathi91 commented 10 months ago

Hello, Thank you for the great work. I would like to know if there is (or will be) any support for having attributes in XML elements, instead of having separate XML entities for each message field.


Example: I have the following .proto file:

syntax = "proto2";

package tutorial;

option java_multiple_files = true;
option java_package = "com.example.tutorial.protos";
option java_outer_classname = "AddressBookProtos";

message Person {
  required string name = 1;
  required int32 id = 2;
  required string email = 3;

  enum PhoneType {
    PHONE_TYPE_UNSPECIFIED = 0;
    PHONE_TYPE_MOBILE = 1;
    PHONE_TYPE_HOME = 2;
    PHONE_TYPE_WORK = 3;
  }

  message PhoneNumber {
    optional string number = 1;
    optional PhoneType type = 2 [default = PHONE_TYPE_HOME];
  }

  repeated PhoneNumber phones = 4;
}

message AddressBook {
  repeated Person people = 1;
}

When I use new XmlFormat().printToString(addressBook), the output string looks like this:

<AddressBook>
  <people>
    <name>John Doe</name>
    <id>1234</id>
    <email>jdoe@example.com</email>
    <phones>
      <number>555-4321-9999</number>
      <type>PHONE_TYPE_HOME</type>
    </phones>
  </people>
</AddressBook>

However, I would like to have the possibility to have some of the fields as attributes:

<AddressBook>
  <people name="John Doe" id="1234" email="jdoe@example.com">
    <phones>
      <number>555-4321-9999</number>
      <type>PHONE_TYPE_HOME</type>
    </phones>
  </people>
</AddressBook>

The second one is arguably more readable and more concise. So, is there (or will there be) any support for it in this library? Thanks!