Jpath can be used to read and write a RapidJSON value in an intuitive way.
Requires a C++ compiler with variadic template (since C++11) capability.
Jpath::get(val, ref /* out */, ... /* path */)
: readsref
fromval
along with the specific path; fails if any intermediate node does not exist- returns
true
for success,false
for fail
- returns
Jpath::set(doc, val, ref /* in */, ... /* path */)
: writesref
toval
along with the specific path; omitted intermediate nodes are generated- returns
true
for success,false
for fail
- returns
#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:
{
"hello": [
{
"world": ...,
"pi": ...
}
]
}