mirror of
https://github.com/logos-co/logos-liblogos.git
synced 2026-08-27 12:51:10 +00:00
move from Qt JSON to nlohmann_json
This commit is contained in:
@@ -8,6 +8,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core RemoteObjects)
|
||||
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core RemoteObjects)
|
||||
|
||||
# Find nlohmann_json
|
||||
find_package(nlohmann_json REQUIRED)
|
||||
|
||||
# Set output directories
|
||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
pkgs.qt6.qtremoteobjects
|
||||
pkgs.zstd
|
||||
pkgs.gtest
|
||||
pkgs.nlohmann_json
|
||||
logosModule
|
||||
processStats
|
||||
logosPackageManager
|
||||
|
||||
@@ -158,6 +158,7 @@ target_link_libraries(logos_core PUBLIC
|
||||
logos_module
|
||||
process_stats
|
||||
package_manager_lib
|
||||
nlohmann_json::nlohmann_json
|
||||
)
|
||||
|
||||
# Include directories for the library
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include "dependency_resolver.h"
|
||||
#include <QDebug>
|
||||
#include <QJsonArray>
|
||||
#include <QSet>
|
||||
#include <QHash>
|
||||
|
||||
@@ -29,11 +28,10 @@ namespace DependencyResolver {
|
||||
|
||||
modulesToLoad.insert(moduleName);
|
||||
|
||||
QJsonObject metadata = getMetadata(moduleName);
|
||||
if (!metadata.isEmpty()) {
|
||||
QJsonArray deps = metadata.value("dependencies").toArray();
|
||||
for (const QJsonValue& dep : deps) {
|
||||
QString depName = dep.toString();
|
||||
nlohmann::json metadata = getMetadata(moduleName);
|
||||
if (metadata.is_object() && metadata.contains("dependencies")) {
|
||||
for (const auto& dep : metadata["dependencies"]) {
|
||||
QString depName = QString::fromStdString(dep.get<std::string>());
|
||||
if (!depName.isEmpty() && !modulesToLoad.contains(depName)) {
|
||||
queue.append(depName);
|
||||
}
|
||||
@@ -54,11 +52,10 @@ namespace DependencyResolver {
|
||||
inDegree[moduleName] = 0;
|
||||
}
|
||||
|
||||
QJsonObject metadata = getMetadata(moduleName);
|
||||
if (!metadata.isEmpty()) {
|
||||
QJsonArray deps = metadata.value("dependencies").toArray();
|
||||
for (const QJsonValue& dep : deps) {
|
||||
QString depName = dep.toString();
|
||||
nlohmann::json metadata = getMetadata(moduleName);
|
||||
if (metadata.is_object() && metadata.contains("dependencies")) {
|
||||
for (const auto& dep : metadata["dependencies"]) {
|
||||
QString depName = QString::fromStdString(dep.get<std::string>());
|
||||
if (!depName.isEmpty() && modulesToLoad.contains(depName)) {
|
||||
inDegree[moduleName]++;
|
||||
dependents[depName].append(moduleName);
|
||||
|
||||
@@ -3,13 +3,13 @@
|
||||
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QJsonObject>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <functional>
|
||||
|
||||
namespace DependencyResolver {
|
||||
|
||||
using IsKnownFn = std::function<bool(const QString&)>;
|
||||
using GetMetadataFn = std::function<QJsonObject(const QString&)>;
|
||||
using GetMetadataFn = std::function<nlohmann::json(const QString&)>;
|
||||
|
||||
QStringList resolve(const QStringList& requested,
|
||||
IsKnownFn isKnown,
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QHash>
|
||||
#include <QJsonObject>
|
||||
|
||||
class PluginRegistry;
|
||||
|
||||
|
||||
@@ -1,14 +1,37 @@
|
||||
#include "plugin_registry.h"
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonValue>
|
||||
#include <cassert>
|
||||
#include <module_lib/module_lib.h>
|
||||
#include <package_manager_lib.h>
|
||||
|
||||
using namespace ModuleLib;
|
||||
|
||||
static nlohmann::json qjsonToNlohmann(const QJsonValue& val) {
|
||||
switch (val.type()) {
|
||||
case QJsonValue::Bool: return val.toBool();
|
||||
case QJsonValue::Double: return val.toDouble();
|
||||
case QJsonValue::String: return val.toString().toStdString();
|
||||
case QJsonValue::Array: {
|
||||
nlohmann::json arr = nlohmann::json::array();
|
||||
for (const QJsonValue& v : val.toArray())
|
||||
arr.push_back(qjsonToNlohmann(v));
|
||||
return arr;
|
||||
}
|
||||
case QJsonValue::Object: {
|
||||
nlohmann::json obj = nlohmann::json::object();
|
||||
QJsonObject qobj = val.toObject();
|
||||
for (auto it = qobj.begin(); it != qobj.end(); ++it)
|
||||
obj[it.key().toStdString()] = qjsonToNlohmann(it.value());
|
||||
return obj;
|
||||
}
|
||||
default: return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
static PackageManagerLib& packageManagerInstance() {
|
||||
static PackageManagerLib instance;
|
||||
return instance;
|
||||
@@ -40,13 +63,12 @@ void PluginRegistry::discoverInstalledModules() {
|
||||
}
|
||||
|
||||
std::string jsonStr = pm.getInstalledModules();
|
||||
QJsonDocument doc = QJsonDocument::fromJson(QByteArray::fromStdString(jsonStr));
|
||||
QJsonArray modules = doc.array();
|
||||
nlohmann::json modules = nlohmann::json::parse(jsonStr, nullptr, false);
|
||||
if (!modules.is_array()) return;
|
||||
|
||||
for (const QJsonValue& val : modules) {
|
||||
QJsonObject mod = val.toObject();
|
||||
QString name = mod.value("name").toString();
|
||||
QString mainFilePath = mod.value("mainFilePath").toString();
|
||||
for (const auto& mod : modules) {
|
||||
QString name = QString::fromStdString(mod.value("name", ""));
|
||||
QString mainFilePath = QString::fromStdString(mod.value("mainFilePath", ""));
|
||||
|
||||
if (name.isEmpty() || mainFilePath.isEmpty())
|
||||
continue;
|
||||
@@ -77,7 +99,7 @@ QString PluginRegistry::processPlugin(const QString& pluginPath) {
|
||||
}
|
||||
|
||||
m_knownPlugins.insert(metadata.name, pluginPath);
|
||||
m_pluginMetadata.insert(metadata.name, metadata.rawMetadata);
|
||||
m_pluginMetadata.insert(metadata.name, qjsonToNlohmann(QJsonValue(metadata.rawMetadata)));
|
||||
|
||||
return metadata.name;
|
||||
}
|
||||
@@ -90,7 +112,7 @@ QString PluginRegistry::pluginPath(const QString& name) const {
|
||||
return m_knownPlugins.value(name);
|
||||
}
|
||||
|
||||
QJsonObject PluginRegistry::pluginMetadata(const QString& name) const {
|
||||
nlohmann::json PluginRegistry::pluginMetadata(const QString& name) const {
|
||||
return m_pluginMetadata.value(name);
|
||||
}
|
||||
|
||||
@@ -102,7 +124,7 @@ void PluginRegistry::registerPlugin(const QString& name, const QString& path) {
|
||||
m_knownPlugins.insert(name, path);
|
||||
}
|
||||
|
||||
void PluginRegistry::registerMetadata(const QString& name, const QJsonObject& metadata) {
|
||||
void PluginRegistry::registerMetadata(const QString& name, const nlohmann::json& metadata) {
|
||||
m_pluginMetadata.insert(name, metadata);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QHash>
|
||||
#include <QJsonObject>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
class PluginRegistry {
|
||||
public:
|
||||
@@ -17,10 +17,10 @@ public:
|
||||
|
||||
bool isKnown(const QString& name) const;
|
||||
QString pluginPath(const QString& name) const;
|
||||
QJsonObject pluginMetadata(const QString& name) const;
|
||||
nlohmann::json pluginMetadata(const QString& name) const;
|
||||
QStringList knownPluginNames() const;
|
||||
void registerPlugin(const QString& name, const QString& path);
|
||||
void registerMetadata(const QString& name, const QJsonObject& metadata);
|
||||
void registerMetadata(const QString& name, const nlohmann::json& metadata);
|
||||
|
||||
bool isLoaded(const QString& name) const;
|
||||
void markLoaded(const QString& name);
|
||||
@@ -33,7 +33,7 @@ public:
|
||||
private:
|
||||
QStringList m_pluginsDirs;
|
||||
QHash<QString, QString> m_knownPlugins;
|
||||
QHash<QString, QJsonObject> m_pluginMetadata;
|
||||
QHash<QString, nlohmann::json> m_pluginMetadata;
|
||||
QStringList m_loadedPlugins;
|
||||
};
|
||||
|
||||
|
||||
@@ -3,11 +3,9 @@
|
||||
#include "plugin_registry.h"
|
||||
#include "logos_core.h"
|
||||
#include <QCoreApplication>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QDir>
|
||||
#include <QTemporaryDir>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
|
||||
@@ -144,9 +142,9 @@ TEST_F(PluginManagerTest, ResolveDependencies_ReturnsEmptyForUnknownPlugin) {
|
||||
|
||||
TEST_F(PluginManagerTest, ResolveDependencies_ReturnsSinglePluginWithNoDeps) {
|
||||
PluginManager::registry().registerPlugin("plugin_a", "/path/to/plugin_a");
|
||||
QJsonObject metadata;
|
||||
nlohmann::json metadata;
|
||||
metadata["name"] = "plugin_a";
|
||||
metadata["dependencies"] = QJsonArray();
|
||||
metadata["dependencies"] = nlohmann::json::array();
|
||||
PluginManager::registry().registerMetadata("plugin_a", metadata);
|
||||
|
||||
QStringList requested;
|
||||
@@ -162,16 +160,14 @@ TEST_F(PluginManagerTest, ResolveDependencies_ReturnsCorrectOrder) {
|
||||
PluginManager::registry().registerPlugin("plugin_a", "/path/to/plugin_a");
|
||||
PluginManager::registry().registerPlugin("plugin_b", "/path/to/plugin_b");
|
||||
|
||||
QJsonObject metadataA;
|
||||
nlohmann::json metadataA;
|
||||
metadataA["name"] = "plugin_a";
|
||||
QJsonArray depsA;
|
||||
depsA.append("plugin_b");
|
||||
metadataA["dependencies"] = depsA;
|
||||
metadataA["dependencies"] = nlohmann::json::array({"plugin_b"});
|
||||
PluginManager::registry().registerMetadata("plugin_a", metadataA);
|
||||
|
||||
QJsonObject metadataB;
|
||||
nlohmann::json metadataB;
|
||||
metadataB["name"] = "plugin_b";
|
||||
metadataB["dependencies"] = QJsonArray();
|
||||
metadataB["dependencies"] = nlohmann::json::array();
|
||||
PluginManager::registry().registerMetadata("plugin_b", metadataB);
|
||||
|
||||
QStringList requested;
|
||||
@@ -189,23 +185,19 @@ TEST_F(PluginManagerTest, ResolveDependencies_HandlesTransitiveDeps) {
|
||||
PluginManager::registry().registerPlugin("plugin_b", "/path/to/plugin_b");
|
||||
PluginManager::registry().registerPlugin("plugin_c", "/path/to/plugin_c");
|
||||
|
||||
QJsonObject metadataA;
|
||||
nlohmann::json metadataA;
|
||||
metadataA["name"] = "plugin_a";
|
||||
QJsonArray depsA;
|
||||
depsA.append("plugin_b");
|
||||
metadataA["dependencies"] = depsA;
|
||||
metadataA["dependencies"] = nlohmann::json::array({"plugin_b"});
|
||||
PluginManager::registry().registerMetadata("plugin_a", metadataA);
|
||||
|
||||
QJsonObject metadataB;
|
||||
nlohmann::json metadataB;
|
||||
metadataB["name"] = "plugin_b";
|
||||
QJsonArray depsB;
|
||||
depsB.append("plugin_c");
|
||||
metadataB["dependencies"] = depsB;
|
||||
metadataB["dependencies"] = nlohmann::json::array({"plugin_c"});
|
||||
PluginManager::registry().registerMetadata("plugin_b", metadataB);
|
||||
|
||||
QJsonObject metadataC;
|
||||
nlohmann::json metadataC;
|
||||
metadataC["name"] = "plugin_c";
|
||||
metadataC["dependencies"] = QJsonArray();
|
||||
metadataC["dependencies"] = nlohmann::json::array();
|
||||
PluginManager::registry().registerMetadata("plugin_c", metadataC);
|
||||
|
||||
QStringList requested;
|
||||
@@ -270,17 +262,16 @@ static void createFakeModule(const QString& parentDir,
|
||||
QDir dir(parentDir);
|
||||
dir.mkpath(moduleName);
|
||||
|
||||
QJsonObject manifest;
|
||||
manifest["name"] = moduleName;
|
||||
nlohmann::json manifest;
|
||||
manifest["name"] = moduleName.toStdString();
|
||||
manifest["version"] = "1.0.0";
|
||||
manifest["type"] = type;
|
||||
manifest["main"] = mainFile;
|
||||
manifest["type"] = type.toStdString();
|
||||
manifest["main"] = mainFile.toStdString();
|
||||
manifest["description"] = "Fake test module";
|
||||
|
||||
QString manifestPath = dir.filePath(moduleName + "/manifest.json");
|
||||
std::ofstream mf(manifestPath.toStdString());
|
||||
QJsonDocument doc(manifest);
|
||||
mf << doc.toJson().toStdString();
|
||||
mf << manifest.dump();
|
||||
mf.close();
|
||||
|
||||
QString binaryPath = dir.filePath(moduleName + "/" + mainFile);
|
||||
|
||||
@@ -2,9 +2,7 @@
|
||||
#include <process_stats/process_stats.h>
|
||||
#include <QCoreApplication>
|
||||
#include <QProcess>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <cstring>
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -117,13 +115,10 @@ TEST_F(ProcessStatsTest, GetModuleStats_ReturnsEmptyArrayWhenNoPlugins) {
|
||||
|
||||
ASSERT_NE(result, nullptr);
|
||||
|
||||
QByteArray jsonData(result);
|
||||
QJsonDocument doc = QJsonDocument::fromJson(jsonData);
|
||||
nlohmann::json doc = nlohmann::json::parse(result);
|
||||
|
||||
EXPECT_TRUE(doc.isArray());
|
||||
|
||||
QJsonArray modulesArray = doc.array();
|
||||
EXPECT_EQ(modulesArray.size(), 0);
|
||||
EXPECT_TRUE(doc.is_array());
|
||||
EXPECT_EQ(doc.size(), 0);
|
||||
|
||||
delete[] result;
|
||||
}
|
||||
@@ -155,24 +150,21 @@ TEST_F(ProcessStatsTest, GetModuleStats_ReturnsValidJsonStructure) {
|
||||
|
||||
ASSERT_NE(result, nullptr);
|
||||
|
||||
QByteArray jsonData(result);
|
||||
QJsonDocument doc = QJsonDocument::fromJson(jsonData);
|
||||
nlohmann::json doc = nlohmann::json::parse(result);
|
||||
|
||||
EXPECT_TRUE(doc.isArray());
|
||||
EXPECT_TRUE(doc.is_array());
|
||||
ASSERT_EQ(doc.size(), 1);
|
||||
|
||||
QJsonArray modulesArray = doc.array();
|
||||
ASSERT_EQ(modulesArray.size(), 1);
|
||||
|
||||
QJsonObject moduleObj = modulesArray[0].toObject();
|
||||
auto moduleObj = doc[0];
|
||||
EXPECT_TRUE(moduleObj.contains("name"));
|
||||
EXPECT_TRUE(moduleObj.contains("cpu_percent"));
|
||||
EXPECT_TRUE(moduleObj.contains("cpu_time_seconds"));
|
||||
EXPECT_TRUE(moduleObj.contains("memory_mb"));
|
||||
|
||||
EXPECT_EQ(moduleObj["name"].toString().toStdString(), "test_plugin");
|
||||
EXPECT_GE(moduleObj["cpu_percent"].toDouble(), 0.0);
|
||||
EXPECT_GE(moduleObj["cpu_time_seconds"].toDouble(), 0.0);
|
||||
EXPECT_GE(moduleObj["memory_mb"].toDouble(), 0.0);
|
||||
EXPECT_EQ(moduleObj["name"].get<std::string>(), "test_plugin");
|
||||
EXPECT_GE(moduleObj["cpu_percent"].get<double>(), 0.0);
|
||||
EXPECT_GE(moduleObj["cpu_time_seconds"].get<double>(), 0.0);
|
||||
EXPECT_GE(moduleObj["memory_mb"].get<double>(), 0.0);
|
||||
|
||||
delete[] result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user