scar-chat7/shared/utils/json_config.h

52 lines
1.1 KiB
C
Raw Permalink Normal View History

2025-12-07 12:00:44 -07:00
#pragma once
#include <string>
#include <optional>
#include <nlohmann/json.hpp>
namespace scar {
class JsonConfig {
public:
explicit JsonConfig(const std::string& file_path);
// Load config from file (returns false if file doesn't exist)
bool load();
// Save config to file
bool save() const;
// Get value with optional default
template<typename T>
T get(const std::string& key, const T& default_value) const {
try {
if (data_.contains(key)) {
return data_[key].get<T>();
}
} catch (const std::exception&) {
// Fall through to default
}
return default_value;
}
// Set value
template<typename T>
void set(const std::string& key, const T& value) {
data_[key] = value;
}
// Check if key exists
bool has(const std::string& key) const {
return data_.contains(key);
}
// Get file path
const std::string& filePath() const { return file_path_; }
private:
std::string file_path_;
nlohmann::json data_;
};
} // namespace scar