Comparing version
{ | ||
"name": "json2pb", | ||
"version": "1.1.2", | ||
"version": "1.1.4", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "author": "Randy Liu <randyliu@foxmail.com>", |
# json2pb | ||
Json2pb is a command line tool for converting json files to protobuf encoding file. | ||
Json2pb是一个可以把json对象转换为Protobuf编码的命令行工具。 | ||
如果每一个json对象都含有_message字段来说明这个对象的类型 | ||
那么把Json对象用Protobuf打包只需要输入:json2pb -pmotion.proto motion.json | ||
*想要把csv文件打包的朋友们, 可以先用[evalcsv](https://github.com/randyliu/evalcsv.git)把csv文件转换为json对象。* | ||
Install | ||
------- | ||
1. Install [node.js](https://nodejs.org) | ||
2. Install with [npm](https://npmjs.org/package/json2pb) | ||
``` | ||
npm install -g json2pb | ||
``` | ||
Usage | ||
----- | ||
Usage: json2pb [options] files | ||
Options: | ||
-h, --help Display this information. | ||
-v, --version Print the compiler version. | ||
-t, --tag The tag of storing message name, default is _message | ||
-n, --namespace Set the namespace to be built. | ||
-p, --proto Specify the .proto file for searching class name. | ||
Example | ||
------- | ||
- 每一个json对象必须含有一个_message字段来说明这个对象的类型。 | ||
motion.json: | ||
{ | ||
"_message": "motion", | ||
"rows": [ | ||
{ | ||
"motion_id": 11001, | ||
"name": "跪姿肩部下沉", | ||
"privileges": [ | ||
"owner", | ||
"admin", | ||
"user", | ||
"guest" | ||
], | ||
"type": "热身", | ||
"category": "胸", | ||
"_message": "motion_row" | ||
}, | ||
{ | ||
"motion_id": 11002, | ||
"name": "直体肩部下沉", | ||
"privileges": [ | ||
"owner", | ||
"admin", | ||
"user", | ||
"guest" | ||
], | ||
"type": "热身", | ||
"category": "胸", | ||
"_message": "motion_row" | ||
} | ||
] | ||
} | ||
motion.proto: | ||
syntax = "proto3"; | ||
message motion_row { | ||
int32 motion_id = 1; | ||
string name = 2; | ||
repeated string privileges = 3; | ||
string type = 4; | ||
string category = 5; | ||
} | ||
message motion { | ||
repeated motion_row rows = 1; | ||
} | ||
command line: | ||
json2pb -pmotion.proto motion.json | ||
Author | ||
------ | ||
[@randyliu](http://github.com/randyliu) | ||
Licence | ||
------- | ||
[See the MIT License](https://opensource.org/licenses/MIT) |
'use strict'; | ||
var mod_getopt = require('posix-getopt'), | ||
pkg = require('../package.json'), | ||
path = require('path'), | ||
jparser = require('./jparser.js'), | ||
ProtoBuf = require("protobufjs"), | ||
packer = require('./packer.js'), | ||
cofs = require('co-fs'), | ||
ejs = require('ejs'); | ||
var namespace = pkg['name']; | ||
var json2pb = module.exports = {}; | ||
var json2proto = module.exports = {}; | ||
function version() | ||
@@ -25,19 +24,17 @@ { | ||
{ | ||
console.error("Usage: %s [options] file", pkg['name']); | ||
console.error("Usage: %s [options] files", pkg['name']); | ||
console.error("Options:"); | ||
console.error(" -h, --help Display this information."); | ||
console.error(" -v, --version Print the compiler version."); | ||
console.error(" -o, --output Set the output file name."); | ||
console.error(" -a, --auto Automatically generate a title for unnamed object."); | ||
console.error(" -p, --prefix Set the class name prefix, default prefix is %s", pkg['name']); | ||
console.error(" -n, --namespace Set the namespace, default namespace is %s", namespace); | ||
console.error(" -t, --title_tag Specify the title_tag for searching class name, default title is \"_title\"."); | ||
console.error(" -t, --tag The tag of storing message name, default is %s", packer.tag); | ||
console.error(" -n, --namespace Set the namespace to be built."); | ||
console.error(" -p, --proto Specify the .proto file for searching class name."); | ||
} | ||
json2proto.main = function*(argv) { | ||
json2pb.main = function*(argv) { | ||
var parser, option; | ||
var output_file = null; | ||
var builder = ProtoBuf.newBuilder({ convertFieldsToCamelCase: false }); | ||
parser = new mod_getopt.BasicParser('v(version)h(help)t:(title)n:(namespace)p:(prefix)a(auto)o:(output)', argv); | ||
parser = new mod_getopt.BasicParser('v(version)h(help)t:(tag)n:(namespace)p:(proto)', argv); | ||
var namespace = ''; | ||
while ((option = parser.getopt()) !== undefined) { | ||
@@ -51,16 +48,10 @@ switch (option.option) { | ||
return 0; | ||
case 'p': | ||
jparser.prefix = option.optarg; | ||
break; | ||
case 'n': | ||
namespace = option.optarg; | ||
break; | ||
case 'a': | ||
jparser.auto = true; | ||
break; | ||
case 't': | ||
jparser.title_tag = option.optarg; | ||
packer.tag = option.optarg; | ||
break; | ||
case 'o': | ||
output_file = option.optarg; | ||
case 'p': | ||
ProtoBuf.loadProtoFile(option.optarg, builder); | ||
break; | ||
@@ -74,30 +65,30 @@ default: | ||
if (parser.optind() >= argv.length) { | ||
usage('missing required argument: "input"'); | ||
console.error('Missing required argument: "input"'); | ||
usage(); | ||
return 1; | ||
} | ||
packer.root = builder.build(namespace); | ||
if(packer.root == null) | ||
{ | ||
console.error('Missing --proto=[protobuf .proto file].'); | ||
usage(); | ||
return 1; | ||
} | ||
//1.推测结构体名和确定类型。 | ||
for(var i = parser.optind(); i < argv.length; ++i) { | ||
var object = yield jparser.parse(argv[i]); | ||
if(jparser.auto) { | ||
var inf = argv[i]; | ||
var outf = null; | ||
if(inf.substring(inf.length - 5, inf.length) == '.json') { | ||
//Json Object with Title | ||
outf = inf.substring(0, inf.length - 5) + '.jot'; | ||
} else { | ||
outf = inf + '.jot'; | ||
} | ||
yield cofs.writeFile(outf, JSON.stringify(object), 'utf8'); | ||
var input_file = argv[i]; | ||
var output_file = null; | ||
if (input_file.substring(input_file.length - 5, input_file.length) == '.json') { | ||
output_file = input_file.substring(0, input_file.length - 5) + '.pb'; | ||
} else { | ||
output_file = input_file + '.pb'; | ||
} | ||
} | ||
if(output_file != null) { | ||
var schema = { | ||
namespace : namespace, | ||
definitions : jparser.definitions() | ||
}; | ||
var str = yield cofs.readFile(path.join(__dirname, '../ejs', 'protobuf.ejs'), 'utf8'); | ||
var proto = ejs.render(str, { schema }); | ||
yield cofs.writeFile(output_file, proto, 'utf8'); | ||
var str = yield cofs.readFile(input_file, 'utf8'); | ||
var obj = JSON.parse(str); | ||
var pbobj = yield packer.json2pb(obj); | ||
var byteBuffer = pbobj.encode(); | ||
var buffer = byteBuffer.toBuffer(); | ||
yield cofs.writeFile(output_file, buffer); | ||
yield cofs.chmod(output_file, 438); | ||
@@ -104,0 +95,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
37894
133.12%25
150%100
3233.33%161
-54.26%1
Infinity%1
Infinity%