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,如果需要使用nested namespace则需要C++20的支持;
  • 本工具支持//注释。

本工具使用MIT协议。

基本格式

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(int maxDecimalPlaces = 6);
    std::string ToPrettyString(int maxDecimalPlaces = 6);
    rapidjson::Value ToValue(rapidjson::Document::AllocatorType& allocator, bool copy = false);
}

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

功能如下:

函数签名 功能
A& FromString(const char* str); 从JSON格式字符串str解析数据,写入结构体
A& FromValue(const rapidjson::Value& v); rapidjson::Value中获取数据,写入结构体
std::string ToString(int maxDecimalPlaces = 6); 将结构体中数据序列化为字符串(紧凑),浮点数小数部分长度为maxDecimalPlaces
std::string ToPrettyString(int maxDecimalPlaces = 6); 将结构体中数据序列化为字符串(适合阅读),浮点数小数部分长度为maxDecimalPlaces
rapidjson::Value ToValue(rapidjson::Document::AllocatorType& allocator, bool copy = false); 将结构体中数据转化为rapidjson::Value,对于array<string>copy用于控制是否采用copy-string策略(默认为const-string策略)注:这里可能有未发现的潜在问题

保留字

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 proto2rapidjson

使用

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

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

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.1.6.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

proto2rapidjson-1.1.6-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: proto2rapidjson-1.1.6.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.8

File hashes

Hashes for proto2rapidjson-1.1.6.tar.gz
Algorithm Hash digest
SHA256 3b03aca41284fd0437c164856055950f0c09d4ed0f7935c600e76bb7004c3ec8
MD5 2eff8d557eac425b8d223f092b4d789b
BLAKE2b-256 c80344494c816d255e01447ab385b624a252426a1d76ebaa20544a5d20599841

See more details on using hashes here.

File details

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

File metadata

  • Download URL: proto2rapidjson-1.1.6-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.8

File hashes

Hashes for proto2rapidjson-1.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 4c614207dfe7dcdfea6cd17928ad2ebcda1d4b2d25ea10b866156572eb8ffeca
MD5 322b08958c95794f02087a64bc7f36c2
BLAKE2b-256 7c17d61d0e34346ab80d7976ab2a1830aff1d38fa53e8555d3079b5c7622f9db

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