Skip to content

Commit e062636

Browse files
committed
QJsonObject conversions are added to library
1 parent 441efef commit e062636

File tree

3 files changed

+59
-27
lines changed

3 files changed

+59
-27
lines changed

inc/taggedjsonarray.h

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
namespace {
1818
template<typename T>
19-
std::vector<T> extractFromQJSONArray(QJsonArray arr, const bool checkValue)
19+
std::vector<T> extractFromQJSONArray(const QJsonArray& arr, const bool checkValue)
2020
{
2121
std::vector<T> out;
2222
out.reserve(arr.size());
@@ -45,6 +45,9 @@ template<typename T>
4545
class TaggedJSONArray <T, typename std::enable_if_t<TJO_JSON_COMPATIBLE>>
4646
{
4747
public:
48+
//! Default constructor, which is useful if the parameters planned to be filled later
49+
explicit TaggedJSONArray() {}
50+
4851
/*!
4952
* \brief TaggedJSONArray constructor variant that takes QJsonArray input directly
5053
* \param val target JSON array data
@@ -66,11 +69,11 @@ class TaggedJSONArray <T, typename std::enable_if_t<TJO_JSON_COMPATIBLE>>
6669

6770
//! Implicit value constructor for the tagged object constructor
6871
template<typename V, typename = std::enable_if_t<std::is_convertible_v<V, QJsonArray>>>
69-
TaggedJSONArray(V&& val) { m_arr = std::forward<V>(val); };
72+
TaggedJSONArray(V&& val) : m_arr(std::forward<V>(val)) {};
7073

7174
//! Assignment operator setter
7275
template<typename V, typename = std::enable_if_t<std::is_convertible_v<V, QJsonArray>>>
73-
void operator=(V&& val) { m_arr = std::forward<V>(val); };
76+
TaggedJSONArray& operator=(V&& val) { m_arr = std::forward<V>(val); };
7477

7578
//Operator overloads
7679
bool operator!=(const QJsonArray& other) const { return m_arr != other; };
@@ -161,6 +164,8 @@ class TaggedJSONArray <T, typename std::enable_if_t<TJO_JSON_COMPATIBLE>>
161164
return ret;
162165
}
163166

167+
QJsonValue toJsonValue() const {return m_arr;}
168+
164169
private:
165170
QJsonArray m_arr;
166171

@@ -220,6 +225,9 @@ template<typename T>
220225
class TaggedJSONArray<T, typename std::enable_if_t<TJO_IS_TAGGED_OBJECT>>
221226
{
222227
public:
228+
//! Default constructor, which is useful if the parameters planned to be filled later
229+
explicit TaggedJSONArray() : m_arr() {}
230+
223231
/*!
224232
* @brief Main constructor that is intended to be used with the #TJO_DEFINE_JSON_TAGGED_OBJECT() macro.
225233
* @param ref QJsonValue that holds the array of predefined JSON objects.
@@ -234,11 +242,11 @@ class TaggedJSONArray<T, typename std::enable_if_t<TJO_IS_TAGGED_OBJECT>>
234242

235243
//! Implicit value constructor for the tagged object constructor
236244
template<typename V, typename = std::enable_if_t<std::is_convertible_v<V, std::vector<T>>>>
237-
TaggedJSONArray(V&& val) { m_arr = std::forward<V>(val); };
245+
TaggedJSONArray(V&& val) : m_arr(std::forward<V>(val)) {};
238246

239247
//!Assignment operator to get the whole container data
240248
template<typename V, typename = std::enable_if_t<std::is_convertible_v<V, std::vector<T>>>>
241-
void operator=(V&& val) { m_arr = std::forward<V>(val); };
249+
TaggedJSONArray& operator=(V&& val) { m_arr = std::forward<V>(val); };
242250

243251
bool operator!=(const std::vector<T>& other) const { return m_arr != other; };
244252
bool operator==(const std::vector<T>& other) const { return m_arr == other; };
@@ -271,7 +279,17 @@ class TaggedJSONArray<T, typename std::enable_if_t<TJO_IS_TAGGED_OBJECT>>
271279
};
272280

273281
//!QVector access
274-
QVector<T> toQVector() const { return QVector::fromStdVector(m_arr); }
282+
QVector<T> toQVector() const { return QVector<T>::fromStdVector(m_arr); }
283+
284+
//!Converts the holding values back to a QJsonArray that holds each TaggedObject information
285+
QJsonValue toJsonValue() const
286+
{
287+
QJsonArray ret;
288+
for(const T& curObj : m_arr){
289+
ret.append(curObj.toJsonValue());
290+
}
291+
return ret;
292+
}
275293

276294
private:
277295
std::vector<T> m_arr;

inc/taggedjsonobject.h

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,20 @@ template <typename T, typename=std::enable_if_t<TJO_JSON_COMPATIBLE>>
1919
class TaggedJSONObject
2020
{
2121
public:
22+
//! Default constructor, which is useful if the parameters planned to be filled later
23+
explicit TaggedJSONObject() : m_value() {}
24+
2225
/*!
2326
* \brief TaggedJSONObject Constructor that takes the JSON data and stores it according to the template argument.
2427
* \param val Target JSON value to be stored.
2528
* \param checkValue If set to true, invalid conversions (missing value, wrong type etc.) will throw a runtime error.
2629
*/
27-
explicit TaggedJSONObject(QJsonValue val, const bool checkValue=true)
28-
{
29-
//Check if there is a valid data if it's intended
30-
if(checkValue && val.isUndefined())
31-
throw(std::runtime_error("Invalid data has been encountered while parsing the json data for TaggedJSONObject"));
32-
33-
dispatchValue(std::move(val));
34-
};
30+
explicit TaggedJSONObject(const QJsonValue& val, const bool checkValue=true) : m_value(dispatchValue(val, checkValue))
31+
{};
3532

3633
//! Implicit value constructor for the tagged object constructor
3734
template<typename V, typename = std::enable_if_t<std::is_convertible_v<V, T>>>
38-
TaggedJSONObject(V&& val) { m_value = std::forward<V>(val); };
35+
TaggedJSONObject(V&& val) : m_value(std::forward<V>(val)) {};
3936

4037
/*!
4138
* \brief l-value reference getter for the contained object
@@ -86,7 +83,7 @@ class TaggedJSONObject
8683
* \brief operator -> Shortcut for object values (QString, QVariant etc.) methods.
8784
* \return The contained object
8885
*/
89-
const T* const operator->() const {return &m_value;};
86+
const T* operator->() const {return &m_value;};
9087

9188
//! Shortcut for value operations (for QJsonValue and QJsonObjects only)
9289
template<typename S=T, typename=std::enable_if_t<std::is_same_v<S, QJsonValue> || std::is_same_v<S, QJsonObject>>>
@@ -106,28 +103,34 @@ class TaggedJSONObject
106103
return QString(m_value);
107104
};
108105

106+
QJsonValue toJsonValue() const {return QJsonValue{m_value};}
107+
109108
private:
110109
T m_value;
111110

112111
template<class TO>
113112
friend std::ostream& operator<< (std::ostream& stream, const TaggedJSONObject<TO>& obj);
114113

115114
//Determine the function to be called at compile time based on the current template parameter
116-
void dispatchValue(QJsonValue&& val){
115+
T dispatchValue(const QJsonValue& val, const bool checkValue){
116+
//Check if there is a valid data if it's intended
117+
if(checkValue && val.isUndefined())
118+
throw(std::runtime_error("Invalid data has been encountered while parsing the json data for TaggedJSONObject"));
119+
117120
if constexpr(std::is_same_v<T, bool>)
118-
m_value = val.toBool();
121+
return val.toBool();
119122
else if constexpr(std::is_integral_v<T>)
120-
m_value = val.toInt();
123+
return val.toInt();
121124
else if constexpr(std::is_floating_point_v<T>)
122-
m_value = val.toDouble();
125+
return val.toDouble();
123126
else if constexpr(std::is_same_v<T, QJsonValue>)
124-
m_value = std::move(val);
127+
return val;
125128
else if constexpr(std::is_same_v<T, QJsonObject>)
126-
m_value = val.toObject();
129+
return val.toObject();
127130
else if constexpr(std::is_same_v<T, QString>)
128-
m_value = val.toString();
131+
return val.toString();
129132
else if constexpr(std::is_same_v<T, QVariant>)
130-
m_value = val.toVariant();
133+
return val.toVariant();
131134
else
132135
throw(std::invalid_argument("Template argument for the TaggedJSONObject is not valid"));
133136
}

inc/taggedjsonobjectmacros.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
#include <QJsonObject>
88
#include <QByteArray>
99

10+
#define TAGGEDOBJECTMACRO_PREPARE_JSON_OBJECT(type, name) ret[#name] = name.toJsonValue();
11+
#define TAGGEDOBJECTMACRO_PREPARE_JSON_OBJECT_UNPACK(pair) TAGGEDOBJECTMACRO_PREPARE_JSON_OBJECT pair
12+
1013
#define TAGGEDOBJECTMACRO_DECLARE_MEMBER(type, name) type name;
1114
#define TAGGEDOBJECTMACRO_DECLARE_MEMBER_UNPACK(pair) TAGGEDOBJECTMACRO_DECLARE_MEMBER pair
1215

13-
#define TAGGEDOBJECTMACRO_INITIALIZE_MEMBER(type, name) name(val.take(#name), checkValues)
16+
#define TAGGEDOBJECTMACRO_INITIALIZE_MEMBER(type, name) name(obj[#name], checkValues)
1417
#define TAGGEDOBJECTMACRO_INITIALIZE_MEMBER_UNPACK(pair) TAGGEDOBJECTMACRO_INITIALIZE_MEMBER pair
1518

1619
#define TAGGEDOBJECTMACRO_INITIALIZE_MEMBER_VALUE(type, name) name(val[#name], checkValues)
@@ -49,11 +52,19 @@ respective JSON data, a runtime error will be raised.
4952
#define TJO_DEFINE_JSON_TAGGED_OBJECT(CLASS_NAME, ...) \
5053
class CLASS_NAME{\
5154
public:\
52-
explicit CLASS_NAME(QJsonObject val, const bool checkValues=true) : MAP_LIST(TAGGEDOBJECTMACRO_INITIALIZE_MEMBER_UNPACK, __VA_ARGS__) {}; \
53-
explicit CLASS_NAME(QJsonValue val, const bool checkValues=true) : MAP_LIST(TAGGEDOBJECTMACRO_INITIALIZE_MEMBER_VALUE_UNPACK, __VA_ARGS__) {}; \
55+
explicit CLASS_NAME() {}\
56+
explicit CLASS_NAME(const QJsonObject& obj, const bool checkValues=true) : MAP_LIST(TAGGEDOBJECTMACRO_INITIALIZE_MEMBER_UNPACK, __VA_ARGS__) {}; \
57+
explicit CLASS_NAME(const QJsonValue& val, const bool checkValues=true) : MAP_LIST(TAGGEDOBJECTMACRO_INITIALIZE_MEMBER_VALUE_UNPACK, __VA_ARGS__) {}; \
5458
explicit CLASS_NAME(const QByteArray& json, const bool checkValues=true) : CLASS_NAME(TaggedObject::getJSONObjectFromJSONText(json), checkValues) {};\
5559
explicit CLASS_NAME(const QString& filePath, const bool checkValues=true) : CLASS_NAME(TaggedObject::getJSONObjectFromFile(filePath), checkValues) {};\
5660
explicit CLASS_NAME(MAP_LIST(TAGGEDOBJECTMACRO_LIST_MEMBERS_UNPACK, __VA_ARGS__), const bool checkValues=true) : MAP_LIST(TAGGEDOBJECTMACRO_MOVE_PARAMETERS_UNPACK, __VA_ARGS__) {};\
61+
QJsonObject toJsonObject() const\
62+
{\
63+
QJsonObject ret;\
64+
MAP(TAGGEDOBJECTMACRO_PREPARE_JSON_OBJECT_UNPACK, __VA_ARGS__)\
65+
return ret;\
66+
}\
67+
QJsonValue toJsonValue() const { return toJsonObject(); }\
5768
MAP(TAGGEDOBJECTMACRO_DECLARE_MEMBER_UNPACK, __VA_ARGS__)\
5869
};
5970

0 commit comments

Comments
 (0)