3
votes

I'm researching the possibility of serializing data via Google Protocol Buffers on a C++ application, sending the data to a JavaScript application, and deserializing the data for use by the JavaScript application. However, with no "ParseFromString()" function capability I'm not sure how this can be done and can't find any clear answer or example. How can this be done?

I'm using google-protobuf and proto3. I haven't tried much... truth is that I don't know where to even start. I would like to provide a MCVE but don't know how to with what I'm trying to achieve.

I've looked at "deserializeBinary" but I lost typing (deserializeBinary() required an "object" type param) when parsing the data from the received message. I'm using binary-parser for parsing the header from the serialized protocol buffer data.

// var msg == full received message from c++ server 
// which consists of 32 bit header
var parser = new Parser()
  .uint16("header_val_1")
  .uint16("header_val_2")
  .string("msg_payload", {
    zeroTerminated: true
  });
var msgObj = parser.parse(msg);
var payloadData = msgObj.msg_payload;
var newData = ProtoMsg.SpecialData.deserializeBinary(payloadData);
// throws type error (expects 'object')

I'd expect newData to be populated with the expected SpecialData message type so that it can checked for specific data points.

newData.getLength()

However that I can't get past the error and even then I'm not sure if I'm doing this right.

1

1 Answers

0
votes

What you're doing can work. However, you should consider what data types are available in javascript. There are no 64 bit integers available. There are some third party javascript libraries available like jsbn for arbitrary precision numbers that could be used. binary-parser is a good choice but it cannot handle all the types required.

You'd have to fill in the javascript column for this table and with javascript it will be tricky to cover everything.

Secondly, writing a parser is no simple task, you'll have to follow all the rules in the encoding spec. It could be a lot of work.

Protocol buffers are used primarily for performance, when compared to other options like json. But in the world of javascript, I suspect the performance will not be stellar, and you could have been better off using json which fits well with javascript.

In any case, there actually exists protobuf support for js, it's not one of their primary supported languages but it's available. There is also another option. If you're still looking at writing your own, you could see how those implementations handled the challenges I've mentioned and other challenges that arise.