Skip to content

Commit

Permalink
add json class
Browse files Browse the repository at this point in the history
Signed-off-by: IgnoreWarnings <[email protected]>
  • Loading branch information
IgnoreWarnings committed Jun 11, 2024
1 parent 2b13450 commit 45f732b
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions fpga/include/villas/fpga/json_parser.hpp
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;
}
};

0 comments on commit 45f732b

Please sign in to comment.