3#include <rapidjson/document.h>
4#include <rapidjson/istreamwrapper.h>
5#include <rapidjson/ostreamwrapper.h>
6#include <rapidjson/writer.h>
12 template <
typename T> std::string stringifyRapidjson(
const T &obj)
14 rapidjson::StringBuffer sbuffer;
15 rapidjson::Writer<rapidjson::StringBuffer> writer(sbuffer);
17 return sbuffer.GetString();
24 if (!inFile.is_open())
26 throw std::invalid_argument(
"Can't open config file");
29 rapidjson::IStreamWrapper fStreamWrapper(inFile);
31 rapidjson::Document doc;
32 doc.ParseStream(fStreamWrapper);
37 throw std::invalid_argument(
"Read config is empty or invalid JSON format");
41 for (
const auto &entry : doc.GetObject())
44 entry.value.IsString() ? entry.value.GetString() : stringifyRapidjson(entry.value);
51 rapidjson::OStreamWrapper fStreamWrapper(outFile);
53 rapidjson::Document doc;
56 for (
const auto &[keyVal, valueVal] :
_configMap)
58 rapidjson::Value key(keyVal.c_str(), doc.GetAllocator());
59 rapidjson::Value value(valueVal.c_str(), doc.GetAllocator());
60 doc.AddMember(key, value, doc.GetAllocator());
63 rapidjson::Writer<rapidjson::OStreamWrapper> writer(fStreamWrapper);
72 return itr ==
_configMap.end() ?
"" : itr->second;
std::unordered_map< std::string, std::string > _configMap
void set(const std::string &key, const std::string_view &value)
std::string get(const std::string &key) const
void remove(const std::string &key)
ConfigParser(std::string configPath)