'Create protobuf with json values

is it possible to merge or set protobuf message values with matching json values???

example json:

{id: 1, channel: 2, userIsLogged:true}

and a proto definition:

message ServerResponse {
    int32 id = 1
    Result result = 2
    int32 channel = 3
    bool userExists = 4
    bool userIsLogged =5
}

Is there a tool or proto utility to create the proto message without setting the properties individually?



Solution 1:[1]

It depends on which language you are using with Protobuf message. Protobuf has provides some toolkits to parse Proto3 message from Json.

If you are using Java, you could use JsonFormat to parse Json into Protobuf message:

JsonFormat.parser().merge(json_string, builder);

If you are using C++, you could use json_util to parse Json:

#include <google/protobuf/util/json_util.h>
int main(int argc, char** argv){
  ServerResponse sr
  // Parse the json_string into sr.
  google::protobuf::util::JsonParseOptions options;
  JsonStringToMessage(json_string, &sr, options);
}

For other languages, you could check Protobuf reference to find a related toolkit.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ramsay