Proto2RapidJSON
简介
本工具旨在利用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) const;
std::string ToPrettyString(int maxDecimalPlaces = 6) const;
rapidjson::Value ToValue(rapidjson::Document::AllocatorType& allocator, bool copy = false) const;
}
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) const;
std::string ToPrettyString(int maxDecimalPlaces = 6) const;
rapidjson::Value ToValue(rapidjson::Document::AllocatorType& allocator, bool copy = false) const;
}
功能如下:
| 函数签名 | 功能 |
|---|---|
A& FromString(const char* str); |
从JSON格式字符串str解析数据,写入结构体 |
A& FromValue(const rapidjson::Value& v); |
从rapidjson::Value中获取数据,写入结构体 |
std::string ToString(int maxDecimalPlaces = 6) const; |
将结构体中数据序列化为字符串(紧凑),浮点数小数部分长度为maxDecimalPlaces |
std::string ToPrettyString(int maxDecimalPlaces = 6) const; |
将结构体中数据序列化为字符串(适合阅读),浮点数小数部分长度为maxDecimalPlaces |
rapidjson::Value ToValue(rapidjson::Document::AllocatorType& allocator, bool copy = false) const; |
将结构体中数据转化为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>
Metadata
Release files for proto2rapidjson 1.1.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| proto2rapidjson-1.1.8.tar.gz | 11.0 kB | Without provenance | Details for proto2rapidjson-1.1.8.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| proto2rapidjson-1.1.8-py3-none-any.whl | Python 3 | none | any | Details for proto2rapidjson-1.1.8-py3-none-any.whl |
Total release size: 23.5 kB
Release files / proto2rapidjson-1.1.8.tar.gz
| Download URL | proto2rapidjson-1.1.8.tar.gz |
|---|---|
| Size | 11.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8e2dd057711985f680717942c0d6d90544953257288a4dd28fee98b6f9f57804
|
|
BLAKE2b-256 checksum How to use checksums |
9c7ba8fa27a97b6ea2339427adf12d8714ab73ea2542a6e83ea6810b0a9c16ff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10
|
Release files / proto2rapidjson-1.1.8-py3-none-any.whl
| Download URL | proto2rapidjson-1.1.8-py3-none-any.whl |
|---|---|
| Size | 12.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9b879eefff67ab2522af60a625dc1152f8ee1e4b24f0e333462c6f093469868a
|
|
BLAKE2b-256 checksum How to use checksums |
41492876e7d2ce3d8df5982980727a4f56aea2906a7a8d9a33d690c2a732f48f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10
|
Security for proto2rapidjson 1.1.8
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.