33#ifndef _QX_SERIALIZE_QJSON_STD_MAP_H_
34#define _QX_SERIALIZE_QJSON_STD_MAP_H_
47#include <QtCore/qjsonvalue.h>
48#include <QtCore/qjsonobject.h>
49#include <QtCore/qjsonarray.h>
60template <
typename Key,
typename Value>
63 static inline QJsonValue
toJson(
const std::map<Key, Value> & t,
const QString & format)
65 typedef typename std::map<Key, Value>::const_iterator type_itr;
66 QJsonArray arr; QJsonValue val;
68 for (type_itr itr = t.begin(); itr != t.end(); ++itr)
76 return QJsonValue(arr);
80template <
typename Key,
typename Value>
83 static inline qx_bool fromJson(
const QJsonValue & j, std::map<Key, Value> & t,
const QString & format)
86 if (! j.isArray()) {
return qx_bool(
true); }
87 QJsonArray arr = j.toArray(); QJsonValue val; QJsonObject obj;
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 Value>
105 static inline QJsonValue
toJson(
const std::map<QString, Value> & t,
const QString & format)
107 typedef typename std::map<QString, Value>::const_iterator type_itr;
108 QJsonObject obj; QJsonValue val;
110 for (type_itr itr = t.begin(); itr != t.end(); ++itr)
113 obj.insert(itr->first, val);
116 return QJsonValue(obj);
120template <
typename Value>
123 static inline qx_bool fromJson(
const QJsonValue & j, std::map<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(std::make_pair(key, value));
140template <
typename Value>
143 static inline QJsonValue
toJson(
const std::map<std::string, Value> & t,
const QString & format)
145 typedef typename std::map<std::string, Value>::const_iterator type_itr;
146 QJsonObject obj; QJsonValue val;
148 for (type_itr itr = t.begin(); itr != t.end(); ++itr)
151 QString key = QString::fromStdString(itr->first);
153 std::string s = itr->first;
154 QString key = QString::fromLatin1(s.data(),
int(s.size()));
158 obj.insert(key, val);
161 return QJsonValue(obj);
165template <
typename Value>
168 static inline qx_bool fromJson(
const QJsonValue & j, std::map<std::string, Value> & t,
const QString & format)
171 if (! j.isObject()) {
return qx_bool(
true); }
172 QJsonObject obj = j.toObject(); QJsonValue val;
174 for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
176 QString key = itr.key(); Value value;
180 std::string s = key.toStdString();
182 std::string s = key.toLatin1().constData();
185 t.insert(std::make_pair(s, value));
192#if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
194template <
typename Value>
197 static inline QJsonValue
toJson(
const std::map<std::wstring, Value> & t,
const QString & format)
199 typedef typename std::map<std::wstring, Value>::const_iterator type_itr;
200 QJsonObject obj; QJsonValue val;
202 for (type_itr itr = t.begin(); itr != t.end(); ++itr)
205 obj.insert(QString::fromStdWString(itr->first), val);
208 return QJsonValue(obj);
212template <
typename Value>
215 static inline qx_bool fromJson(
const QJsonValue & j, std::map<std::wstring, Value> & t,
const QString & format)
218 if (! j.isObject()) {
return qx_bool(
true); }
219 QJsonObject obj = j.toObject(); QJsonValue val;
221 for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
223 QString key = itr.key(); Value value;
225 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, std::map< Key, Value > &t, const QString &format)
static qx_bool fromJson(const QJsonValue &j, std::map< QString, Value > &t, const QString &format)
static qx_bool fromJson(const QJsonValue &j, std::map< std::string, Value > &t, const QString &format)
static qx_bool fromJson(const QJsonValue &j, std::map< std::wstring, Value > &t, const QString &format)
static QJsonValue toJson(const std::map< Key, Value > &t, const QString &format)
static QJsonValue toJson(const std::map< QString, Value > &t, const QString &format)
static QJsonValue toJson(const std::map< std::string, Value > &t, const QString &format)
static QJsonValue toJson(const std::map< std::wstring, Value > &t, const QString &format)