33#ifdef _QX_ENABLE_BOOST
34#ifndef _QX_SERIALIZE_QJSON_BOOST_UNORDERED_MAP_H_
35#define _QX_SERIALIZE_QJSON_BOOST_UNORDERED_MAP_H_
48#include <QtCore/qjsonvalue.h>
49#include <QtCore/qjsonobject.h>
50#include <QtCore/qjsonarray.h>
59template <
typename Key,
typename Value>
62 static inline QJsonValue
toJson(
const boost::unordered_map<Key, Value> & t,
const QString & format)
64 typedef typename boost::unordered_map<Key, Value>::const_iterator type_itr;
65 QJsonArray arr; QJsonValue val;
67 for (type_itr itr = t.begin(); itr != t.end(); ++itr)
75 return QJsonValue(arr);
79template <
typename Key,
typename Value>
82 static inline qx_bool fromJson(
const QJsonValue & j, boost::unordered_map<Key, Value> & t,
const QString & format)
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()));
89 for (
int i = 0; i < arr.count(); i++)
91 val = arr.at(i);
if (! val.isObject()) {
continue; }
92 obj = val.toObject(); Key key; Value value;
95 t.insert(std::make_pair(key, value));
102template <
typename Key,
typename Value>
105 static inline QJsonValue
toJson(
const boost::unordered_multimap<Key, Value> & t,
const QString & format)
107 typedef typename boost::unordered_multimap<Key, Value>::const_iterator type_itr;
108 QJsonArray arr; QJsonValue val;
110 for (type_itr itr = t.begin(); itr != t.end(); ++itr)
118 return QJsonValue(arr);
122template <
typename Key,
typename Value>
125 static inline qx_bool fromJson(
const QJsonValue & j, boost::unordered_multimap<Key, Value> & t,
const QString & format)
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()));
132 for (
int i = 0; i < arr.count(); i++)
134 val = arr.at(i);
if (! val.isObject()) {
continue; }
135 obj = val.toObject(); Key key; Value value;
138 t.insert(std::make_pair(key, value));
145template <
typename Value>
148 static inline QJsonValue
toJson(
const boost::unordered_map<QString, Value> & t,
const QString & format)
150 typedef typename boost::unordered_map<QString, Value>::const_iterator type_itr;
151 QJsonObject obj; QJsonValue val;
153 for (type_itr itr = t.begin(); itr != t.end(); ++itr)
156 obj.insert(itr->first, val);
159 return QJsonValue(obj);
163template <
typename Value>
166 static inline qx_bool fromJson(
const QJsonValue & j, boost::unordered_map<QString, Value> & t,
const QString & format)
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()));
173 for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
175 QString key = itr.key(); Value value;
177 t.insert(std::make_pair(key, value));
184template <
typename Value>
187 static inline QJsonValue
toJson(
const boost::unordered_map<std::string, Value> & t,
const QString & format)
189 typedef typename boost::unordered_map<std::string, Value>::const_iterator type_itr;
190 QJsonObject obj; QJsonValue val;
192 for (type_itr itr = t.begin(); itr != t.end(); ++itr)
195 QString key = QString::fromStdString(itr->first);
197 QString key = QString::fromLatin1(itr->first.data(),
int(itr->first.size()));
201 obj.insert(key, val);
204 return QJsonValue(obj);
208template <
typename Value>
211 static inline qx_bool fromJson(
const QJsonValue & j, boost::unordered_map<std::string, Value> & t,
const QString & format)
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()));
218 for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
220 QString key = itr.key(); Value value;
224 std::string s = key.toStdString();
226 std::string s = key.toLatin1().constData();
229 t.insert(std::make_pair(s, value));
236#if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
238template <
typename Value>
241 static inline QJsonValue
toJson(
const boost::unordered_map<std::wstring, Value> & t,
const QString & format)
243 typedef typename boost::unordered_map<std::wstring, Value>::const_iterator type_itr;
244 QJsonObject obj; QJsonValue val;
246 for (type_itr itr = t.begin(); itr != t.end(); ++itr)
249 obj.insert(QString::fromStdWString(itr->first), val);
252 return QJsonValue(obj);
256template <
typename Value>
259 static inline qx_bool fromJson(
const QJsonValue & j, boost::unordered_map<std::wstring, Value> & t,
const QString & format)
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()));
266 for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
268 QString key = itr.key(); Value value;
270 t.insert(std::make_pair(key.toStdWString(), value));
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
qx_bool from_json(const QJsonValue &j, T &t, const QString &format=QString())
QJsonValue to_json(const T &t, const QString &format=QString())
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)