9 #include <nlohmann/json.hpp>
12namespace display_device {
14 template<
typename Type>
15 std::string toJsonHelper(
const Type &obj,
const std::optional<unsigned int> &indent,
bool *success) {
21 nlohmann::json json_obj = obj;
22 return json_obj.dump(
static_cast<int>(indent.value_or(-1)));
23 }
catch (
const nlohmann::json::exception &err) {
29 }
catch (
const std::out_of_range &err) {
35 }
catch (
const std::invalid_argument &err) {
45 template<
typename Type>
46 bool fromJsonHelper(
const std::string &
string, Type &obj, std::string *error_message =
nullptr) {
49 error_message->clear();
52 Type parsed_obj = nlohmann::json::parse(
string);
53 obj = std::move(parsed_obj);
55 }
catch (
const nlohmann::json::exception &err) {
57 *error_message = err.what();
61 }
catch (
const std::out_of_range &err) {
63 *error_message = err.what();
67 }
catch (
const std::invalid_argument &err) {
69 *error_message = err.what();
76 #define DD_JSON_DEFINE_CONVERTER(Type) \
77 std::string toJson(const Type &obj, const std::optional<unsigned int> &indent, bool *success) { \
78 return toJsonHelper(obj, indent, success); \
80 bool fromJson(const std::string &string, Type &obj, std::string *error_message) { \
81 return fromJsonHelper<Type>(string, obj, error_message); \