Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Fugoes/ctrj
Compile Time RapidJSON: A compile time C++ header only JSON library without bloating yet another hand-crafted JSON parser based on RapidJSON.
https://github.com/Fugoes/ctrj
Last synced: 3 months ago
JSON representation
Compile Time RapidJSON: A compile time C++ header only JSON library without bloating yet another hand-crafted JSON parser based on RapidJSON.
- Host: GitHub
- URL: https://github.com/Fugoes/ctrj
- Owner: Fugoes
- Created: 2020-06-24T07:46:48.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-06-29T04:22:12.000Z (over 4 years ago)
- Last Synced: 2024-08-02T01:25:19.266Z (6 months ago)
- Language: C++
- Size: 91.8 KB
- Stars: 15
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# CTRJ
(C)ompile (T)ime (R)apid(J)SON.
Many people out there have written compile time JSON libraries and bloat their own JSON parsers. This is the compile time JSON library which does not bloat yet another one.
## Overview
- Header only.
- Provide reader API & writer API based on RapidJSON's writer API & SAX reader API.
- High performance. See benchmark session below.
- Type safe. Access to JSON object's properties are type-checked at compile time.
- Allocation free parser. When using the reader API, no allocation happens besides memory needed by the JSON object.## Usage
### Installation
Just copy the `ctrj/` folder to your project.
### Defining Schema
Here is an example from `test/test_citm_catalogs.cpp`:
```C++
#include "ctrj/schema.hpp"namespace {
const char _areaNames[] = "areaNames";
const char _audienceSubCategoryNames[] = "audienceSubCategoryNames";
// other keys ...
}using js_schema = ctrj::obj<
ctrj::fld<_areaNames, ctrj::dyn_obj>,
ctrj::fld<_audienceSubCategoryNames, ctrj::dyn_obj>,
ctrj::fld<_blockNames, ctrj::dyn_obj>,
ctrj::fld<_events, ctrj::dyn_obj>,
ctrj::fld<_id, ctrj::u64>,
ctrj::fld<_subTopicIds, ctrj::arr>,
ctrj::fld<_subjectCode, ctrj::nul>,
ctrj::fld<_subtitle, ctrj::nul>,
ctrj::fld<_topicIds, ctrj::arr>
>>>,
ctrj::fld<_venueNames, ctrj::dyn_obj>
>;
```All supported schema elements are:
- Primitive types:
- `ctrj::u64` for 64-bit unsigned integer,
- `ctrj::i64` for 64-bit signed integer,
- `ctrj::f64` for double,
- `ctrj::str` for string,
- `ctrj::bol` for boolean.
- Compound types (all type parameters could be another compound type):
- `ctrj::arr` for array of type `T`,
- `ctrj::nul` for nullable type of type `T`,
- `ctrj::obj, ctrj::fld, ..., ctrj::fld>` for object with `K0` property of type `T0`, `K1` property of type `T1`, ..., `Kn` property of type `Tn`,
- `ctrj::dyn_obj` for object with dynamic keys, and all these keys point to value of type `T`.### Using JSON Objects
`ctrj::val` represents a JSON object of schema `T`. After you create an instance of `ctrj::val`, your IDE would guide you. JSON types are mapped into STL types.
Here is an example for the above schema:
```C++
#include "ctrj/value.hpp"// schema definition
int main() {
ctrj::val js{};js.get<_areaNames>().flds["some field"].str = "some value";
auto &event = js.get<_events>().flds["some event"];
event.get<_description>().opt.emplace(ctrj::val{});
event.get<_subTopicIds>().vec.push_back({});
event.get<_subTopicIds>().vec[0].u64 = 0xdeadbeef;
// ...
}
```### Reading JSON
Here is an example for the above schema:
```C++
#include "ctrj/reader.hpp"int main() {
// ...
using handler_t = ctrj::reader>;
rapidjson::StringStream ss{content_c_str};
rapidjson::Reader reader{};
handler_t handler{js};
reader.Parse(ss, handler);
if (reader.HasParseError()) std::cout << "ERROR" << std::endl;
// ...
}
```### Writing JSON
Here is an example for the above schema:
```C++
#include "ctrj/writer.hpp"int main() {
// ...
rapidjson::StringBuffer buf{};
rapidjson::Writer w{buf};
ctrj::write(js).to(w);
std::cout << buf.GetString() << std::endl;
// ...
}
```## Benchmark
Firstly, build this repo using `cmake`. Use `Release` build type please. To run some benchmark:
```bash
# benchmarking RapidJSON's Document::Parse
./test_bench_rj 1000 path/to/ctrj/data/citm_catalog.json
# benchmarking CTRJ's parse & validate
./test_citm_catalogs 1000 path/to/ctrj/data/citm_catalog.json
```Usually `CTRJ` achieves about 70%~80% of the throughput of RapidJSON. Please note that, RapidJSON's benchmark does NOT include validation of schema.