JsonFormat doesn't support extensions. You can replace extensions with
google.protobuf.Any if you want to use the proto with JsonFormat.

On Wed, Dec 20, 2017 at 3:39 PM, Behrang Saeedzadeh <[email protected]>
wrote:

>
>
> *down vote**favorite*
> <https://stackoverflow.com/questions/47903567/how-to-fix-com-google-protobuf-invalidprotocolbufferexception-cannot-find-fiel#>
>
> *Cross-post from
> StackOverflow: 
> https://stackoverflow.com/questions/47903567/how-to-fix-com-google-protobuf-invalidprotocolbufferexception-cannot-find-fiel
> <https://stackoverflow.com/questions/47903567/how-to-fix-com-google-protobuf-invalidprotocolbufferexception-cannot-find-fiel>*
>
> I have 2 Protobuf models:
> User:
>
> package demo;
>
> option java_package = "com.stackoverflow.question";
> option java_outer_classname = "UserModel";
>
> message User {
>
>     message UserHeader {
>         required int64 userId = 1;
>     }
>
>     required UserHeader header = 1;
>
>     extensions 100 to 200;}
>
> Employee:
>
> import "person.proto";
> package demo;
>
> option java_package = "com.stackoverflow.question";
> option java_outer_classname = "EmployeeModel";
>
> extend demo.User {
>     optional EmployeeDetails details = 101;}
>
> message EmployeeDetails {
>     required string department = 1;
>     repeated Skill skills = 2;}
>
> message Skill {
>     required int64 id = 1;
>     required string name = 2;}
>
> I can create a model and serialize it to JSON using JsonFormat.printer().
> print(...):
>
> ExtensionRegistry registry = 
> ExtensionRegistry.newInstance();EmployeeModel.registerAllExtensions(registry);
> UserModel.User.Builder userBuilder = UserModel.User.newBuilder();
> userBuilder.setHeader(UserModel.User.UserHeader.newBuilder().setUserId(1000));
> EmployeeModel.EmployeeDetails.Builder employeeBuilder = 
> EmployeeModel.EmployeeDetails.newBuilder();
> employeeBuilder.setDepartment("Department 1")
>                .addSkills(EmployeeModel.Skill.newBuilder()
>                                              .setId(10_000)
>                                              .setName("Skill 10_0000")
>                                              .build())
>                .addSkills(EmployeeModel.Skill.newBuilder()
>                                              .setId(11_000)
>                                              .setName("Skill 11_0000")
>                                              .build());
>
> userBuilder.setExtension(EmployeeModel.details, employeeBuilder.build());
> final String json = JsonFormat.printer().print(userBuilder.build());
>
> However deserializing the generated JSON back to Java objects fails with
> com.google.protobuf.InvalidProtocolBufferException: Cannot find field:
> details in message demo.User:
>
> UserModel.User.Builder userBuilder2 = UserModel.User.newBuilder();
> JsonFormat.parser().merge(json, userBuilder2);
>
> And there doesn't seem to be a way to pass an ExtensionRegistry to JsonFo
> rmat.parser()either.
>
> Is there a way to make this *Protobuf → JSON → Protobuf* 
> serialization/deserialization
> chain work?
>
> --
> 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 https://groups.google.com/group/protobuf.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to