diff --git a/src/json.cpp b/src/json.cpp index 792ec57..0fbdffd 100644 --- a/src/json.cpp +++ b/src/json.cpp @@ -18,6 +18,7 @@ #include "fs-compat.hpp" #include "types.hpp" +#include #include namespace FediBlock::json @@ -43,4 +44,22 @@ string pull_request_body(string_view branch, const entry_type &entry) return json.dump(); } +entry_type from_json(const string_view json_string) +{ + const auto json{nlohmann::json::parse(json_string.data())}; + entry_type entry; + entry.description = json[0].at("description").get(); + entry.instance = json[0].at("instance").get(); + entry.receipts = json[0].at("receipts").get>(); + try + { + entry.screenshot_filepath = json[0].at("screenshot").get(); + } + catch (const nlohmann::detail::out_of_range &) // Ignore missing screenshot. + {} + entry.tags = json[0].at("tags").get>(); + + return entry; +} + } // namespace FediBlock::json diff --git a/src/json.hpp b/src/json.hpp index 21760b0..181ed09 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -35,6 +35,9 @@ using std::string_view; [[nodiscard]] string pull_request_body(string_view branch, const entry_type &entry); +// Convert a JSON string into an entry_type. +[[nodiscard]] entry_type from_json(string_view json_string); + } // namespace FediBlock::json #endif // FEDIBLOCK_BACKEND_JSON_HPP