-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpb.js
More file actions
26 lines (23 loc) · 638 Bytes
/
pb.js
File metadata and controls
26 lines (23 loc) · 638 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// protocol buffer
var fs = require('fs')
, MESSAGE_DESC = './data.desc'
, MESSAGE_SCHEMA = 'demo.Message'
, protobuf = require('node-protobuf')
, log = require('./log')
, pb = new protobuf(fs.readFileSync(MESSAGE_DESC));
exports.serialize = function(obj){
try {
return pb.serialize(obj, MESSAGE_SCHEMA);
} catch (e) {
log.error('pb', 'serialize', JSON.stringify(obj) + ' exception: ' + e);
return null;
}
};
exports.deserialize = function(b){
try {
return pb.parse(b, MESSAGE_SCHEMA);
} catch (e) {
log.error('pb', 'deserialize', JSON.stringify(b) + ' exception: ' + e);
return null;
}
};