src/cpp-common: add `bt2c::JsonVal` class and derived classes
This patch adds a simple set of classes to contain decoded JSON values.
All the new classes inherit the abstract base `JsonVal`. A `JsonVal`
instance contains its type (enumeration) as well as a text location
(`TextLoc`) which indicates where the JSON value is located within some
original JSON text.
The complete class hierarchy is:
JsonVal
JsonNullVal
JsonBoolVal
JsonSIntVal
JsonUIntVal
JsonRealVal
JsonStrVal
JsonCompoundVal (template)
ArrayJsonVal
ObjJsonVal
In reality, `JsonNullVal`, `JsonBoolVal`, `JsonSIntVal`, `JsonUIntVal`,
`JsonRealVal`, and `JsonStrVal` are aliases of `JsonScalarVal` with
specific template parameters.
Each of the classes above has its own inner `UP` alias which is the type
of a unique pointer to a specific `const` JSON value.
`json-val.hpp` also offers various createJsonVal() functions which rely
on their parameter count and types to create specific JSON values.
Here's a simple usage example using default text locations:
bt2c::JsonArrayVal::Container vals;
// Append JSON signed integer value
vals.push_back(bt2c::createJsonVal(23LL, bt2c::TextLoc {}));
// Append JSON boolean value
vals.push_back(bt2c::createJsonVal(true, bt2c::TextLoc {}));
// Append JSON null value
vals.push_back(bt2c::createJsonVal(bt2c::TextLoc {}));
// Append JSON string value
vals.push_back(bt2c::createJsonVal("salut la gang",
bt2c::TextLoc {}));
// Create JSON array value, moving `vals`
auto arrayJsonVal = bt2c::createJsonVal(std::move(vals),
bt2c::TextLoc {});
// Inspect JSON array value, printing only JSON signed int. values
for (auto& val : *arrayJsonVal) {
// Type of `val` is `const Json::UP&`
if (val->isSInt()) {
std::cout << *val->asSInt() << std::endl;
}
}
This is part of an effort to support CTF2‑SPEC‑2.0 [1]. It will be
easier to convert CTF 2 metadata stream fragments using JSON value
objects than using the listener mode version of bt2c::parseJson()
directly.
[1]: https://diamon.org/ctf/CTF2-SPEC-2.0.html
Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I35a431f881da33f516513529d1bec8bb2a905e26
Reviewed-on: https://review.lttng.org/c/babeltrace/+/7466
Reviewed-on: https://review.lttng.org/c/babeltrace/+/12683
This page took 0.02453 seconds and 4 git commands to generate.