Add json::from_json().
This commit is contained in:
parent
96b8967f24
commit
b91cba19d8
19
src/json.cpp
19
src/json.cpp
|
@ -18,6 +18,7 @@
|
|||
#include "fs-compat.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
#include <nlohmann/detail/exceptions.hpp>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
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<string>();
|
||||
entry.instance = json[0].at("instance").get<string>();
|
||||
entry.receipts = json[0].at("receipts").get<vector<string>>();
|
||||
try
|
||||
{
|
||||
entry.screenshot_filepath = json[0].at("screenshot").get<string>();
|
||||
}
|
||||
catch (const nlohmann::detail::out_of_range &) // Ignore missing screenshot.
|
||||
{}
|
||||
entry.tags = json[0].at("tags").get<vector<string>>();
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
} // namespace FediBlock::json
|
||||
|
|
|
@ -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
|
||||
|
|
Reference in New Issue