var ProtoBuf = require("protobufjs"), ByteBuffer = ProtoBuf.ByteBuffer, // ProtoBuf.js uses and also exposes ByteBuffer.js Long = ProtoBuf.Long; // as well as Long.js (not used in this example) // Option 1: Loading the .proto file directly var builder = ProtoBuf.loadProtoFile("./json.proto"), // Creates the Builder JS = builder.build("js"); // Returns just the 'js' namespace if that's all we need // Option 2: Loading the .json file generated through 'proto2js json.proto > json.json' var root = ProtoBuf.loadJsonFile("./json.json").build(), // Here we make the Builder return the root namespace JS = root.js; // then we reference 'js' inside. Both is possible. // Option 3: Loading the module generated through 'proto2js json.proto -commonjs=js > json.js' var JS = require("./json.js"); // Returns what is specified with -commonjs[=XX] (omitted=root) // `JS` now contains the js namespace from json.proto: Value, Array and Object // This is how we use these classes: /** * Converts a JSON-like structure to JS-Namespace values. * @param {*} val JSON * @returns {!JS.Value} JS-Namespace value * @inner */ function _protoify(val) { switch (typeof val) { case 'number': if (val%1 === 0 && val >= (0x80000000|0) && val <= (0x7fffffff|0)) return new JS.Value(val); // sets the first field declared in .js.Value else return new JS.Value(null, val); // sets the second field case 'string': return new JS.Value({ 'string': val }); // uses object notation instead case 'boolean': return new JS.Value({ 'boolean': val }); case 'object': if (val === null) return new JS.Value({ 'null': true }); if (Object.prototype.toString.call(val) === "[object Array]") { var arr = new JS.Array(); for (var i=0; i 2048) tempBuffer = ByteBuffer.allocate(1024); // In case this module is running inside of a daemon, we'd just call this // method every now and then to discard the tempBuffer if it becomes too // large. This is just an example on how to reuse ByteBuffers effectively. // You may consider something like this for the performance benefit, which // is decreasing the memory allocation footprint of your app. }; // Have a nice day!