convert json to any protocol buffer java

39 views
Skip to first unread message

stephanie mulder

unread,
Sep 12, 2020, 9:39:33 AM9/12/20
to Protocol Buffers
Hi,

I want to convert any protocol buffer to json.
My protocol buffer looks as below.
```
syntax = "proto3";

message A {
   int32 id = 1;
}

message B {
   int32 id = 2;
}

message C {
   int32 id = 3;
}
```
I am converting each of them as follows using JsonFormat:
```
String jsonString;
A.Builder Abuilder = A.newBuilder();
JsonFormat.parser().ignoringUnknownFields().merge(jsonString, builder);
A a = Abuilder.build();

Similarly for B
B.Builder Bbuilder = B.newBuilder();
JsonFormat.parser().ignoringUnknownFields().merge(jsonString, builder);
B b = Bbuilder.build()
```

I wanted to know if I can write a generic method using template or java generics or using ```com.google.protobuf.Message``` so that I don't have to write the same method for each of my protocol buffer.

Thanks,
Stephanie
Reply all
Reply to author
Forward
0 new messages