https://github.com/paladin-t/jpath
An easy to use RapidJSON reader/writer in one header.
https://github.com/paladin-t/jpath
cpp json rapidjson
Last synced: 6 months ago
JSON representation
An easy to use RapidJSON reader/writer in one header.
- Host: GitHub
- URL: https://github.com/paladin-t/jpath
- Owner: paladin-t
- License: mit
- Created: 2020-03-17T10:28:08.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T06:32:37.000Z (over 3 years ago)
- Last Synced: 2025-04-06T19:48:16.918Z (12 months ago)
- Topics: cpp, json, rapidjson
- Language: C++
- Homepage:
- Size: 1.04 MB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Jpath - An easy to use RapidJSON reader/writer
Jpath can be used to read and write a RapidJSON value in an intuitive way.
### Dependency
Requires a C++ compiler with variadic template (since C++11) capability.
### Usage
* `Jpath::get(val, ref /* out */, ... /* path */)`: reads `ref` from `val` along with the specific path; fails if any intermediate node does not exist
* returns `true` for success, `false` for fail
* `Jpath::set(doc, val, ref /* in */, ... /* path */)`: writes `ref` to `val` along with the specific path; omitted intermediate nodes are generated
* returns `true` for success, `false` for fail
### Example
```cpp
#include "jpath.hpp"
int main(int argc, const char* argv[]) {
rapidjson::Document doc;
int i32 = 0;
float real = 0.0f;
const char* str = nullptr;
Jpath::set(doc, doc, 42, "hello", 0, "world");
Jpath::set(doc, doc, 22/7.0f, "hello", 0, "pi");
Jpath::get(doc, i32, "hello", 0, "world");
Jpath::get(doc, real, "hello", 0, "pi");
printf("%d\n", i32);
printf("%f\n", real);
Jpath::set(doc, doc, "test", "hello", 0, "world");
Jpath::get(doc, str, "hello", 0, "world");
printf("%s\n", str);
return 0;
}
```
The above code manipulates a JSON as:
```json
{
"hello": [
{
"world": ...,
"pi": ...
}
]
}
```