Unfortunately I don't own the actual protocol buffer models and can't change their definitions.
What I actually want to do is this: I have a Protocol buffer model in byte[] format. I want to deserialize it back to Java objects but then replace all the ID fields in the model, including its extensions. For example, I have a "template" Employee model in byte[] format, and I want to create new Java objects using that template. After deserializing the byte array to a Java object, I want to change the UserHeader.userID and ID of all the skills in it. Is there a fluent API or a concise way available to do this? On Thursday, December 21, 2017 at 10:56:10 AM UTC+11, Feng Xiao wrote: > > 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] > <javascript:>> 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 >> JsonFormat.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] <javascript:>. >> To post to this group, send email to [email protected] >> <javascript:>. >> 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.
