feat: 使用QJson替换nlohmann
使用qt自带json库,替换掉nlohmann库 Log: Task: https://pms.uniontech.com/task-view-111975.html Influence: json解析 Change-Id: Ifef186afc84f7ebd92f9f1591df4b96eba0774c7
This commit is contained in:
@ -1,17 +1,32 @@
|
||||
#ifndef BASIC_H_
|
||||
#define BASIC_H_
|
||||
#include <QDebug>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
namespace Methods
|
||||
{
|
||||
struct Basic
|
||||
{
|
||||
QString type;
|
||||
};
|
||||
|
||||
namespace Methods {
|
||||
struct Basic {
|
||||
std::string type;
|
||||
};
|
||||
inline void fromJson(const QByteArray &array, Basic &basic)
|
||||
{
|
||||
QJsonDocument doc = QJsonDocument::fromJson(array);
|
||||
if (!doc.isObject()) {
|
||||
qWarning() << "fromJson basic failed";
|
||||
return;
|
||||
}
|
||||
|
||||
using json = nlohmann::json;
|
||||
inline void from_json(const json &j, Basic &basic) {
|
||||
j.at("type").get_to(basic.type);
|
||||
}
|
||||
QJsonObject obj = doc.object();
|
||||
if (!obj.contains("type")) {
|
||||
qWarning() << "type not exist in basic array";
|
||||
return;
|
||||
}
|
||||
|
||||
basic.type = obj.value("type").toString();
|
||||
}
|
||||
|
||||
} // namespace Methods
|
||||
|
||||
|
@ -1,23 +1,39 @@
|
||||
#ifndef C664E26D_6517_412B_950F_07E20963349E
|
||||
#define C664E26D_6517_412B_950F_07E20963349E
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <QDebug>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
|
||||
namespace Methods {
|
||||
struct Instance {
|
||||
std::string hash;
|
||||
std::string type{ "instance" };
|
||||
QString hash;
|
||||
QString type{ "instance" };
|
||||
};
|
||||
|
||||
using json = nlohmann::json;
|
||||
inline void to_json(json &j, const Instance &instance)
|
||||
{
|
||||
j = json{ { "type", instance.type }, { "hash", instance.hash } };
|
||||
inline void toJson(QByteArray &array, const Instance &instance) {
|
||||
QJsonObject obj {
|
||||
{ "type", instance.type },
|
||||
{ "hash", instance.hash }
|
||||
};
|
||||
|
||||
array = QJsonDocument(obj).toJson();
|
||||
}
|
||||
|
||||
inline void from_json(const json &j, Instance &instance)
|
||||
{
|
||||
j.at("hash").get_to(instance.hash);
|
||||
inline void fromJson(const QByteArray &array, Instance &instance) {
|
||||
QJsonDocument doc = QJsonDocument::fromJson(array);
|
||||
if (!doc.isObject()) {
|
||||
qWarning() << "fromJson instance failed";
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonObject obj = doc.object();
|
||||
if (!obj.contains("hash")) {
|
||||
qWarning() << "hash not exist in instance array";
|
||||
return;
|
||||
}
|
||||
|
||||
instance.hash = obj.value("hash").toString();
|
||||
}
|
||||
|
||||
} // namespace Methods
|
||||
|
@ -1,27 +1,46 @@
|
||||
#ifndef QUIT_H_
|
||||
#define QUIT_H_
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <QDebug>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
|
||||
namespace Methods {
|
||||
struct Quit {
|
||||
std::string date;
|
||||
std::string type{ "quit" };
|
||||
std::string id;
|
||||
int code;
|
||||
QString date;
|
||||
QString type{ "quit" };
|
||||
QString id;
|
||||
int code;
|
||||
};
|
||||
using json = nlohmann::json;
|
||||
inline void to_json(json &j, const Quit &quit)
|
||||
{
|
||||
j = json{ { "type", quit.type }, { "date", quit.date }, { "id", quit.id }, { "code", quit.code } };
|
||||
|
||||
inline void toJson(QByteArray &array, const Quit &quit) {
|
||||
QJsonObject obj {
|
||||
{ "type", quit.type },
|
||||
{ "date", quit.date },
|
||||
{ "id", quit.id },
|
||||
{ "code", quit.code }
|
||||
};
|
||||
|
||||
array = QJsonDocument(obj).toJson();
|
||||
}
|
||||
|
||||
inline void from_json(const json &j, Quit &quit)
|
||||
{
|
||||
j.at("id").get_to(quit.id);
|
||||
j.at("date").get_to(quit.date);
|
||||
j.at("code").get_to(quit.code);
|
||||
inline void fromJson(const QByteArray &array, Quit &quit) {
|
||||
QJsonDocument doc = QJsonDocument::fromJson(array);
|
||||
if (!doc.isObject()) {
|
||||
qWarning() << "fromJson quit failed";
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonObject obj = doc.object();
|
||||
if (!obj.contains("id") || !obj.contains("date") || !obj.contains("code")) {
|
||||
qWarning() << "id date code not exist in quit array";
|
||||
return;
|
||||
}
|
||||
|
||||
quit.id = obj.value("id").toString();
|
||||
quit.date = obj.value("date").toString();
|
||||
quit.code = obj.value("code").toInt();
|
||||
}
|
||||
|
||||
} // namespace Methods
|
||||
|
||||
#endif // QUIT_H_
|
||||
|
@ -1,29 +1,50 @@
|
||||
#ifndef REGISTER_H_
|
||||
#define REGISTER_H_
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
#include <QDebug>
|
||||
|
||||
namespace Methods {
|
||||
struct Registe {
|
||||
std::string date;
|
||||
std::string id;
|
||||
std::string type{ "registe" };
|
||||
std::string hash;
|
||||
QString date;
|
||||
QString id;
|
||||
QString type{ "registe" };
|
||||
QString hash;
|
||||
bool state;
|
||||
};
|
||||
|
||||
using json = nlohmann::json;
|
||||
inline void to_json(json &j, const Registe ®iste)
|
||||
{
|
||||
j = json{ { "type", registe.type }, { "id", registe.id }, { "hash", registe.hash }, { "state", registe.state }, { "date", registe.date } };
|
||||
inline void toJson(QByteArray &array, const Registe ®iste) {
|
||||
QJsonObject obj = {
|
||||
{ "type", registe.type },
|
||||
{ "id", registe.id },
|
||||
{ "hash", registe.hash },
|
||||
{ "state", registe.state },
|
||||
{ "date", registe.date }
|
||||
};
|
||||
|
||||
array = QJsonDocument(obj).toJson();
|
||||
}
|
||||
inline void from_json(const json &j, Registe ®iste)
|
||||
{
|
||||
j.at("id").get_to(registe.id);
|
||||
j.at("date").get_to(registe.date);
|
||||
j.at("hash").get_to(registe.hash);
|
||||
j.at("state").get_to(registe.state);
|
||||
|
||||
inline void fromJson(const QByteArray &array, Registe ®iste) {
|
||||
QJsonDocument doc = QJsonDocument::fromJson(array);
|
||||
if (!doc.isObject()) {
|
||||
qWarning() << "fromJson registe failed";
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonObject obj = doc.object();
|
||||
if (!obj.contains("id") || !obj.contains("date") || !obj.contains("hash")\
|
||||
|| !obj.contains("state")) {
|
||||
qWarning() << "id date code state not exist in registe array";
|
||||
return;
|
||||
}
|
||||
|
||||
registe.id = obj.value("id").toString();
|
||||
registe.date = obj.value("date").toString();
|
||||
registe.hash = obj.value("hash").toString();
|
||||
registe.state = obj.value("state").toBool();
|
||||
}
|
||||
|
||||
} // namespace Methods
|
||||
|
||||
#endif // REGISTER_H_
|
||||
|
@ -1,36 +1,76 @@
|
||||
#ifndef B0B88BD6_CF1E_4E87_926A_E6DBE6B9B19C
|
||||
#define B0B88BD6_CF1E_4E87_926A_E6DBE6B9B19C
|
||||
|
||||
#include <map>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <QList>
|
||||
#include <QMap>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonArray>
|
||||
#include <QDebug>
|
||||
|
||||
namespace Methods {
|
||||
struct Task {
|
||||
std::string id;
|
||||
std::string runId;
|
||||
std::string type{ "task" };
|
||||
std::string date;
|
||||
std::vector<std::string> arguments;
|
||||
std::multimap<std::string, std::string> environments;
|
||||
};
|
||||
|
||||
using json = nlohmann::json;
|
||||
inline void to_json(json &j, const Task &task)
|
||||
namespace Methods
|
||||
{
|
||||
j = json{ { "type", task.type }, { "id", task.id }, { "run_id", task.runId }, { "date", task.date }, { "arguments", task.arguments }, { "environments", task.environments } };
|
||||
}
|
||||
struct Task
|
||||
{
|
||||
QString id;
|
||||
QString runId;
|
||||
QString type{"task"};
|
||||
QString date;
|
||||
QList<QString> arguments;
|
||||
QMap<QString, QString> environments;
|
||||
};
|
||||
|
||||
inline void from_json(const json &j, Task &task)
|
||||
{
|
||||
j.at("id").get_to(task.id);
|
||||
j.at("run_id").get_to(task.runId);
|
||||
j.at("date").get_to(task.date);
|
||||
j.at("arguments").get_to(task.arguments);
|
||||
j.at("environments").get_to(task.environments);
|
||||
}
|
||||
inline void toJson(QByteArray &array, const Task &task) {
|
||||
QJsonArray argArray;
|
||||
for (auto arg : task.arguments) {
|
||||
argArray.append(arg);
|
||||
}
|
||||
|
||||
} // namespace Methods
|
||||
QVariantMap envsMap;
|
||||
for (auto it = task.environments.constBegin(); it != task.environments.constEnd(); ++it) {
|
||||
envsMap.insert(it.key(), it.value());
|
||||
}
|
||||
|
||||
QJsonObject obj = {
|
||||
{"type", task.type},
|
||||
{"id", task.id},
|
||||
{"run_id", task.runId},
|
||||
{"date", task.date},
|
||||
{"arguments", argArray},
|
||||
{"environments", QJsonObject::fromVariantMap(envsMap)}
|
||||
};
|
||||
|
||||
array = QJsonDocument(obj).toJson();
|
||||
}
|
||||
|
||||
inline void fromJson(const QByteArray &array, Task &task) {
|
||||
QJsonDocument doc = QJsonDocument::fromJson(array);
|
||||
if (!doc.isObject()) {
|
||||
qWarning() << "fromJson task failed";
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonObject obj = doc.object();
|
||||
if (!obj.contains("id") || !obj.contains("runId") || !obj.contains("date") \
|
||||
|| !obj.contains("arguments") || !obj.contains("environments")) {
|
||||
qWarning() << "id runId date arguments environments not exist in task array";
|
||||
return;
|
||||
}
|
||||
|
||||
task.id = obj.value("id").toString();
|
||||
task.runId = obj.value("runId").toString();
|
||||
task.date = obj.value("date").toString();
|
||||
for (auto arg : obj.value("arguments").toArray()) {
|
||||
task.arguments.append(arg.toString());
|
||||
}
|
||||
|
||||
QVariantMap envsMap = obj.value("environments").toObject().toVariantMap();
|
||||
for (auto it = envsMap.constBegin(); it != envsMap.constEnd(); ++it) {
|
||||
task.environments.insert(it.key(), it.value().toString());
|
||||
}
|
||||
}
|
||||
} // namespace Methods
|
||||
|
||||
#endif /* B0B88BD6_CF1E_4E87_926A_E6DBE6B9B19C */
|
||||
|
Reference in New Issue
Block a user