33#ifndef _QX_SERIALIZE_QJSON_QHASH_H_
34#define _QX_SERIALIZE_QJSON_QHASH_H_
47#include <QtCore/qjsonvalue.h>
48#include <QtCore/qjsonobject.h>
49#include <QtCore/qjsonarray.h>
50#include <QtCore/qhash.h>
59template <
typename Key,
typename Value>
62 static inline QJsonValue
toJson(
const QHash<Key, Value> & t,
const QString & format)
64 QJsonArray arr; QJsonValue val;
65 QHashIterator<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, QHash<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(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;
102template <
typename Value>
105 static inline QJsonValue
toJson(
const QHash<QString, Value> & t,
const QString & format)
107 QJsonObject obj; QJsonValue val;
108 QHashIterator<QString, Value> itr(t);
110 while (itr.hasNext())
114 obj.insert(itr.key(), val);
117 return QJsonValue(obj);
121template <
typename Value>
124 static inline qx_bool fromJson(
const QJsonValue & j, QHash<QString, Value> & t,
const QString & format)
127 if (! j.isObject()) {
return qx_bool(
true); }
128 QJsonObject obj = j.toObject(); QJsonValue val;
129 t.reserve(obj.count());
131 for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
133 QString key = itr.key(); Value value;
135 t.insert(key, value);
142template <
typename Value>
145 static inline QJsonValue
toJson(
const QHash<std::string, Value> & t,
const QString & format)
147 QJsonObject obj; QJsonValue val;
148 QHashIterator<std::string, Value> itr(t);
150 while (itr.hasNext())
155 QString key = QString::fromStdString(itr.key());
157 std::string s = itr.key();
158 QString key = QString::fromLatin1(s.data(),
int(s.size()));
162 obj.insert(key, val);
165 return QJsonValue(obj);
169template <
typename Value>
172 static inline qx_bool fromJson(
const QJsonValue & j, QHash<std::string, Value> & t,
const QString & format)
175 if (! j.isObject()) {
return qx_bool(
true); }
176 QJsonObject obj = j.toObject(); QJsonValue val;
177 t.reserve(obj.count());
179 for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
181 QString key = itr.key(); Value value;
185 std::string s = key.toStdString();
187 std::string s = key.toLatin1().constData();
197#if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
199template <
typename Value>
202 static inline QJsonValue
toJson(
const QHash<std::wstring, Value> & t,
const QString & format)
204 QJsonObject obj; QJsonValue val;
205 QHashIterator<std::wstring, Value> itr(t);
207 while (itr.hasNext())
211 obj.insert(QString::fromStdWString(itr.key()), val);
214 return QJsonValue(obj);
218template <
typename Value>
221 static inline qx_bool fromJson(
const QJsonValue & j, QHash<std::wstring, Value> & t,
const QString & format)
224 if (! j.isObject()) {
return qx_bool(
true); }
225 QJsonObject obj = j.toObject(); QJsonValue val;
226 t.reserve(obj.count());
228 for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
230 QString key = itr.key(); Value value;
232 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, QHash< Key, Value > &t, const QString &format)
static qx_bool fromJson(const QJsonValue &j, QHash< QString, Value > &t, const QString &format)
static qx_bool fromJson(const QJsonValue &j, QHash< std::string, Value > &t, const QString &format)
static qx_bool fromJson(const QJsonValue &j, QHash< std::wstring, Value > &t, const QString &format)
static QJsonValue toJson(const QHash< Key, Value > &t, const QString &format)
static QJsonValue toJson(const QHash< QString, Value > &t, const QString &format)
static QJsonValue toJson(const QHash< std::string, Value > &t, const QString &format)
static QJsonValue toJson(const QHash< std::wstring, Value > &t, const QString &format)