libzeep

PrevUpHomeNext

The JSON element

The main data structure in this library is zeep::json::element, this is the representation of a JSON object and thus can contain various types of data. See this synopsis on how to use it.

using namespace zeep::json::literals;
using json = zeep::json::element;

json j1;

1j1["b"] = true;
j1["i"] = 1;
j1["f"] = 2.7183;
j1["s"] = "Hello, world!";
j1["ai"] = { 1, 2, 3 };
j1["n"] = nullptr;
j1["o"] = { { "b", false }, { "i", 2 } };
j1["o"]["s"] = "sub field";

std::cout << j1 << std::endl;

2json j2 = R"(
{
    "b": true,
    "i": 1,
    "f": 2.7183,
    "s": "Hello, world!",
    "ai": [ 1, 2, 3 ],
    "n": null,
    "o": {
        "b": false,
        "i": 2,
        "s": "sub field"
    }
}
)"_json;

std::cout << j2 << std::endl;

assert(j1 == j2);

1

Fill a JSON object with some data, the type is detected automatically

2

Construct a JSON object by parsing a raw string

There is also support for enums, see the following example. The idea is, you call the std::value_serializer<Enum>::init once to initialize the global mapping of enum values to strings. The name parameter is optional, but required if you use this serializer also in a SOAP controller.

enum class MyEnum { aap, noot, mies };
zeep::value_serializer<MyEnum>::init("MyEnum",
{
	{ MyEnum::aap, "aap" },
	{ MyEnum::noot, "noot" },
	{ MyEnum::mies, "mies" }
});
json j{ MyEnum::aap };
assert(j.as<std::string>() == "aap");

STL-like interface

The zeep::json::element class acts as an STL container, see the class reference for more information. But to give you an idea:

json j;

1j = zeep::json::element::array({ 1, 2, 3 });
j.push_back(4);
j.emplace_back("five");

assert(j == R"([ 1, 2, 3, 4, "five" ])"_json);

2j = zeep::json::element::object({ { "a", true }, { "b", "2" } });
j.emplace("c", 3);

assert(j == R"({ "a": true, "b": "2", "c": 3 })"_json);

1

Make j an array

2

Now make j an object, this will erase the data and initialize a new object


PrevUpHomeNext