add ordered_json variants for reading files

This commit is contained in:
Green Sky 2022-11-24 20:50:02 +01:00
parent 191e9f6b44
commit ab0e5afb94
No known key found for this signature in database
2 changed files with 28 additions and 0 deletions

View File

@ -316,6 +316,31 @@ nlohmann::json FilesystemService::readJson(const char* filepath) const {
return r; return r;
} }
nlohmann::ordered_json FilesystemService::readJsonOrdered(fs_file_t file) const {
if (!file)
return {};
seek(file, 0);
std::string buffer;
readString(file, buffer);
// disable exeptions
// TODO: use callback instead of readString()
return nlohmann::ordered_json::parse(buffer, nullptr, false);
}
nlohmann::ordered_json FilesystemService::readJsonOrdered(const char* filepath) const {
if (!isFile(filepath)) {
return {};
}
auto h = open(filepath, READ);
auto r = readJsonOrdered(h);
close(h);
return r;
}
bool FilesystemService::writeJson(fs_file_t file, nlohmann::json& j, const int indent, const char indent_char) const { bool FilesystemService::writeJson(fs_file_t file, nlohmann::json& j, const int indent, const char indent_char) const {
if (!file) { if (!file) {
LOG_ERROR("writing json to invalid file"); LOG_ERROR("writing json to invalid file");

View File

@ -78,6 +78,9 @@ class FilesystemService : public Service {
nlohmann::json readJson(fs_file_t file) const; nlohmann::json readJson(fs_file_t file) const;
nlohmann::json readJson(const char* filepath) const; nlohmann::json readJson(const char* filepath) const;
nlohmann::ordered_json readJsonOrdered(fs_file_t file) const;
nlohmann::ordered_json readJsonOrdered(const char* filepath) const;
bool writeJson(fs_file_t file, nlohmann::json& j, const int indent = -1, const char indent_char = ' ') const; bool writeJson(fs_file_t file, nlohmann::json& j, const int indent = -1, const char indent_char = ' ') const;
bool writeJson(const char* filepath, nlohmann::json& j, const int indent = -1, const char indent_char = ' ') const; bool writeJson(const char* filepath, nlohmann::json& j, const int indent = -1, const char indent_char = ' ') const;