将JSON对象转换为protobuf IStruct [英] Convert JSON object to protobuf IStruct

查看:508
本文介绍了将JSON对象转换为protobuf IStruct的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Dialogflow用于Node.js的API



该API显然是由Google通过协议缓冲区实现的,尽管它提供了一个基于JavaScript / JSON的简单接口,我已经成功地使用了该接口简单的查询。



为了请求Dialogflow向我发送特定平台的响应数据,我显然需要将一个简单的JSON对象编码为protobuf格式。链接的源代码给出了一个示例,该示例使用了一个 structjson util,该推测可以用来进行必要的转换:

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

const请求= {
/ *省略其他属性* /
queryParams:{
负载:structjson.jsonToStructProto({source:'ACTIONS_ON_GOOGLE'})
},
};

不幸的是,为structjson util提供的链接已死,我找不到其他引用



除了上面的例子,我除了编码一个简单的JSON对象外,不需要做任何其他事情。

解决方案
解决方案

似乎Google Dialogflow示例已已更新至使用 pb-util 包进行JSON到protobuf的转换。该软件包提供了一种轻松地将JSON编码为protobuf 的方法,因此该示例来自原始问题就变成了:

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

const请求= {
/ *省略其他属性* /
queryParams:{
负载:struct.encode({source:'ACTIONS_ON_GOOGLE'})
},
};

也感谢@karthick查找原始structjson文件


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?

解决方案

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.

这篇关于将JSON对象转换为protobuf IStruct的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆