1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

add json parser

This commit is contained in:
Pascal Bauer 2024-08-26 13:04:34 +02:00 committed by Niklas Eiling
parent 41a1edf07a
commit dde382c382

View file

@ -0,0 +1,54 @@
/* Wrapper for Jannson library to process Json files in OOP.
*
* Author: Pascal Bauer <pascal.bauer@rwth-aachen.de>
*
* SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <pascal.bauer@rwth-aachen.de>
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <jansson.h>
#include <memory>
#include <spdlog/common.h>
#include <string>
class JsonParser {
private:
inline static auto logger = villas::Log::get("Json Parser");
public:
json_t* json;
public:
JsonParser(json_t *json) : json(json) {}
JsonParser(const std::string &configFilePath) {
FILE *f = fopen(configFilePath.c_str(), "r");
if (!f)
throw RuntimeError("Cannot open config file: {}", configFilePath);
this->json = json_loadf(f, 0, nullptr);
if (!json) {
logger->error("Cannot parse JSON config");
fclose(f);
throw RuntimeError("Cannot parse JSON config");
}
fclose(f);
}
~JsonParser() {
json_decref(json);
}
json_t* get(const std::string &key) {
json_t* result = json_object_get(this->json, key.c_str());
if (result == nullptr) {
logger->error("No section {} found in config", key);
exit(1);
}
return result;
}
};