9 #include <nlohmann/json.hpp>
11namespace display_device {
13 template<
typename Type>
14 std::string toJsonHelper(
const Type &obj,
const std::optional<unsigned int> &indent,
bool *success) {
20 nlohmann::json json_obj = obj;
21 return json_obj.dump(
static_cast<int>(indent.value_or(-1)));
22 }
catch (
const std::exception &err) {
32 template<
typename Type>
33 bool fromJsonHelper(
const std::string &
string, Type &obj, std::string *error_message =
nullptr) {
36 error_message->clear();
39 Type parsed_obj = nlohmann::json::parse(
string);
40 obj = std::move(parsed_obj);
42 }
catch (
const std::exception &err) {
44 *error_message = err.what();
51 #define DD_JSON_DEFINE_CONVERTER(Type) \
52 std::string toJson(const Type &obj, const std::optional<unsigned int> &indent, bool *success) { \
53 return toJsonHelper(obj, indent, success); \
55 bool fromJson(const std::string &string, Type &obj, std::string *error_message) { \
56 return fromJsonHelper<Type>(string, obj, error_message); \