mirror of
https://github.com/MadeOfJelly/MushMachine.git
synced 2024-12-04 19:23:28 +01:00
add ordered_json variants for reading files
This commit is contained in:
parent
191e9f6b44
commit
ab0e5afb94
@ -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");
|
||||||
|
@ -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;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user