ifm3d
ifm3d::json Class Reference

#include <ifm3d/common/json.hpp>

Detailed Description

Documentation

ifm3d provides a copy of nlohmann::json, please see it's documentation for detailed usage: https://json.nlohmann.me/

Using ifm3d::json together with nlohmann::json

Working with both nlohmann::json and ifm3d::json together doesn't require any specific setup, however the types are not automatically convertible, so to get a nlohmann::json instance from a ifm3d::json instance one has to convert the json to a string and then parse it again.

ifm3d::json j1 = ...;
nlohmann::json j2 = nlohmann::json::parse(j1.dump(0));

To ease this conversion ifm3d provides a custom converter which can be used by defining IFM3D_JSON_NLOHMANN_COMPAT before including ifm3d::json.

#include <nlohmann/json.hpp> // nlohmann json needs to be included before
// any ifm3d include.
#define IFM3D_JSON_NLOHMANN_COMPAT // enable the nlohmann json converter
#include <ifm3d/common/json.hpp>

After this the values can just be assigned and will be converted automatically

ifm3d::json j1 = ...;
nlohman::json j2 = j1;

User Defined Literals

Using the user-defined string literals operator""_json and operator""_json_pointer

using namespace ifm3d::literals;
ifm3d::json j = "[1,2,3]"_json;

ambiguity errors will occur since by default nlohmann::json currently places it's operator""_json and operator""_json_pointer into the global namespace. To solve this nlohman::json can be configured to place them into the nlohmann namespace by defining

#define JSON_USE_GLOBAL_UDLS 0
#include <nlohmann/json.hpp>

before including it (see the nlohmann::json doc for more details).

After this the correct namespaces can always be brought into scope when needed, but some care needs to be given to never bring both into the same scope, e.g. the following WILL NOT WORK:

// Bring both UDLs into scope
using namespace nlohmann::json_literals;
using namespace ifm3d::json_literals;
int main()
{
// auto j = "42"_json; // This line would fail to compile,
// because _json is ambigous
std::cout << j << std::endl;
}

To counteract this, the namspaces can be brought into smaller scopes, the following will all be valid

void json_nl()
{
using namespace nlohmann::literals;
auto nl = "{}"_json;
}
void json_ifm()
{
using namespace ifm3d::literals;
auto ifm = "{}"_json;
}
void json_both()
{
nlohmann::json nl;
{
using namespace nlohmann::literals;
nl = "{}"_json;
}
{
using namespace ifm3d::literals;
ifm = "{}"_json;
}
}

The documentation for this class was generated from the following file:
ifm3d::json
Definition: json.hpp:128