Simple RapidJSON example?
sanderfoobar opened this issue · 1 comments
sanderfoobar commented
Missing a RapidJSON example or otherwise documentation regarding its usage.
My object:
#include <iostream>
#include <string>
#include "jinja2cpp/template.h"
#include <jinja2cpp/binding/rapid_json.h>
using namespace jinja2;
[...]
const char *json = R"({
"message": "Hello World from Parser!"
})";
rapidjson::Document doc;
doc.Parse(json);
My template:
std::string source = R"(
Value: {{ json.message }}
)";
How to render the template with the RapidJson object available?
This does not work:
std::string result = tpl.RenderAsString(doc).value();
sanderfoobar commented
The answer is to wrap doc
using jinja2::Reflect()
:
#include <iostream>
#include <string>
#include "jinja2cpp/template.h"
#include <jinja2cpp/binding/rapid_json.h>
using namespace jinja2;
int main(void) {
const char *json = R"(
{
"message": "Hello World from Parser!",
"big_int": 100500100500100,
"bool_true": true
}
)";
rapidjson::Document doc;
doc.Parse(json);
std::string source = R"(
{{ json.message }}
)";
Template tpl;
tpl.Load(source);
ValuesMap params = {
{"json", Reflect(doc)},
};
std::string result = tpl.RenderAsString(params).value();
std::cout << result << std::endl;
return 0;
}