Skip to main content

Convert .proto file to header-only RapidJSON based c++ code

Project description

Proto2RapidJSON

Python package codecov

简介

本工具旨在利用proto文件生成基于RapidJSON的JSON文件解析与序列化的C++工具。

  • 本工具只会支持最基本的类proto语法,但保证其强类型属性;
  • 本工具生成的C++代码只依赖RapidJSON与C++11 STL;
  • 本工具支持//注释。

基本格式

package test;

message B {
  bool isok;
}

message A {
  double x;
  int32 y;
  repeated B b;
}

对应的JSON文件(message A对应的)为

{
    "x": 1.24,
    "y": 123,
    "b": [
        {
            "isok": true
        },
        {
            "isok": false
        }
    ]
}

注:暂不允许可选属性,但允许额外的属性(不会被解析)。

生成的C++文件应该包含结构体与接口如下

struct B {
    bool isok;
    B& FromString(const char* str);
    B& FromValue(const rapidjson::Value& v);
    std::string ToString();
    std::string ToPrettyString();
    rapidjson::Value ToValue(rapidjson::Document::AllocatorType& allocator);
}

struct A {
    double x;
    int y;
    std::vector<B> b;
    A& FromString(const char* str);
    A& FromValue(const rapidjson::Value& v);
    std::string ToString();
    std::string ToPrettyString();
    rapidjson::Value ToValue(rapidjson::Document::AllocatorType& allocator);
}

保留字

message, package, {}, ;, repeated, double, int32, int64, uint32, uint64, float, bool, string, //

语法

Program -> Package Message*
Package -> "package" id ";"
Message -> "message" id "{" Element* "}"
Element -> Type id ("=" num) ";" | "repeated" Type id ("=" num) ;"
Type -> id | "double" | "float" | "int32" | "uint32" | "int64" | "uint64" | "bool" | "string"

注1:("=" num)仅用于兼容proto文件格式,不起任何作用

注2:id允许包含数字与下划线,且不允许数字开头

使用

在完成安装pip install .后,可以使用如下指令执行:

python -m proto2rapidjson -i <INPUT> -o <OUTPUT>

计划

  • 添加序列化的模板
  • 编写CLI,生成二进制文件并支持pip install .
  • 完善测试与代码覆盖率测试
  • 禁止相同id

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

proto2rapidjson-1.0.0.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

proto2rapidjson-1.0.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file proto2rapidjson-1.0.0.tar.gz.

File metadata

  • Download URL: proto2rapidjson-1.0.0.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.23.0 setuptools/46.4.0.post20200518 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.3

File hashes

Hashes for proto2rapidjson-1.0.0.tar.gz
Algorithm Hash digest
SHA256 11d3ec18511c3fb3f04150026e2a8d05ba976001ff9afee5f4eb3ed2f783bf07
MD5 34f5bc0683f4daa0cfeb6ed6fe1fef31
BLAKE2b-256 7f3162bc0034bd60632ce52810de8fae5226be5f89412ddae5212d3b97aee1fe

See more details on using hashes here.

File details

Details for the file proto2rapidjson-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: proto2rapidjson-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.23.0 setuptools/46.4.0.post20200518 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.3

File hashes

Hashes for proto2rapidjson-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ef14e1d234d8539abf93f3384e42b04d446a1bef335fe1dc1b1699063b088660
MD5 53f4123e6357220c28c50c8e5213f111
BLAKE2b-256 408595b7b3408ba5ca89e7f342ca0da225c95fe66cd09989410137d02af0a030

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page