Transform JSON objects into native objects

This commit is contained in:
Bernhard Rosenkränzer 2024-08-29 13:21:00 +02:00
parent 7f0cd77b4e
commit 892de92aa2
6 changed files with 59 additions and 23 deletions

View File

@ -9,7 +9,8 @@ set(QT_VERSION 6 CACHE STRING "Major version of Qt to use (6 will work, anything
find_package(Qt${QT_VERSION} COMPONENTS Core Gui Widgets WebEngineWidgets REQUIRED)
add_subdirectory(dialogs)
add_subdirectory(lib)
add_executable(configurator main.cpp Configurator.cpp)
target_link_libraries(configurator Qt${QT_VERSION}::Core Qt${QT_VERSION}::Gui Qt${QT_VERSION}::Widgets Qt${QT_VERSION}::WebEngineWidgets Qt${QT_VERSION}::Network dialogs)
target_link_libraries(configurator Qt${QT_VERSION}::Core Qt${QT_VERSION}::Gui Qt${QT_VERSION}::Widgets Qt${QT_VERSION}::WebEngineWidgets Qt${QT_VERSION}::Network dialogs fed)
install(TARGETS configurator DESTINATION bin)

View File

@ -1,6 +1,6 @@
#include "Configurator.h"
#include <iostream>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
Configurator::Configurator(int &argc, char **&argv):QApplication(argc, argv),_nam(this) {
@ -25,27 +25,13 @@ void Configurator::appListReceived(QNetworkReply *reply) {
_loginDialog->show();
return;
}
_services=QJsonDocument::fromJson(reply->readAll());
if(!_services.isArray()) {
QJsonDocument services = QJsonDocument::fromJson(reply->readAll());
if(!services.isArray()) {
_loginDialog->setMessage(tr("App list did not return an array. Is the domain name correct?"));
_loginDialog->show();
return;
}
QJsonArray a=_services.array();
for(auto i : a) {
if(!i.isObject()) {
std::cerr << "Non-object in JSON array" << std::endl;
continue;
}
QJsonObject o = i.toObject();
// We can assume the object has:
// o["Title"].toString()
// o["Url"].toString()
// o["DocumentationUrl"].toString()
// o["Image"].toString()
// o["Description"].toString()
// o["SpecialNote"].toString()
// o["LDAP"].toBool()
std::cerr << qPrintable(o["Title"].toString()) << " " << o["LDAP"].toBool() << std::endl;
}
_services = services.array();
for(auto i : _services)
std::cerr << qPrintable(i.title) << std::endl;
}

View File

@ -3,8 +3,9 @@
#include <QApplication>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QJsonDocument>
#include <dialogs/FederatedLogin.h>
#include <lib/Service.h>
class Configurator:public QApplication {
Q_OBJECT
@ -16,5 +17,5 @@ protected Q_SLOTS:
private:
QNetworkAccessManager _nam;
FederatedLogin * _loginDialog;
QJsonDocument _services;
Services _services;
};

2
lib/CMakeLists.txt Normal file
View File

@ -0,0 +1,2 @@
add_library(fed STATIC Service.cpp)
target_link_libraries(fed Qt${QT_VERSION}::Core Qt${QT_VERSION}::Gui Qt${QT_VERSION}::Widgets)

22
lib/Service.cpp Normal file
View File

@ -0,0 +1,22 @@
#include "Service.h"
#include <iostream>
Service::Service(QJsonObject const &o) {
title = o["Title"].toString();
url = o["Url"].toString();
documentationUrl = o["DocumentationUrl"].toString();
image = o["Image"].toString();
description = o["Description"].toString();
specialNote = o["SpecialNote"].toString();
LDAP = o["LDAP"].toBool();
}
Services::Services(QJsonArray const &a):QList<Service>() {
for(auto i : a) {
if(!i.isObject()) {
std::cerr << "Non-object in JSON array, ignoring" << std::endl;
continue;
}
append(Service{i.toObject()});
}
}

24
lib/Service.h Normal file
View File

@ -0,0 +1,24 @@
#pragma once
#include <QJsonArray>
#include <QJsonObject>
#include <QString>
#include <QUrl>
struct Service {
Service(QJsonObject const &o);
QString title;
QUrl url;
QUrl documentationUrl;
QUrl image;
QString description;
QString specialNote;
bool LDAP;
};
class Services:public QList<Service> {
public:
Services():QList<Service>() {}
Services(QJsonArray const &a);
};