21#include "third_party/nlohmann/json.hpp"
46 std::ifstream
file(filepath);
49 std::cerr <<
"JsonHelper: Failed to open file: " << filepath << std::endl;
53 std::string
jsonStr((std::istreambuf_iterator<char>(
file)), std::istreambuf_iterator<char>());
59 catch (
const std::exception&
e)
61 std::cerr <<
"JsonHelper: Error loading JSON from " << filepath <<
": " <<
e.what() << std::endl;
77 std::ofstream
file(filepath);
80 std::cerr <<
"JsonHelper: Failed to open file for writing: " << filepath << std::endl;
88 catch (
const std::exception&
e)
90 std::cerr <<
"JsonHelper: Error saving JSON to " << filepath <<
": " <<
e.what() << std::endl;
106inline std::string
GetString(
const json&
j,
const std::string&
key,
const std::string& defaultValue =
"")
108 if (
j.contains(
key) &&
j[
key].is_string())
109 return j[
key].get<std::string>();
122 if (
j.contains(
key) &&
j[
key].is_number())
123 return j[
key].get<
int>();
136 if (
j.contains(
key) &&
j[
key].is_number())
138 int value =
j[
key].get<
int>();
142 return static_cast<uint32_t>(value);
156 if (
j.contains(
key) &&
j[
key].is_number())
157 return static_cast<float>(
j[
key].get<
double>());
170 if (
j.contains(
key) &&
j[
key].is_number())
171 return j[
key].get<
double>();
184 if (
j.contains(
key) &&
j[
key].is_boolean())
185 return j[
key].get<
bool>();
206 if (
j.contains(
key) && !
j[
key].is_null())
208 return j[
key].get<
T>();
211 catch (
const std::exception&)
230 return j.contains(
key) &&
j[
key].is_array();
241 return j.contains(
key) &&
j[
key].is_object();
253 return j[
key].size();
268 for (
size_t i = 0;
i <
arr.size(); ++
i)
366 if (!
j.contains(
key))
368 std::cerr <<
"JsonHelper: Missing required key: " <<
key << std::endl;
387 if (!
j.contains(
key))
389 std::cerr <<
"JsonHelper: Missing required key '" <<
key <<
"'";
390 if (!context.empty())
391 std::cerr <<
" in " << context;
392 std::cerr << std::endl;
ComponentTypeID GetComponentTypeID_Static()
std::string GetString(const json &j, const std::string &key, const std::string &defaultValue="")
Safely get a string value from JSON.
size_t GetArraySize(const json &j, const std::string &key)
Get the size of an array.
bool ValidateKeysVerbose(const json &j, const std::vector< std::string > &requiredKeys, const std::string &context="")
Validate that all required keys exist and log which ones are missing.
std::string GetNestedString(const json &j, const std::string &parentKey, const std::string &childKey, const std::string &defaultValue="")
Get a nested string value from a parent object.
uint32_t GetUInt(const json &j, const std::string &key, uint32_t defaultValue=0)
Safely get an unsigned integer value from JSON.
T json_get(const json &j, const std::string &key, const T &defaultValue)
Generic template function to safely get any type from JSON.
bool SaveConfig(const std::string &filepath, const json &j)
Save a configuration file (alias for SaveJsonToFile with indent=2)
float GetNestedFloat(const json &j, const std::string &parentKey, const std::string &childKey, float defaultValue=0.0f)
Get a nested float value from a parent object.
double GetDouble(const json &j, const std::string &key, double defaultValue=0.0)
Safely get a double value from JSON.
int GetNestedInt(const json &j, const std::string &parentKey, const std::string &childKey, int defaultValue=0)
Get a nested int value from a parent object.
bool LoadJsonFromFile(const std::string &filepath, json &j)
Load and parse a JSON file.
bool ValidateKeys(const json &j, const std::vector< std::string > &requiredKeys)
Validate that all required keys exist in a JSON object.
int GetInt(const json &j, const std::string &key, int defaultValue=0)
Safely get an integer value from JSON.
bool LoadConfig(const std::string &filepath, json &j)
Load a configuration file (alias for LoadJsonFromFile)
void ForEachInArray(const json &j, const std::string &key, std::function< void(const json &, size_t)> callback)
Iterate over an array with a callback function.
float GetFloat(const json &j, const std::string &key, float defaultValue=0.0f)
Safely get a float value from JSON.
bool SaveJsonToFile(const std::string &filepath, const json &j, int indent=4)
Save a JSON object to a file with formatting.
bool IsArray(const json &j, const std::string &key)
Check if a key contains an array.
bool IsObject(const json &j, const std::string &key)
Check if a key contains an object.
bool GetBool(const json &j, const std::string &key, bool defaultValue=false)
Safely get a boolean value from JSON.