QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches
QxSerializeQJson_QMap.h
Go to the documentation of this file.
1/****************************************************************************
2**
3** https://www.qxorm.com/
4** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
5**
6** This file is part of the QxOrm library
7**
8** This software is provided 'as-is', without any express or implied
9** warranty. In no event will the authors be held liable for any
10** damages arising from the use of this software
11**
12** Commercial Usage
13** Licensees holding valid commercial QxOrm licenses may use this file in
14** accordance with the commercial license agreement provided with the
15** Software or, alternatively, in accordance with the terms contained in
16** a written agreement between you and Lionel Marty
17**
18** GNU General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU
20** General Public License version 3.0 as published by the Free Software
21** Foundation and appearing in the file 'license.gpl3.txt' included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU General Public License version 3.0 requirements will be
24** met : http://www.gnu.org/copyleft/gpl.html
25**
26** If you are unsure which license is appropriate for your use, or
27** if you have questions regarding the use of this file, please contact :
28** contact@qxorm.com
29**
30****************************************************************************/
31
32#ifndef _QX_NO_JSON
33#ifndef _QX_SERIALIZE_QJSON_QMAP_H_
34#define _QX_SERIALIZE_QJSON_QMAP_H_
35
36#ifdef _MSC_VER
37#pragma once
38#endif
39
47#include <QtCore/qjsonvalue.h>
48#include <QtCore/qjsonobject.h>
49#include <QtCore/qjsonarray.h>
50#include <QtCore/qmap.h>
51
52#include <QxConvert/QxConvert.h>
54
55namespace qx {
56namespace cvt {
57namespace detail {
58
59template <typename Key, typename Value>
60struct QxConvert_ToJson< QMap<Key, Value> >
61{
62 static inline QJsonValue toJson(const QMap<Key, Value> & t, const QString & format)
63 {
64 QJsonArray arr; QJsonValue val;
65 QMapIterator<Key, Value> itr(t);
66
67 while (itr.hasNext())
68 {
69 itr.next(); QJsonObject obj;
70 val = qx::cvt::to_json(itr.key(), format); obj.insert("key", val);
71 val = qx::cvt::to_json(itr.value(), format); obj.insert("value", val);
72 arr.append(obj);
73 }
74
75 return QJsonValue(arr);
76 }
77};
78
79template <typename Key, typename Value>
80struct QxConvert_FromJson< QMap<Key, Value> >
81{
82 static inline qx_bool fromJson(const QJsonValue & j, QMap<Key, Value> & t, const QString & format)
83 {
84 t.clear();
85 if (! j.isArray()) { return qx_bool(true); }
86 QJsonArray arr = j.toArray(); QJsonValue val; QJsonObject obj;
87
88 for (int i = 0; i < arr.count(); i++)
89 {
90 val = arr.at(i); if (! val.isObject()) { continue; }
91 obj = val.toObject(); Key key; Value value;
92 qx::cvt::from_json(obj.value("key"), key, format);
93 qx::cvt::from_json(obj.value("value"), value, format);
94 t.insert(key, value);
95 }
96
97 return qx_bool(true);
98 }
99};
100
101template <typename Value>
102struct QxConvert_ToJson< QMap<QString, Value> >
103{
104 static inline QJsonValue toJson(const QMap<QString, Value> & t, const QString & format)
105 {
106 QJsonObject obj; QJsonValue val;
107 QMapIterator<QString, Value> itr(t);
108
109 while (itr.hasNext())
110 {
111 itr.next();
112 val = qx::cvt::to_json(itr.value(), format);
113 obj.insert(itr.key(), val);
114 }
115
116 return QJsonValue(obj);
117 }
118};
119
120template <typename Value>
121struct QxConvert_FromJson< QMap<QString, Value> >
122{
123 static inline qx_bool fromJson(const QJsonValue & j, QMap<QString, Value> & t, const QString & format)
124 {
125 t.clear();
126 if (! j.isObject()) { return qx_bool(true); }
127 QJsonObject obj = j.toObject(); QJsonValue val;
128
129 for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
130 {
131 QString key = itr.key(); Value value;
132 qx::cvt::from_json(itr.value(), value, format);
133 t.insert(key, value);
134 }
135
136 return qx_bool(true);
137 }
138};
139
140template <typename Value>
141struct QxConvert_ToJson< QMap<std::string, Value> >
142{
143 static inline QJsonValue toJson(const QMap<std::string, Value> & t, const QString & format)
144 {
145 QJsonObject obj; QJsonValue val;
146 QMapIterator<std::string, Value> itr(t);
147
148 while (itr.hasNext())
149 {
150 itr.next();
151
152#ifndef QT_NO_STL
153 QString key = QString::fromStdString(itr.key());
154#else // QT_NO_STL
155 std::string s = itr.key();
156 QString key = QString::fromLatin1(s.data(), int(s.size()));
157#endif // QT_NO_STL
158
159 val = qx::cvt::to_json(itr.value(), format);
160 obj.insert(key, val);
161 }
162
163 return QJsonValue(obj);
164 }
165};
166
167template <typename Value>
168struct QxConvert_FromJson< QMap<std::string, Value> >
169{
170 static inline qx_bool fromJson(const QJsonValue & j, QMap<std::string, Value> & t, const QString & format)
171 {
172 t.clear();
173 if (! j.isObject()) { return qx_bool(true); }
174 QJsonObject obj = j.toObject(); QJsonValue val;
175
176 for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
177 {
178 QString key = itr.key(); Value value;
179 qx::cvt::from_json(itr.value(), value, format);
180
181#ifndef QT_NO_STL
182 std::string s = key.toStdString();
183#else // QT_NO_STL
184 std::string s = key.toLatin1().constData();
185#endif // QT_NO_STL
186
187 t.insert(s, value);
188 }
189
190 return qx_bool(true);
191 }
192};
193
194#if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
195
196template <typename Value>
197struct QxConvert_ToJson< QMap<std::wstring, Value> >
198{
199 static inline QJsonValue toJson(const QMap<std::wstring, Value> & t, const QString & format)
200 {
201 QJsonObject obj; QJsonValue val;
202 QMapIterator<std::wstring, Value> itr(t);
203
204 while (itr.hasNext())
205 {
206 itr.next();
207 val = qx::cvt::to_json(itr.value(), format);
208 obj.insert(QString::fromStdWString(itr.key()), val);
209 }
210
211 return QJsonValue(obj);
212 }
213};
214
215template <typename Value>
216struct QxConvert_FromJson< QMap<std::wstring, Value> >
217{
218 static inline qx_bool fromJson(const QJsonValue & j, QMap<std::wstring, Value> & t, const QString & format)
219 {
220 t.clear();
221 if (! j.isObject()) { return qx_bool(true); }
222 QJsonObject obj = j.toObject(); QJsonValue val;
223
224 for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
225 {
226 QString key = itr.key(); Value value;
227 qx::cvt::from_json(itr.value(), value, format);
228 t.insert(key.toStdWString(), value);
229 }
230
231 return qx_bool(true);
232 }
233};
234
235#endif // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
236
237} // namespace detail
238} // namespace cvt
239} // namespace qx
240
241#endif // _QX_SERIALIZE_QJSON_QMAP_H_
242#endif // _QX_NO_JSON
qx::QxBool qx_bool
Definition QxBool.h:150
qx::cvt : namespace to provide global functions to convert any kind of objects to/from QString and QV...
qx_bool : boolean type with code and description message when an error occured
Definition QxBool.h:71
qx_bool from_json(const QJsonValue &j, T &t, const QString &format=QString())
Definition QxConvert.h:84
QJsonValue to_json(const T &t, const QString &format=QString())
Definition QxConvert.h:83
Root namespace for all QxOrm library features.
static qx_bool fromJson(const QJsonValue &j, QMap< Key, Value > &t, const QString &format)
static qx_bool fromJson(const QJsonValue &j, QMap< QString, Value > &t, const QString &format)
static qx_bool fromJson(const QJsonValue &j, QMap< std::string, Value > &t, const QString &format)
static qx_bool fromJson(const QJsonValue &j, QMap< std::wstring, Value > &t, const QString &format)
static QJsonValue toJson(const QMap< Key, Value > &t, const QString &format)
static QJsonValue toJson(const QMap< QString, Value > &t, const QString &format)
static QJsonValue toJson(const QMap< std::string, Value > &t, const QString &format)
static QJsonValue toJson(const QMap< std::wstring, Value > &t, const QString &format)