QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches
QxSerializeQJson_boost_unordered_map.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#ifdef _QX_ENABLE_BOOST
34#ifndef _QX_SERIALIZE_QJSON_BOOST_UNORDERED_MAP_H_
35#define _QX_SERIALIZE_QJSON_BOOST_UNORDERED_MAP_H_
36
37#ifdef _MSC_VER
38#pragma once
39#endif
40
48#include <QtCore/qjsonvalue.h>
49#include <QtCore/qjsonobject.h>
50#include <QtCore/qjsonarray.h>
51
52#include <QxConvert/QxConvert.h>
54
55namespace qx {
56namespace cvt {
57namespace detail {
58
59template <typename Key, typename Value>
60struct QxConvert_ToJson< boost::unordered_map<Key, Value> >
61{
62 static inline QJsonValue toJson(const boost::unordered_map<Key, Value> & t, const QString & format)
63 {
64 typedef typename boost::unordered_map<Key, Value>::const_iterator type_itr;
65 QJsonArray arr; QJsonValue val;
66
67 for (type_itr itr = t.begin(); itr != t.end(); ++itr)
68 {
69 QJsonObject obj;
70 val = qx::cvt::to_json(itr->first, format); obj.insert("key", val);
71 val = qx::cvt::to_json(itr->second, 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< boost::unordered_map<Key, Value> >
81{
82 static inline qx_bool fromJson(const QJsonValue & j, boost::unordered_map<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 t.reserve(static_cast<typename boost::unordered_map<Key, Value>::size_type>(arr.count()));
88
89 for (int i = 0; i < arr.count(); i++)
90 {
91 val = arr.at(i); if (! val.isObject()) { continue; }
92 obj = val.toObject(); Key key; Value value;
93 qx::cvt::from_json(obj.value("key"), key, format);
94 qx::cvt::from_json(obj.value("value"), value, format);
95 t.insert(std::make_pair(key, value));
96 }
97
98 return qx_bool(true);
99 }
100};
101
102template <typename Key, typename Value>
103struct QxConvert_ToJson< boost::unordered_multimap<Key, Value> >
104{
105 static inline QJsonValue toJson(const boost::unordered_multimap<Key, Value> & t, const QString & format)
106 {
107 typedef typename boost::unordered_multimap<Key, Value>::const_iterator type_itr;
108 QJsonArray arr; QJsonValue val;
109
110 for (type_itr itr = t.begin(); itr != t.end(); ++itr)
111 {
112 QJsonObject obj;
113 val = qx::cvt::to_json(itr->first, format); obj.insert("key", val);
114 val = qx::cvt::to_json(itr->second, format); obj.insert("value", val);
115 arr.append(obj);
116 }
117
118 return QJsonValue(arr);
119 }
120};
121
122template <typename Key, typename Value>
123struct QxConvert_FromJson< boost::unordered_multimap<Key, Value> >
124{
125 static inline qx_bool fromJson(const QJsonValue & j, boost::unordered_multimap<Key, Value> & t, const QString & format)
126 {
127 t.clear();
128 if (! j.isArray()) { return qx_bool(true); }
129 QJsonArray arr = j.toArray(); QJsonValue val; QJsonObject obj;
130 t.reserve(static_cast<typename boost::unordered_multimap<Key, Value>::size_type>(arr.count()));
131
132 for (int i = 0; i < arr.count(); i++)
133 {
134 val = arr.at(i); if (! val.isObject()) { continue; }
135 obj = val.toObject(); Key key; Value value;
136 qx::cvt::from_json(obj.value("key"), key, format);
137 qx::cvt::from_json(obj.value("value"), value, format);
138 t.insert(std::make_pair(key, value));
139 }
140
141 return qx_bool(true);
142 }
143};
144
145template <typename Value>
146struct QxConvert_ToJson< boost::unordered_map<QString, Value> >
147{
148 static inline QJsonValue toJson(const boost::unordered_map<QString, Value> & t, const QString & format)
149 {
150 typedef typename boost::unordered_map<QString, Value>::const_iterator type_itr;
151 QJsonObject obj; QJsonValue val;
152
153 for (type_itr itr = t.begin(); itr != t.end(); ++itr)
154 {
155 val = qx::cvt::to_json(itr->second, format);
156 obj.insert(itr->first, val);
157 }
158
159 return QJsonValue(obj);
160 }
161};
162
163template <typename Value>
164struct QxConvert_FromJson< boost::unordered_map<QString, Value> >
165{
166 static inline qx_bool fromJson(const QJsonValue & j, boost::unordered_map<QString, Value> & t, const QString & format)
167 {
168 t.clear();
169 if (! j.isObject()) { return qx_bool(true); }
170 QJsonObject obj = j.toObject(); QJsonValue val;
171 t.reserve(static_cast<typename boost::unordered_map<QString, Value>::size_type>(obj.count()));
172
173 for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
174 {
175 QString key = itr.key(); Value value;
176 qx::cvt::from_json(itr.value(), value, format);
177 t.insert(std::make_pair(key, value));
178 }
179
180 return qx_bool(true);
181 }
182};
183
184template <typename Value>
185struct QxConvert_ToJson< boost::unordered_map<std::string, Value> >
186{
187 static inline QJsonValue toJson(const boost::unordered_map<std::string, Value> & t, const QString & format)
188 {
189 typedef typename boost::unordered_map<std::string, Value>::const_iterator type_itr;
190 QJsonObject obj; QJsonValue val;
191
192 for (type_itr itr = t.begin(); itr != t.end(); ++itr)
193 {
194#ifndef QT_NO_STL
195 QString key = QString::fromStdString(itr->first);
196#else // QT_NO_STL
197 QString key = QString::fromLatin1(itr->first.data(), int(itr->first.size()));
198#endif // QT_NO_STL
199
200 val = qx::cvt::to_json(itr->second, format);
201 obj.insert(key, val);
202 }
203
204 return QJsonValue(obj);
205 }
206};
207
208template <typename Value>
209struct QxConvert_FromJson< boost::unordered_map<std::string, Value> >
210{
211 static inline qx_bool fromJson(const QJsonValue & j, boost::unordered_map<std::string, Value> & t, const QString & format)
212 {
213 t.clear();
214 if (! j.isObject()) { return qx_bool(true); }
215 QJsonObject obj = j.toObject(); QJsonValue val;
216 t.reserve(static_cast<typename boost::unordered_map<std::string, Value>::size_type>(obj.count()));
217
218 for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
219 {
220 QString key = itr.key(); Value value;
221 qx::cvt::from_json(itr.value(), value, format);
222
223#ifndef QT_NO_STL
224 std::string s = key.toStdString();
225#else // QT_NO_STL
226 std::string s = key.toLatin1().constData();
227#endif // QT_NO_STL
228
229 t.insert(std::make_pair(s, value));
230 }
231
232 return qx_bool(true);
233 }
234};
235
236#if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
237
238template <typename Value>
239struct QxConvert_ToJson< boost::unordered_map<std::wstring, Value> >
240{
241 static inline QJsonValue toJson(const boost::unordered_map<std::wstring, Value> & t, const QString & format)
242 {
243 typedef typename boost::unordered_map<std::wstring, Value>::const_iterator type_itr;
244 QJsonObject obj; QJsonValue val;
245
246 for (type_itr itr = t.begin(); itr != t.end(); ++itr)
247 {
248 val = qx::cvt::to_json(itr->second, format);
249 obj.insert(QString::fromStdWString(itr->first), val);
250 }
251
252 return QJsonValue(obj);
253 }
254};
255
256template <typename Value>
257struct QxConvert_FromJson< boost::unordered_map<std::wstring, Value> >
258{
259 static inline qx_bool fromJson(const QJsonValue & j, boost::unordered_map<std::wstring, Value> & t, const QString & format)
260 {
261 t.clear();
262 if (! j.isObject()) { return qx_bool(true); }
263 QJsonObject obj = j.toObject(); QJsonValue val;
264 t.reserve(static_cast<typename boost::unordered_map<std::wstring, Value>::size_type>(obj.count()));
265
266 for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
267 {
268 QString key = itr.key(); Value value;
269 qx::cvt::from_json(itr.value(), value, format);
270 t.insert(std::make_pair(key.toStdWString(), value));
271 }
272
273 return qx_bool(true);
274 }
275};
276
277#endif // ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
278
279} // namespace detail
280} // namespace cvt
281} // namespace qx
282
283#endif // _QX_SERIALIZE_QJSON_BOOST_UNORDERED_MAP_H_
284#endif // _QX_ENABLE_BOOST
285#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, boost::unordered_map< Key, Value > &t, const QString &format)
static qx_bool fromJson(const QJsonValue &j, boost::unordered_map< QString, Value > &t, const QString &format)
static qx_bool fromJson(const QJsonValue &j, boost::unordered_map< std::string, Value > &t, const QString &format)
static qx_bool fromJson(const QJsonValue &j, boost::unordered_map< std::wstring, Value > &t, const QString &format)
static qx_bool fromJson(const QJsonValue &j, boost::unordered_multimap< Key, Value > &t, const QString &format)
static QJsonValue toJson(const boost::unordered_map< Key, Value > &t, const QString &format)
static QJsonValue toJson(const boost::unordered_map< QString, Value > &t, const QString &format)
static QJsonValue toJson(const boost::unordered_map< std::string, Value > &t, const QString &format)
static QJsonValue toJson(const boost::unordered_map< std::wstring, Value > &t, const QString &format)
static QJsonValue toJson(const boost::unordered_multimap< Key, Value > &t, const QString &format)