I've been trying to generate protocol buffer messages from a json string using gson. Does anyone know how if it is possible to do it?
I have already tried:
Gson gson = new Gson();
Type type = new TypeToken<List<PROTOBUFFMESSAGE.Builder>>() {}.getType();
List<PROTOBUFFMESSAGE.Builder> list = (List<PROTOBUFFMESSAGE.Builder>) gson.fromJson(aJsonString, type);and
Gson gson = new Gson();
Type type = new TypeToken<List<PROTOBUFFMESSAGE>>() {}.getType();
List<PROTOBUFFMESSAGE> list = (List<PROTOBUFFMESSAGE>) gson.fromJson(aJsonString, type);The message inside the json uses the same names as in the protocol buffer i.e:
message PROTOBUFFMESSAGE {
optional string this_is_a_message = 1;
repeated string this_is_a_list = 2;
}will lead to a json:
[
{
"this_is_a_message": "abc",
"this_is_a_list": [
"123",
"qwe"
]
},
{
"this_is_a_message": "aaaa",
"this_is_a_list": [
"foo",
"bar"
]
}
]Although a list with the correct number of PROTOBUFFMESSAGE gets generated, they contain all their fields to null, so I'm not sure if this is a problem with the mapping, the reflection system not detecting protobuffs fields or something else. If anyone know how to do this it would be great. Btw I'm talking about java here.
changing the names in the json to:
{
"thisIsAMessage_": "abc",
"thisIsAList_": [
"123",
"qwe"
]
}Makes the de serialization happen. And it does work except for the list that throws:
java.lang.IllegalArgumentException: Can not set com.google.protobuf.LazyStringList