Files
trmnl_server_cpp/helpers.h

42 lines
1.5 KiB
C++

#ifndef HELPERS_H_
#define HELPERS_H_
#include <iostream>
#include <string>
#include <stdint.h>
#include "json.hpp"
// Reads json from supplied filename
// j - output json
// filename - filepath from which to read json
// log - ostream to log human readable errors to - can be nullptr
// Returns - true if read and parse is successful
bool read_file_json(nlohmann::json& j, const std::string& filename, std::ostream* log);
// Reads configuration json from supplied istream
// j - output json
// in - istream from which to read json
// log - ostream to log human readable errors to - can be nullptr
// Returns - true if read and parse is successful
bool read_stream_json(nlohmann::json& j, std::istream& in, std::ostream* log);
// Writes json to supplied filename
// j - input json
// filename - filepath to which to write json
// log - ostream to log human readable errors to - can be nullptr
// Returns - true if write is successful
bool write_file_json(const nlohmann::json& j, const std::string& filename, std::ostream* log);
// JSON Extraction Helpers
// out value is modified only if an extraction happened
// Returns - whether an extraction happened
bool json_extract(const nlohmann::json& j, const std::string& key, std::string& out);
bool json_extract(const nlohmann::json& j, const std::string& key, int& out);
bool json_extract(const nlohmann::json& j, const std::string& key, bool& out);
bool json_extract(const nlohmann::json& j, const std::string& key, uint16_t& out);
#endif // HELPERS_H_