I have exteded data in my .proto file:
option java_outer_classname = "Foobar";
message Foo {
optional int32 i = 1;
extensions 10 to 99999;
}
message Bar {
extend Foo {
optional int32 j = 10001;
optional string name = 10002;
}
}
message Msg {
optional Foo foo = 1;
}
So, here is code which i'm using to parse data using DynamicMessage:
// Create data
Foobar.Foo foo = Foobar.Foo.newBuilder()
.setI(123)
.setExtension(Foobar.Bar.j, 456)
.setExtension(Foobar.Bar.name, "simpleName")
.build();
Foobar.Msg msg = Foobar.Msg.newBuilder().setFoo(foo).build();
byte[] msgData = msg.toByteArray();
// register extensions
ExtensionRegistry registry = ExtensionRegistry.newInstance();
Foobar.registerAllExtensions(registry);
final DescriptorProtos.FileDescriptorSet fds =
DescriptorProtos.FileDescriptorSet.parseFrom(new
FileInputStream("foobar.desc"));
Descriptors.Descriptor descriptor =
Descriptors.FileDescriptor.buildFrom(fds.getFile(0), new
Descriptors.FileDescriptor[]{})
.findMessageTypeByName("Msg");
// build Msg from data using dynamically created descriptor
DynamicMessage dynamicMessage =
DynamicMessage.parseFrom(descriptor, msgData, registry);
System.out.println("dynamically created descriptor: " +
JsonFormat.printToString(dynamicMessage));
// build Msg from data using manually created descriptor
DynamicMessage dynamicMessage1 =
DynamicMessage.parseFrom(Foobar.Msg.getDescriptor(), msgData, registry);
System.out.println("manually created descriptor: " +
JsonFormat.printToString(dynamicMessage1));
Result:
dynamically created descriptor: {"foo": {"i": 123, "10001": [456], "10002":
["simpleName"]}}
manually created descriptor: {"foo": {"i": 123,"Bar.j": 456,"Bar.name":
"simpleName"}}
So, dynamicMessage1 parsed correctly - it has fields "Bar.j" and "Bar.name"
which were parsed as extensions.
But dynamicMessage parsed incorrectly - those fields parsed as unknown
fields and their names are equal to thier numbers - "10001" and "10002".
May be problem is in getting descriptor for the "Msg" message? Is it
correct way to get the descriptor dynamicaly?
--
You received this message because you are subscribed to the Google Groups
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/groups/opt_out.