From 586e1e405386420db90d9d1e71a04ca5c0c90701 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 26 Mar 2014 18:27:31 +0100 Subject: [PATCH] refs #192 , enabled CValueObject for JSON * operators * from/toJson method --- src/blackmisc/valueobject.h | 76 +++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/src/blackmisc/valueobject.h b/src/blackmisc/valueobject.h index 6acf2399e..9be6bc705 100644 --- a/src/blackmisc/valueobject.h +++ b/src/blackmisc/valueobject.h @@ -8,6 +8,9 @@ #include #include #include +#include +#include +#include #include #include @@ -196,6 +199,18 @@ namespace BlackMisc */ virtual QVariant toQVariant() const = 0; + /*! + * \brief Contribute to JSON object + * \return updated JSON object + */ + virtual QJsonObject toJson() const { QJsonObject json; return json;} + + /*! + * \brief Initialize from JSOn object + * \param json + */ + virtual void fromJson(const QJsonObject &json) { Q_UNUSED(json); } + /*! * \brief As icon, not implement by all classes * \return @@ -334,6 +349,67 @@ namespace BlackMisc return argument << static_cast(valueObject); } + /*! + * \brief Non member, non friend operator >> for JSON + * \param json + * \param valueObject + * \return + */ + inline const QJsonObject &operator>>(const QJsonObject &json, CValueObject &valueObject) + { + valueObject.fromJson(json); + return json; + } + + /*! + * \brief Non member, non friend operator >> for JSON + * \param json + * \param valueObject + * \return + */ + inline const QJsonValue &operator>>(const QJsonValue &json, CValueObject &valueObject) + { + valueObject.fromJson(json.toObject()); + return json; + } + + /*! + * \brief Non member, non friend operator >> for JSON + * \param json + * \param valueObject + * \return + */ + inline const QJsonValueRef &operator>>(const QJsonValueRef &json, CValueObject &valueObject) + { + valueObject.fromJson(json.toObject()); + return json; + } + + /*! + * \brief Non member, non friend operator >> for JSON + * \param json + * \param value + * \return + */ + inline QJsonArray &operator<<(QJsonArray &json, const CValueObject &value) + { + json.append(value.toJson()); + return json; + } + + /*! + * \brief Non member, non friend operator >> for JSON + * \param json + * \param value + * \return + */ + template typename std::enable_if::value, QJsonObject>::type & + operator<<(QJsonObject &json, const std::pair &value) + { + json.insert(value.first, QJsonValue(value.second.toJson())); + return json; + } + /*! * Allow comparison with QVariant, e.g. QVariant == CFrequency ? * \param variant