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 */
|
||||
|
@ -67,27 +67,29 @@ struct ClientPrivate {
|
||||
|
||||
return true;
|
||||
}
|
||||
nlohmann::json get(const nlohmann::json& call)
|
||||
{
|
||||
|
||||
QByteArray get(const QByteArray &call) {
|
||||
send(call);
|
||||
|
||||
char buf[512];
|
||||
char buf[512];
|
||||
std::string result;
|
||||
int bytesRead;
|
||||
int bytesRead;
|
||||
while ((bytesRead = recv(socket_fd, buf, 512, 0)) > 0) {
|
||||
for (int i = 0; i < bytesRead; i++) {
|
||||
for (int i = 0; i < bytesRead; ++i) {
|
||||
result += buf[i];
|
||||
}
|
||||
|
||||
if (buf[bytesRead - 1] == '\0') {
|
||||
break;
|
||||
}
|
||||
};
|
||||
return nlohmann::json::parse(result);
|
||||
}
|
||||
|
||||
QJsonDocument doc = QJsonDocument::fromRawData(result.data(), result.size());
|
||||
return doc.toJson();
|
||||
}
|
||||
|
||||
size_t send(const nlohmann::json& call)
|
||||
{
|
||||
std::string data = call.dump();
|
||||
size_t send(const QByteArray &call) {
|
||||
std::string data = call.data();
|
||||
data += '\0';
|
||||
return write(socket_fd, data.c_str(), data.length());
|
||||
}
|
||||
@ -102,12 +104,12 @@ bool Client::connect(const std::string& host)
|
||||
return d_ptr->connect(host);
|
||||
}
|
||||
|
||||
nlohmann::json Client::get(const nlohmann::json& call)
|
||||
QByteArray Client::get(const QByteArray &call)
|
||||
{
|
||||
return d_ptr->get(call);
|
||||
}
|
||||
|
||||
size_t Client::send(const nlohmann::json& call)
|
||||
size_t Client::send(const QByteArray &call)
|
||||
{
|
||||
return d_ptr->send(call);
|
||||
}
|
||||
|
@ -3,23 +3,26 @@
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace Socket {
|
||||
class ClientPrivate;
|
||||
class Client {
|
||||
std::unique_ptr<ClientPrivate> d_ptr;
|
||||
#include <QJsonDocument>
|
||||
|
||||
public:
|
||||
Client();
|
||||
~Client();
|
||||
bool connect(const std::string& host);
|
||||
nlohmann::json get(const nlohmann::json& call);
|
||||
size_t send(const nlohmann::json& call);
|
||||
void onReadyRead(std::function<void(const std::vector<char>&)> func);
|
||||
void waitForFinished();
|
||||
};
|
||||
} // namespace Socket
|
||||
namespace Socket
|
||||
{
|
||||
class ClientPrivate;
|
||||
class Client
|
||||
{
|
||||
std::unique_ptr<ClientPrivate> d_ptr;
|
||||
|
||||
public:
|
||||
Client();
|
||||
~Client();
|
||||
bool connect(const std::string &host);
|
||||
QByteArray get(const QByteArray &call);
|
||||
size_t send(const QByteArray &call);
|
||||
void onReadyRead(std::function<void(const std::vector<char> &)> func);
|
||||
void waitForFinished();
|
||||
};
|
||||
} // namespace Socket
|
||||
|
||||
#endif /* B1D5EB4F_7645_4BDA_87D6_6B80A4910014 */
|
||||
|
@ -13,46 +13,56 @@
|
||||
|
||||
#define JSON_USE_IMPLICIT_CONVERSIONS 0
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <optional>
|
||||
|
||||
#define tl std
|
||||
|
||||
namespace nlohmann {
|
||||
// namespace nlohmann {
|
||||
|
||||
template<class J, class T>
|
||||
inline void from_json(const J &j, tl::optional<T> &v)
|
||||
{
|
||||
if (j.is_null()) {
|
||||
v = tl::nullopt;
|
||||
} else {
|
||||
v = j.template get<T>();
|
||||
}
|
||||
}
|
||||
// template<class J, class T>
|
||||
// inline void from_json(const J &j, tl::optional<T> &v)
|
||||
// {
|
||||
// if (j.is_null()) {
|
||||
// v = tl::nullopt;
|
||||
// } else {
|
||||
// v = j.template get<T>();
|
||||
// }
|
||||
// }
|
||||
|
||||
template<class J, class T>
|
||||
inline void to_json(J &j, const tl::optional<T> &o)
|
||||
{
|
||||
if (o.has_value()) {
|
||||
j = o.value();
|
||||
}
|
||||
}
|
||||
// template<class J, class T>
|
||||
// inline void to_json(J &j, const tl::optional<T> &o)
|
||||
// {
|
||||
// if (o.has_value()) {
|
||||
// j = o.value();
|
||||
// }
|
||||
// }
|
||||
|
||||
} // namespace nlohmann
|
||||
// } // namespace nlohmann
|
||||
|
||||
namespace linglong {
|
||||
// namespace linglong {
|
||||
|
||||
template<class T>
|
||||
tl::optional<T> optional(const nlohmann::json &j, const char *key)
|
||||
{
|
||||
tl::optional<T> o;
|
||||
auto iter = j.template find(key);
|
||||
if (iter != j.end()) {
|
||||
o = iter->template get<tl::optional<T>>();
|
||||
}
|
||||
return o;
|
||||
}
|
||||
// template<class T>
|
||||
// tl::optional<T> optional(const QJsonObject &j, const char *key)
|
||||
// {
|
||||
// tl::optional<T> o;
|
||||
// auto iter = j.template find(key);
|
||||
// if (iter != j.end()) {
|
||||
// o = iter->template get<tl::optional<T>>();
|
||||
// }
|
||||
// return o;
|
||||
// }
|
||||
|
||||
} // namespace linglong
|
||||
// template<class T>
|
||||
// tl::optional<T> optional(const nlohmann::json &j, const char *key)
|
||||
// {
|
||||
// tl::optional<T> o;
|
||||
// auto iter = j.template find(key);
|
||||
// if (iter != j.end()) {
|
||||
// o = iter->template get<tl::optional<T>>();
|
||||
// }
|
||||
// return o;
|
||||
// }
|
||||
|
||||
// } // namespace linglong
|
||||
|
||||
#endif /* LINGLONG_BOX_SRC_UTIL_JSON_H_ */
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -20,25 +20,25 @@
|
||||
|
||||
#include <fstream>
|
||||
|
||||
namespace linglong {
|
||||
namespace util {
|
||||
namespace json {
|
||||
// namespace linglong {
|
||||
// namespace util {
|
||||
// namespace json {
|
||||
|
||||
inline nlohmann::json fromByteArray(const std::string &content)
|
||||
{
|
||||
return nlohmann::json::parse(content);
|
||||
}
|
||||
// inline nlohmann::json fromByteArray(const std::string &content)
|
||||
// {
|
||||
// return nlohmann::json::parse(content);
|
||||
// }
|
||||
|
||||
inline nlohmann::json fromFile(const std::string &filepath)
|
||||
{
|
||||
std::ifstream f(filepath);
|
||||
std::string str((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
|
||||
auto j = fromByteArray(str);
|
||||
return j;
|
||||
}
|
||||
// inline nlohmann::json fromFile(const std::string &filepath)
|
||||
// {
|
||||
// std::ifstream f(filepath);
|
||||
// std::string str((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
|
||||
// auto j = fromByteArray(str);
|
||||
// return j;
|
||||
// }
|
||||
|
||||
} // namespace json
|
||||
} // namespace util
|
||||
} // namespace linglong
|
||||
// } // namespace json
|
||||
// } // namespace util
|
||||
// } // namespace linglong
|
||||
|
||||
#endif /* LINGLONG_BOX_SRC_UTIL_UTIL_H_ */
|
||||
|
Reference in New Issue
Block a user