33#ifndef _QX_SERIALIZE_QJSON_QMAP_H_
34#define _QX_SERIALIZE_QJSON_QMAP_H_
47#include <QtCore/qjsonvalue.h>
48#include <QtCore/qjsonobject.h>
49#include <QtCore/qjsonarray.h>
50#include <QtCore/qmap.h>
59template <
typename Key,
typename Value>
62 static inline QJsonValue
toJson(
const QMap<Key, Value> & t,
const QString & format)
64 QJsonArray arr; QJsonValue val;
65 QMapIterator<Key, Value> itr(t);
69 itr.next(); QJsonObject obj;
75 return QJsonValue(arr);
79template <
typename Key,
typename Value>
82 static inline qx_bool fromJson(
const QJsonValue & j, QMap<Key, Value> & t,
const QString & format)
85 if (! j.isArray()) {
return qx_bool(
true); }
86 QJsonArray arr = j.toArray(); QJsonValue val; QJsonObject obj;
88 for (
int i = 0; i < arr.count(); i++)
90 val = arr.at(i);
if (! val.isObject()) {
continue; }
91 obj = val.toObject(); Key key; Value value;
101template <
typename Value>
104 static inline QJsonValue
toJson(
const QMap<QString, Value> & t,
const QString & format)
106 QJsonObject obj; QJsonValue val;
107 QMapIterator<QString, Value> itr(t);
109 while (itr.hasNext())
113 obj.insert(itr.key(), val);
116 return QJsonValue(obj);
120template <
typename Value>
123 static inline qx_bool fromJson(
const QJsonValue & j, QMap<QString, Value> & t,
const QString & format)
126 if (! j.isObject()) {
return qx_bool(
true); }
127 QJsonObject obj = j.toObject(); QJsonValue val;
129 for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
131 QString key = itr.key(); Value value;
133 t.insert(key, value);
140template <
typename Value>
143 static inline QJsonValue
toJson(
const QMap<std::string, Value> & t,
const QString & format)
145 QJsonObject obj; QJsonValue val;
146 QMapIterator<std::string, Value> itr(t);
148 while (itr.hasNext())
153 QString key = QString::fromStdString(itr.key());
155 std::string s = itr.key();
156 QString key = QString::fromLatin1(s.data(),
int(s.size()));
160 obj.insert(key, val);
163 return QJsonValue(obj);
167template <
typename Value>
170 static inline qx_bool fromJson(
const QJsonValue & j, QMap<std::string, Value> & t,
const QString & format)
173 if (! j.isObject()) {
return qx_bool(
true); }
174 QJsonObject obj = j.toObject(); QJsonValue val;
176 for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
178 QString key = itr.key(); Value value;
182 std::string s = key.toStdString();
184 std::string s = key.toLatin1().constData();
194#if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
196template <
typename Value>
199 static inline QJsonValue
toJson(
const QMap<std::wstring, Value> & t,
const QString & format)
201 QJsonObject obj; QJsonValue val;
202 QMapIterator<std::wstring, Value> itr(t);
204 while (itr.hasNext())
208 obj.insert(QString::fromStdWString(itr.key()), val);
211 return QJsonValue(obj);
215template <
typename Value>
218 static inline qx_bool fromJson(
const QJsonValue & j, QMap<std::wstring, Value> & t,
const QString & format)
221 if (! j.isObject()) {
return qx_bool(
true); }
222 QJsonObject obj = j.toObject(); QJsonValue val;
224 for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
226 QString key = itr.key(); Value value;
228 t.insert(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, 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)