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:
@ -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