Convert JSON object to protobuf IStruct

房东的猫 提交于 2020-05-28 11:52:30

问题


I am using the Dialogflow API for Nodejs.

This API is apparently implemented by Google via protocol buffers, though it presents a simple JavaScript/JSON based interface that I have used successfully for simple queries.

In order to request Dialogflow send me response data for a particular platform, I apparently need to encode a simple JSON object into protobuf format. The linked source gives an example that makes use of a "structjson util" that can presumably be used to do the necessary conversion:

const structjson = require('./structjson.js');

const request = {
    /* other properties omitted */
    queryParams: {
        payload: structjson.jsonToStructProto({source: 'ACTIONS_ON_GOOGLE'})
    },
};

Unfortunately, the link given for the structjson util is dead, and I can find no other reference to it.

I don't need to do anything other than encode a simple JSON object, as per the example above. Is there a simple utility (either runtime or command line) that can be used to do that without having to do major surgery with the protobuf toolchain?


回答1:


It seems the Google Dialogflow samples were updated to use the pb-util package for JSON-to-protobuf conversions. This package provides a method for encoding JSON to protobuf easily, so the example from the original question simply becomes:

const {struct} = require('pb-util');

const request = {
    /* other properties omitted */
    queryParams: {
        payload: struct.encode({source: 'ACTIONS_ON_GOOGLE'})
    },
};

Thanks also to @karthick for locating the original structjson file in the Dialogflow samples repo.



来源:https://stackoverflow.com/questions/59536865/convert-json-object-to-protobuf-istruct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!