/** * Import parameters from given json object. * Throw std::runtime_error if given json is malformated. */ inline void loadJSON(const nlohmann::json& j) { //Empty case if (j.is_null()) { return; } //Check json type if (!j.is_object()) { throw std::runtime_error( "ParametersContainer load parameters json not object"); } //Iterate on json entries for (nlohmann::json::const_iterator it=j.begin();it!=j.end();it++) { if (it.value().is_boolean()) { //Boolean if (_paramsBool.count(it.key()) == 0) { throw std::runtime_error( "ParametersContainer load parameters json bool does not exist: " + it.key()); } else { paramBool(it.key()).value = it.value(); } } else if (it.value().is_number()) { //Number if (_paramsNumber.count(it.key()) == 0) { throw std::runtime_error( "ParametersContainer load parameters json number does not exist: " + it.key()); } else { paramNumber(it.key()).value = it.value(); } } else if (it.value().is_string()) { //String if (_paramsStr.count(it.key()) == 0) { throw std::runtime_error( "ParametersContainer load parameters json str does not exist: " + it.key()); } else { paramStr(it.key()).value = it.value(); } } else { throw std::runtime_error( "ParametersContainer load parameters json malformated"); } } }
to_if_held_down(const nlohmann::json& json) : dispatcher_client(), current_held_down_id_(0) { try { if (json.is_object()) { to_ = std::vector<to_event_definition>{ json.get<to_event_definition>(), }; } else if (json.is_array()) { to_ = json.get<std::vector<to_event_definition>>(); } else { throw pqrs::json::unmarshal_error(fmt::format("json must be object or array, but is `{0}`", json.dump())); } } catch (...) { detach_from_dispatcher(); throw; } }
static entry make_from_json(const nlohmann::json& json) { entry result(device_id(0), event_time_stamp(absolute_time_point(0)), event(), event_type::key_down, event()); if (json.is_object()) { if (auto v = pqrs::json::find<uint32_t>(json, "device_id")) { result.device_id_ = device_id(*v); } if (auto v = pqrs::json::find_json(json, "event_time_stamp")) { result.event_time_stamp_ = event_time_stamp::make_from_json(v->value()); } if (auto v = pqrs::json::find<bool>(json, "valid")) { result.valid_ = *v; } if (auto v = pqrs::json::find<bool>(json, "lazy")) { result.lazy_ = *v; } if (auto v = pqrs::json::find_json(json, "event")) { result.event_ = event::make_from_json(v->value()); } if (auto v = pqrs::json::find_json(json, "event_type")) { result.event_type_ = v->value().get<event_type>(); } if (auto v = pqrs::json::find_json(json, "original_event")) { result.original_event_ = event::make_from_json(v->value()); } } return result; }
bool Contains(const nlohmann::json& json, const std::string& member) { return json.is_object() && json.find(member) != json.end(); }