Utilities for working with common protobuf types.
$ npm i --save pb-util
Encodes a JSON value into a google.protobuf.Value
.
Type: string
number
boolean
null
object
array
const {value} = require('pb-util');
const stringValue = value.encode('hello!');
// => {
// kind: 'stringValue',
// stringValue: 'hello!'
// }
Decodes a google.protobuf.Value
into a JSON value.
Type: google.protobuf.Value
const {value} = require('pb-util');
const str = value.decode({
kind: 'stringValue',
stringValue: 'beep boop'
});
// => 'beep boop'
Encodes a JSON object into a google.protobuf.Struct
.
Type: object
const {struct} = require('pb-util');
const structValue = struct.encode({foo: 'bar'});
// => {
// fields: {
// foo: {
// kind: 'stringValue',
// stringValue: 'bar'
// }
// }
// }
Decodes a google.protobuf.Struct
into a JSON object.
Type: google.protobuf.ListValue
const {struct} = require('pb-util');
const obj = struct.decode({
fields: {
foo: {
kind: 'stringValue',
stringValue: 'bar'
},
yes: {
kind: 'boolValue',
boolValue: true
}
}
});
// => {
// foo: 'bar',
// yes: true
// }
Encodes an array of JSON values into a google.protobuf.ListValue
.
Type: array
const {list} = require('pb-util');
const listValue = list.encode(['foo', 'bar']);
// => {
// values: [
// {
// kind: 'stringValue',
// stringValue: 'foo'
// },
// {
// kind: 'stringValue',
// stringValue: 'bar'
// }
// ]
// }
Decodes a google.protobuf.ListValue
into an array of JSON values.
Type: google.protobuf.ListValue
const {list} = require('pb-util');
const arr = list.decode({
values: [
{
kind: 'stringValue',
stringValue: 'foo'
},
{
kind: 'numberValue',
numberValue: 10
}
]
});
// => ['foo', 10]
ISC