1
0
mirror of https://github.com/jaredtao/TaoQuick.git synced 2025-01-19 20:22:56 +08:00
TaoQuick/3rdparty/TaoCommon/Common/JsonSerialize.h
2020-11-13 13:35:42 +08:00

78 lines
2.1 KiB
C
Raw Blame History

#pragma once
#include "CIMCommDef.h"
//json <20><><EFBFBD>л<EFBFBD>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QVariant>
#include <QVariantList>
#include <QVariantMap>
///@brief <20><><EFBFBD>л<EFBFBD>
#define JsonSerialize_Begin() \
public: \
operator QVariant() const { \
return QVariant::fromValue(this->operator QVariantMap());\
} \
operator QJsonObject() const { \
return QJsonObject::fromVariantMap(this->operator QVariantMap());\
} \
operator QVariantMap() const { \
QVariantMap vmap;
#define JsonSerialize_End() \
return vmap; \
}
///@brief <20><><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD>
#define JsonDeserialize_Begin(class_name) \
public:\
class_name(const QJsonObject& other)\
{\
QVariantMap vmap = other.toVariantMap();
#define JsonDeserialize_End() \
}
///@brief <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>л<EFBFBD>
#define JsonPartialDeserialize_Begin(class_name)\
public:\
class_name& operator=(const QJsonObject& other)\
{\
QVariantMap vmap = other.toVariantMap();
#define JsonPartialDeserialize_End()\
return *this;\
}\
///@brief <20><><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD><D3B3>
#define JsonPureProperty(val) \
vmap.unite((QVariantMap)val);
#define JsonProperty(name, val, atom_type) \
vmap[name] = QVariant::fromValue<atom_type>(val);\
if(vmap[name].isNull()) vmap.remove(name);
#define JsonContainerProperty(name, val, atom_type)\
{\
QVariantList lst;\
for(atom_type t : val)\
{\
lst << QVariant::fromValue<atom_type>(t);\
}\
vmap[name] = lst;\
}\
#define JsonDeserializeContainerProperty(name, val, atom_type)\
if(vmap[name].canConvert<QVariantList>())\
{\
QSequentialIterable iterable = vmap[name].value<QSequentialIterable>();\
Q_FOREACH(const QVariant &v, iterable) \
{\
val.push_back(v.value<atom_type>());\
}\
}
#define JsonDeserializeProperty(name, val, type)\
val = vmap[name].value<type>();