src/cpp-common: add `bt2c::JsonVal` class and derived classes
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 8 Dec 2023 18:32:28 +0000 (18:32 +0000)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 4 Sep 2024 19:05:14 +0000 (15:05 -0400)
commit27e686d896e77a466b56c43c2a8efb7e6b2bdd43
tree47a12891f059b6f9442b20719706582f8f549bf6
parent3310affacd07f200476641a96beecdb44ae6c44e
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
src/Makefile.am
src/cpp-common/bt2c/json-val.cpp [new file with mode: 0644]
src/cpp-common/bt2c/json-val.hpp [new file with mode: 0644]
This page took 0.02453 seconds and 4 git commands to generate.