-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproto2json.js
33 lines (25 loc) · 1011 Bytes
/
proto2json.js
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
27
28
29
30
31
32
33
var ProtoBuf = require("protobufjs"),
fs = require("fs"),
util = require("util"),
path = require("path"),
jsonfile = require('jsonfile');
var output_folder = './json_output'
// var parser = new ProtoBuf.DotProto.Parser(fs.readFileSync(process.argv[3]));
// var ast = parser.parse();
// console.log(util.inspect(ast, false, null, true));
var builder = ProtoBuf.newBuilder({convertFieldsToCamelCase: true});
ProtoBuf.loadProtoFile(path.resolve(process.argv[3]), builder);
var message = builder.build("edu.psu.cse.siis.ic3.Application");
var buffer = fs.readFileSync(process.argv[2]);
var mymsg = message.decode(buffer);
if (!fs.existsSync(output_folder)){
fs.mkdirSync(output_folder);
}
var output = output_folder + '/' +
path.basename(process.argv[2], '.dat') + '.json'
jsonfile.writeFile(output, mymsg, {spaces: 4}, function (err) {
if (err) {
console.error(err);
}
})
//console.log(util.inspect(mymsg, showHidden=false, depth=null, colorize=true));