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