I am trying out the JSON library to use with a Grafana JSON datasource project I am starting. I set up some simple structs to model the data as described at https://grafana.com/grafana/plugins/grafana-simple-json-datasource and started a simple unit test for parsing.
GIVEN("Sample query from grafana documentation")
{
const auto json = R"({
"panelId": 1,
"range": {
"from": "2016-10-31T06:33:44.866Z",
"to": "2016-10-31T12:33:44.866Z",
"raw": {
"from": "now-6h",
"to": "now"
}
},
"rangeRaw": {
"from": "now-6h",
"to": "now"
},
"interval": "30s",
"intervalMs": 30000,
"targets": [
{ "target": "upper_50", "refId": "A", "type": "timeserie" },
{ "target": "upper_75", "refId": "B", "type": "timeserie" }
],
"adhocFilters": [
"key": "City"
"operator": "=",
"value": "Berlin"
]
"format": "json",
"maxDataPoints": 550
})";
WHEN("Parsing query")
{
auto query = spt::model::Query{};
auto obj = li::json_object( s::panelId,
s::range = li::json_object( s::from, s::to, s::raw = li::json_object( s::from, s::to ) ),
s::rangeRaw = li::json_object( s::from, s::to ),
s::interval, s::intervalMs,
s::targets = li::json_vector( s::target, s::refId, s::type ),
s::adhocFilters = li::json_vector( s::key, s::value ),
s::format, s::maxDataPoints );
obj.decode( json, query );
THEN("Query structure populated")
{
REQUIRE(query.panelId == 1);
REQUIRE(query.range.from == "2016-10-31T06:33:44.866Z");
REQUIRE(query.range.to == "2016-10-31T12:33:44.866Z");
REQUIRE(query.targets.size() == 2);
REQUIRE(query.interval == "30s");
REQUIRE(query.format == "json");
REQUIRE(query.maxDataPoints == 550);
}
THEN("Serialise structure back to JSON")
{
std::cout << obj.encode( query ) << '\n';
}
}
{"panelId":1,"range":{"from":"2016-10-31T06:33:44.866Z","to":"2016-10-31T12:33:44.866Z","raw":{"from":"now-6h","to":"now"}},"rangeRaw":{"from":"","to":""},"interval":"","intervalMs":0,"targets":[],"adhocFilters":[],"format":"","maxDataPoints":0}
lithium_json.h:626:58: error: no member named 'error' in 'li::CANNOT_FIND_REQUESTED_MEMBER_IN_TYPE<s::from_t, std::__1::optional<spt::model::RangeRaw> &>'
return CANNOT_FIND_REQUESTED_MEMBER_IN_TYPE<S, T>::error;