2021-07-07 10:26:41 +02:00
|
|
|
#include "json.hpp"
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <filesystem>
|
|
|
|
|
|
2022-03-07 20:29:23 +01:00
|
|
|
nlohmann::ordered_json loadJsonFile(std::string path)
|
2021-07-07 10:26:41 +02:00
|
|
|
{
|
2022-03-07 20:29:23 +01:00
|
|
|
nlohmann::ordered_json j;
|
2021-07-07 10:26:41 +02:00
|
|
|
|
|
|
|
|
if (std::filesystem::exists(path))
|
|
|
|
|
{
|
|
|
|
|
std::ifstream istream(path);
|
|
|
|
|
istream >> j;
|
|
|
|
|
istream.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return j;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 20:29:23 +01:00
|
|
|
void saveJsonFile(std::string path, nlohmann::ordered_json j)
|
2021-07-07 10:26:41 +02:00
|
|
|
{
|
|
|
|
|
std::ofstream ostream(path);
|
|
|
|
|
ostream << j.dump(4);
|
|
|
|
|
ostream.close();
|
2022-03-30 15:41:23 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-22 17:00:18 +01:00
|
|
|
void saveCborFile(std::string path, nlohmann::ordered_json j)
|
|
|
|
|
{
|
|
|
|
|
std::ofstream ostream(path);
|
|
|
|
|
auto v = nlohmann::json::to_cbor(j);
|
|
|
|
|
ostream.write((char *)v.data(), v.size());
|
|
|
|
|
ostream.close();
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-30 15:41:23 +02:00
|
|
|
nlohmann::ordered_json merge_json_diffs(nlohmann::ordered_json master, nlohmann::ordered_json diff)
|
|
|
|
|
{
|
|
|
|
|
nlohmann::ordered_json output = master;
|
|
|
|
|
|
2023-09-21 23:17:26 -04:00
|
|
|
for (nlohmann::detail::iteration_proxy_value<nlohmann::detail::iter_impl<nlohmann::ordered_json>> cfg : diff.items())
|
2022-03-30 15:41:23 +02:00
|
|
|
{
|
2023-09-21 23:17:26 -04:00
|
|
|
if (master.contains(cfg.key()))
|
2022-03-30 15:41:23 +02:00
|
|
|
{
|
|
|
|
|
if (cfg.value().is_object())
|
2023-09-21 23:17:26 -04:00
|
|
|
output[cfg.key()] = merge_json_diffs(master[cfg.key()], cfg.value());
|
2022-03-30 15:41:23 +02:00
|
|
|
else
|
|
|
|
|
output[cfg.key()] = diff[cfg.key()];
|
|
|
|
|
}
|
2023-09-21 23:17:26 -04:00
|
|
|
else
|
|
|
|
|
output[cfg.key()] = cfg.value();
|
2022-03-30 15:41:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nlohmann::ordered_json perform_json_diff(nlohmann::ordered_json master, nlohmann::ordered_json modified)
|
|
|
|
|
{
|
|
|
|
|
nlohmann::ordered_json diff;
|
|
|
|
|
|
2023-09-21 23:17:26 -04:00
|
|
|
for (nlohmann::detail::iteration_proxy_value<nlohmann::detail::iter_impl<nlohmann::ordered_json>> cfg : modified.items())
|
2022-03-30 15:41:23 +02:00
|
|
|
{
|
2023-09-21 23:17:26 -04:00
|
|
|
if (master[cfg.key()] != cfg.value())
|
2022-03-30 15:41:23 +02:00
|
|
|
{
|
|
|
|
|
if (cfg.value().is_object())
|
2023-09-21 23:17:26 -04:00
|
|
|
diff[cfg.key()] = perform_json_diff(master[cfg.key()], cfg.value());
|
2022-03-30 15:41:23 +02:00
|
|
|
else
|
2023-09-21 23:17:26 -04:00
|
|
|
diff[cfg.key()] = cfg.value();
|
2022-03-30 15:41:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return diff;
|
2023-10-30 22:39:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nlohmann::ordered_json loadCborFile(std::string path)
|
|
|
|
|
{
|
|
|
|
|
// Read file
|
|
|
|
|
std::vector<uint8_t> cbor_data;
|
|
|
|
|
std::ifstream in_file(path, std::ios::binary);
|
|
|
|
|
while (!in_file.eof())
|
|
|
|
|
{
|
|
|
|
|
uint8_t b;
|
|
|
|
|
in_file.read((char *)&b, 1);
|
|
|
|
|
cbor_data.push_back(b);
|
|
|
|
|
}
|
|
|
|
|
in_file.close();
|
|
|
|
|
cbor_data.pop_back();
|
|
|
|
|
return nlohmann::json::from_cbor(cbor_data);
|
|
|
|
|
}
|