// Copyright (C) 2024 Simon Quigley // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . #include "distribution.h" #include #include distribution::distribution() : name(""), description(""), displayname(""), title(""), summary(""), domain_name(""), lp(nullptr) {} distribution::~distribution() {} std::optional distribution::parse(const std::string& json_data) { distribution d; d.parse_json(json_data); return d; } void distribution::parse_json(const std::string& json_data) { try { auto data = nlohmann::json::parse(json_data); // Map JSON keys to lambda functions std::map> json_map = { {"name", [this](const nlohmann::json& val) { name = val.get(); }}, {"description", [this](const nlohmann::json& val) { description = val.get(); }}, {"displayname", [this](const nlohmann::json& val) { displayname = val.get(); }}, {"title", [this](const nlohmann::json& val) { title = val.get(); }}, {"summary", [this](const nlohmann::json& val) { summary = val.get(); }}, {"domain_name", [this](const nlohmann::json& val) { domain_name = val.get(); }}, {"self_link", [this](const nlohmann::json& val) { self_link = val.get(); }}, {"web_link", [this](const nlohmann::json& val) { web_link = val.get(); }}, {"translationgroup_link", [this](const nlohmann::json& val) { translationgroup_link = val.get(); }} }; // Process JSON keys dynamically for (auto& [key, value] : data.items()) { try { if (json_map.find(key) != json_map.end()) { json_map[key](value); json_map.erase(key); } } catch (...) { continue; } } } catch (const nlohmann::json::parse_error& e) { std::cerr << "JSON parse error in distribution::parse_json: " << e.what() << std::endl; throw; } } void distribution::set_lp(launchpad* lp_ptr) { lp = lp_ptr; }