money-encoder
v0.1.0
Published
Money encoder/decoder for [protocol buffers](https://developers.google.com/protocol-buffers/)
Downloads
4
Readme
money-encoder
Money encoder/decoder for protocol buffers
Example using mafintosh/protocoll-buffers
// traveller.proto
syntax = "proto2";
/*
* money isn't a real protobuf type, but protocol-buffers
* knows how to encode/decode it since we pass it the money-encoder
*/
message wallet {
optional money dollars = 0;
optional money pesos = 1;
}
message traveller {
required wallet wallet;
}
var protobuf = require('protocol-buffers')
var fs = require('fs')
var money = require('wallet-encoder')
var proto = fs.readFileSync(__dirname + '/wallet.proto')
// initialize protobuf and teach it how to encode/decode money
var messages = protobuf(proto, { encodings: { money: money } })
var traveller = {
wallet: {
dollars: 20.45
, pesos: 235.67
}
}
var buf = messages.entry.encode(traveller)
console.log(buf)
var w = messages.entry.decode(buf)
console.log(w)
Output
<Buffer 02 0a 00 fd 07 00 00 00 10 5c 00 00>
{ wallet: { dollars: 235.68, pesos: 235.67 } }
Installation
npm install money-encoder
API
generated with docme
License
MIT