1.0.0 • Published 9 years ago

json2proto v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
9 years ago

Json2proto

Json2proto is a command line tool for converting some json files to protobuf's proto file.
json2proto���԰Ѷ����洢json�������ļ���Ϊ[ProtoBuf](https://developers.google.com/protocol-buffers/)��proto�ļ���

Solution

����������һ��json������
	p0 : {
		"x" : 0,
		"y" : 1
	}

������[ProtoBuf](https://developers.google.com/protocol-buffers/)����ʾ�������Ϳ����������ģ�
	message Position1 {
	  optional int8 x = 1;
	  optional int8 y = 2;
	}
��Ȼ������������Ҳ���ܴ洢����������, ֻ�Ƕ��˸������ֶ�, �ַ����ͷ�Χ����һЩ���ѡ�
	message Position2 {
	  optional int32 x = 1;
	  optional int32 y = 2;
	  optional int32 z = 3;
	}

����ͬʱҪ�洢p0��������p1����
	p1 : {
		x : 0,
		y : 1,
		z : 2.5
	}
��ô��С(�����ֶΣ� ��С��Χ������)�Ŀ���ͬʱ��������������������Ӧ���������ģ�
	message Position3 {
		optional uint8 x = 1;
		optional uint8 y = 2;
		optional float z = 3;
	}


json2protoͨ�������е������ֶ�_title�� ��ȷ����Щ��������ͬһ�����ͣ� ��������С����������Щ���������͡�
����û��_title�ֶεĶ����� json2proto����ͨ��-a�������Զ����շ���·������_title��

Install

  1. Install node.js

  2. Install with npm

    npm install -g json2proto

Usage

	Usage: json2proto [options] file
	Options:
	  -h, --help                   Display this information.
	  -v, --version                Print the compiler version.
	  -o, --output                 Set the output file name.
	  -a, --auto                   Automatically generate a title for unnamed object.
	  -p, --prefix                 Set the class name prefix, default prefix is json2proto
	  -n, --namespace              Set the namespace, default namespace is json2proto
	  -t, --title_tag              Specify the title_tag for searching class name, default title is "_title".

Example

motion.json:
	{
			"rows": [
			{
					"motion_id": 11001,
							"name": "���˼粿�³�",
							"privileges": [
									"owner",
							"admin",
							"user",
							"guest"
									],
							"type": "����",
							"category": "��"
			},
			{
					"motion_id": 11002,
					"name": "ֱ���粿�³�",
					"privileges": [
							"owner",
					"admin",
					"user",
					"guest"
							],
					"type": "����",
					"category": "��"
			}
			]
	}

command line:
	json2pb -omotion.proto -a motion.json

motion.proto:    
	syntax = "proto3";
	package json2proto ;

	message json2proto_motion {
		repeated json2proto_motion_rows rows = 1;
	}

	message json2proto_motion_rows {
				 int32 motion_id = 1;
				 string name = 2;
		repeated string privileges = 3;
				 string type = 4;
				 string category = 5;
	}


����ԭʼjson�ļ��в�������_title�ֶΣ� ������-a�������Զ�������
motion.jot
{
		"rows": [
			{
				"motion_id": 11001,
				"name": "���˼粿�³�",
				"privileges": [
					"owner",
					"admin",
					"user",
					"guest"
				],
				"type": "����",
				"category": "��",
				"_title": "json2proto_motion_rows"
			},
			{
				"motion_id": 11002,
				"name": "ֱ���粿�³�",
				"privileges": [
					"owner",
					"admin",
					"user",
					"guest"
				],
				"type": "����",
				"category": "��",
				"_title": "json2proto_motion_rows"
			}
		],
		"_title": "json2proto_motion"
	}

Author

@randyliu

Licence

See the MIT License