Nodejs-dialogflow: Is there a clean way to convert a JSON object to a struct?

Created on 31 May 2018  路  12Comments  路  Source: googleapis/nodejs-dialogflow

Hi,

When creating a context, I'd like to pass parameters.

As seen in this sample, I need to convert my Javascript object to a Google protobuf.struct.

The way it's done into the example is by creating and using the structjson.js helper, but it's not available from the dependency and it's not an official API.

Well, I can copy this structjson.js into my project and use it - but is there any versioned npm dependency that can do that ? Or do you plan to create an official structjson API into the lib ?

Thank you

question dialogflow

Most helpful comment

Hey everyone, I've published a small utility module (pb-util) that you can use to easily encode/decode structs while we figure out a better solution that is native to the client.

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

// encode a struct
const structValue = struct.encode({foo: 'bar'});
// {
//   fields: {
//     foo: {
//       kind: 'stringValue',
//       stringValue: 'bar'
//     }
//   }
// }

// decode a struct
const json = struct.decode({
  fields: {
    foo: {
      kind: 'stringValue',
      stringValue: 'bar'
    }
  }
});
// {foo: 'bar'}

All 12 comments

Also interested 馃

+1 interested in this, I'm wanting to go the other way, protobuf.struct to javascript object.

Up

@kinwa91 any update on this?

@JustinBeckwith @alexander-fenster It seems like maybe the gapics themselves could benefit from simple encode/decode methods for google.protobuf values. Thoughts?

I wonder if protobufjs (that we use for reading proto files when loading, so it is always in node_modules) has a way of doing that (I don't know right now but would expect it to have something). I will look into this.

Hey everyone, I've published a small utility module (pb-util) that you can use to easily encode/decode structs while we figure out a better solution that is native to the client.

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

// encode a struct
const structValue = struct.encode({foo: 'bar'});
// {
//   fields: {
//     foo: {
//       kind: 'stringValue',
//       stringValue: 'bar'
//     }
//   }
// }

// decode a struct
const json = struct.decode({
  fields: {
    foo: {
      kind: 'stringValue',
      stringValue: 'bar'
    }
  }
});
// {foo: 'bar'}

@maximelebastard could you confirm that @callmehiphop's utility did the trick for you?

Hi @bcoe, this is a quite old ticket and we don't use this library anymore. But happy to ear that it was resolved for future users thanks to @callmehiphop !

@maximelebastard thanks for the quick feedback, closing the issue as an interesting historical artifact 馃槢

Why is this closed? Are you officially supporting pb-util?

@robbyemmert unfortunately I don't think this is a problem for the dialogflow client to solve. I would suggest subscribing to https://github.com/protobufjs/protobuf.js/issues/839 instead.

Was this page helpful?
0 / 5 - 0 ratings