Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protobuf message from JSON in javascript

In python/C#/C/Go/... generated code it is possible to create a proto message instance from JSON. E.g. in Python where you can just do google.protobuf.json_format.Parse(json, message). I would expect the [JavaScript generated code (here and here) to offer the same, but unfortunately this is not the case.

I am currently sending my JSON in a Struct and receiving the same back. So now I want to upack that Struct and get the message back. But I don't know how.

like image 868
Berco Beute Avatar asked Jul 06 '20 19:07

Berco Beute


People also ask

Does protobuf support JavaScript?

The official protobuf project support only Java, C++, and Python. Not Javascript.

Does protobuf use JSON?

JSON is limited to certain python objects, and it cannot serialize every python object. Protobuf supports a wider range of data types when compared to JSON. For example, enumerations and methods are supported by Protobuf and not supported by JSON. JSON supports only a subset of python data types.

Is protobuf faster than JSON?

Benchmark — strings In one – protobufjs was faster, and in the second — JSON was faster. Looking at the schemas, the immediate suspect was the number of strings. We ran the benchmark with this payload (10,000 strings, of length 10 each).

How do I send protobuf over HTTP?

You can certainly send even a binary payload with an HTTP request, or in an HTTP response. Just write the bytes of the protocol buffer directly into the request/response, and make sure to set the content type to "application/octet-stream". The client, and server, should be able to take care of the rest easily.


Video Answer


1 Answers

You actually have a library called protobuf.js and you can try it out with ObjectInformationType.fromObject(JSON.parse(msg)).

It parses the JSON object into protobuf.

like image 174
Stf Kolev Avatar answered Oct 17 '22 02:10

Stf Kolev