-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5be9a1
commit ef5fd45
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* Wrapper for Jannson library to process Json files in OOP. | ||
* | ||
* Author: Pascal Bauer <[email protected]> | ||
* | ||
* SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <[email protected]> | ||
* 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::logging.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; | ||
} | ||
}; |