mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-16 00:00:02 +01:00
add json parser
This commit is contained in:
parent
95dc3b114c
commit
fafbd37591
1 changed files with 54 additions and 0 deletions
54
fpga/include/villas/fpga/json_parser.hpp
Normal file
54
fpga/include/villas/fpga/json_parser.hpp
Normal 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;
|
||||
}
|
||||
};
|
Loading…
Add table
Reference in a new issue