Replace nlohmann with rei-json

This commit is contained in:
Vyn 2025-06-24 18:56:32 +02:00
parent 72b004b7b0
commit ef56efd314
Signed by: vyn
GPG key ID: E1B2BE34E7A971E7
8 changed files with 51 additions and 24801 deletions

View file

@ -21,3 +21,7 @@ add_library(mirai-core
target_include_directories(mirai-core PRIVATE "external")
target_include_directories(mirai-core PRIVATE "include/mirai-core")
add_subdirectory(external/rei-json)
target_include_directories(mirai-core PRIVATE "external/rei-json/include")
target_link_libraries(mirai-core PRIVATE rei-json)

File diff suppressed because it is too large Load diff

@ -0,0 +1 @@
Subproject commit 63e7986b0901449657c3874ed7b19618315e9f01

View file

@ -8,8 +8,11 @@
#include "DataProvider.h"
#include "MarkdownDataProvider.h"
#include "Source.h"
#include "nlohmann/json.hpp"
#include "rei-json/Array.h"
#include "rei-json/Object.h"
#include "rei-json/json.h"
#include <algorithm>
#include <cassert>
#include <iostream>
#include <memory>
#include <print>
@ -21,31 +24,32 @@ namespace mirai
void Mirai::loadConfig(const std::string &path)
{
auto configJson = nlohmann::json::parse(R"(
{
"files": []
}
)");
std::ifstream file(path);
if (!file) {
return;
}
configJson = nlohmann::json::parse(file);
std::stringstream buffer;
buffer << file.rdbuf();
auto jsonStr = buffer.str();
assert(configJson.is_object());
assert(configJson["files"].is_array());
auto jsonSources = configJson["files"];
for (const auto &filePath : jsonSources) {
assert(filePath.is_object());
const auto name = filePath["name"].get<std::string>();
const auto path = filePath["path"].get<std::string>();
auto configJson = rei::json::parse(jsonStr);
assert(configJson.isObject());
assert(configJson.asObject()["files"].isArray());
auto jsonSources = configJson.asObject()["files"].asArray();
for (int i = 0; i < jsonSources.count(); ++i) {
auto &filePath = jsonSources[i];
assert(filePath.isObject());
const auto name = filePath.asObject().getString("name");
const auto path = filePath.asObject().getString("path");
std::unique_ptr<DataProvider> file = std::make_unique<MarkdownDataProvider>(path);
DataProvider *sourceDataProvider = file.release();
sourceDataProvider->load();
sources_.push_back(std::make_unique<Source>(
SourceConstructor{.name = name, .sourceDataProvider = sourceDataProvider}
));
sources_.push_back(
std::make_unique<Source>(
SourceConstructor{.name = name, .sourceDataProvider = sourceDataProvider}
)
);
}
}
@ -56,20 +60,19 @@ void Mirai::saveConfig()
std::print(std::cerr, "Can't save config to {}", configPath_);
return;
}
auto configJson = nlohmann::json::parse(R"(
{
"files": []
}
)");
auto files = rei::json::JsonArray{};
auto configJson = rei::json::JsonObject{};
configJson.set("files", files);
for (auto &source : sources_) {
nlohmann::json jsonSource;
jsonSource["name"] = source->name();
rei::json::JsonObject jsonSource;
jsonSource.set("name", source->name());
auto dataProvider = dynamic_cast<MarkdownDataProvider *>(source->dataProvider());
jsonSource["path"] = dataProvider->path();
jsonSource["type"] = "FileSystemMarkdown";
configJson["files"].push_back(jsonSource);
jsonSource.set("path", dataProvider->path());
jsonSource.set("type", "FileSystemMarkdown");
configJson.getArray("files").push(jsonSource);
}
file << configJson.dump(4);
file << rei::json::toString(configJson);
file.close();
}
@ -84,9 +87,11 @@ void Mirai::addSource(
{
DataProvider *sourceDataProvider = source.release();
sourceDataProvider->load();
sources_.push_back(std::make_unique<Source>(
SourceConstructor{.name = name, .sourceDataProvider = sourceDataProvider}
));
sources_.push_back(
std::make_unique<Source>(
SourceConstructor{.name = name, .sourceDataProvider = sourceDataProvider}
)
);
saveConfig();
sourceAdded.emit(nullptr);
};

2
external/rei-json vendored

@ -1 +1 @@
Subproject commit 2639dba60a990f9dbc79b5204a925a101ecf24ba
Subproject commit 63e7986b0901449657c3874ed7b19618315e9f01

2
external/selenite vendored

@ -1 +1 @@
Subproject commit 810607c01fa659f31bd11bd41a9ee5611e5db8ea
Subproject commit 6ff7dd8ea890bec8f4f61304e64bbfeabd61e7a8