1 #include "lib/json.hpp"
2 
combine(const nlohmann::json & item1,const nlohmann::json & item2)3 nlohmann::json lib::json::combine(const nlohmann::json &item1, const nlohmann::json &item2)
4 {
5 	auto item = nlohmann::json::array();
6 
7 	if (item1.is_array())
8 		item.insert(item.end(), item1.begin(), item1.end());
9 	if (item2.is_array())
10 		item.insert(item.end(), item2.begin(), item2.end());
11 
12 	return item;
13 }
14 
load(const ghc::filesystem::path & path)15 nlohmann::json lib::json::load(const ghc::filesystem::path &path)
16 {
17 	std::ifstream file(path);
18 	if (!file.is_open() || file.bad())
19 	{
20 		// File not found errors fail silently
21 		return nlohmann::json();
22 	}
23 
24 	try
25 	{
26 		nlohmann::json json;
27 		file >> json;
28 		return json;
29 	}
30 	catch (const std::exception &e)
31 	{
32 		log::warn("Failed to load items from \"{}\": {}",
33 			path.string(), e.what());
34 	}
35 
36 	return nlohmann::json();
37 }
38 
save(const ghc::filesystem::path & path,const nlohmann::json & json)39 void lib::json::save(const ghc::filesystem::path &path, const nlohmann::json &json)
40 {
41 	try
42 	{
43 		std::ofstream file(path);
44 		file << std::setw(4) << json;
45 	}
46 	catch (const std::exception &e)
47 	{
48 		log::warn("Failed to save items to \"{}\": {}",
49 			path.string(), e.what());
50 	}
51 }
52