Hi, I have 2 simple messages defined - Person and PersonLite. PersonLite (5
fields) is actually a smaller "view" of Person (20 fields).
I was thinking of using PersonLite to read in the contents of a message that
was written by Person. It works alright by reading 5 fields, but PersonLite
also reads the remaining 15 fields into the UnknownFields map which is an
unmodifiable collection. I was hoping to read the subset of the fields and
then discard the unknown fields but alas..!!
Is there a reason why UnknownFields cannot be cleared by the user? I wanted
to serialize PersonLite from Person's data and send only those 5 fields over
the network. But now I realize that the entire 20 fields get serialized.
Proto below:
package com.javaforu.demo.protobuf;
option java_package = "com.javaforu.demo.protobuf";
option java_outer_classname = "DemoProtos";
message Person {
required string field_1 = 1;
optional string field_2 = 2;
optional string field_3 = 3;
optional string field_4 = 4;
optional string field_5 = 5;
optional string field_6 = 6;
optional string field_7 = 7;
optional string field_8 = 8;
optional string field_9 = 9;
optional string field_10 = 10;
optional string field_11 = 11;
optional string field_12 = 12;
optional string field_13 = 13;
optional string field_14 = 14;
optional string field_15 = 15;
optional string field_16 = 16;
optional string field_17 = 17;
optional string field_18 = 18;
optional string field_19 = 19;
optional string field_20 = 20;
}
message PersonLite {
required string field_1 = 1;
optional string field_5 = 5;
optional string field_10 = 10;
optional string field_15 = 15;
optional string field_20 = 20;
}
Java code:
public class TestPerson {
public static void main(String[] args) throws IOException {
Person person = Person.parseFrom(new FileInputStream(args[0]));
System.out.println("Read into " + person.getClass().getName() + "
from " + args[0]);
System.out.println(person);
System.out.println("Bytes length " + person.getSerializedSize());
System.out.println();
PersonLite personLite = PersonLite.parseFrom(new
FileInputStream(args[0]));
System.out.println("Read into " + personLite.getClass().getName() +
" from " + args[0]);
System.out.println(personLite);
System.out.println("Bytes length " +
personLite.getSerializedSize());
}
}
Ashwin.