33#ifndef _QX_SERIALIZE_QJSON_QX_COLLECTION_H_
34#define _QX_SERIALIZE_QJSON_QX_COLLECTION_H_
47#include <QtCore/qjsonvalue.h>
48#include <QtCore/qjsonobject.h>
49#include <QtCore/qjsonarray.h>
60template <
typename Key,
typename Value>
65 QJsonArray arr; QJsonValue val;
67 for (
long l = 0; l < t.
count(); l++)
75 return QJsonValue(arr);
79template <
typename Key,
typename Value>
85 if (! j.isArray()) {
return qx_bool(
true); }
86 QJsonArray arr = j.toArray(); QJsonValue val; QJsonObject obj;
87 t.
reserve(
static_cast<long>(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>
107 QJsonObject obj; QJsonValue val;
109 for (
long l = 0; l < t.
count(); l++)
115 return QJsonValue(obj);
119template <
typename Value>
125 if (! j.isObject()) {
return qx_bool(
true); }
126 QJsonObject obj = j.toObject(); QJsonValue val;
127 t.
reserve(
static_cast<long>(obj.count()));
129 for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
131 QString key = itr.key(); Value value;
140template <
typename Value>
145 QJsonObject obj; QJsonValue val;
147 for (
long l = 0; l < t.
count(); l++)
153 QString key = QString::fromLatin1(s.data(),
int(s.size()));
157 obj.insert(key, val);
160 return QJsonValue(obj);
164template <
typename Value>
170 if (! j.isObject()) {
return qx_bool(
true); }
171 QJsonObject obj = j.toObject(); QJsonValue val;
172 t.
reserve(
static_cast<long>(obj.count()));
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();
192#if ((! defined(QT_NO_STL)) && (! defined(QT_NO_STL_WCHAR)))
194template <
typename Value>
199 QJsonObject obj; QJsonValue val;
201 for (
long l = 0; l < t.
count(); l++)
204 obj.insert(QString::fromStdWString(t.
getKeyByIndex(l)), val);
207 return QJsonValue(obj);
211template <
typename Value>
217 if (! j.isObject()) {
return qx_bool(
true); }
218 QJsonObject obj = j.toObject(); QJsonValue val;
219 t.
reserve(
static_cast<long>(obj.count()));
221 for (QJsonObject::const_iterator itr = obj.constBegin(); itr != obj.constEnd(); ++itr)
223 QString key = itr.key(); Value value;
225 t.
insert(key.toStdWString(), value);
QxOrm thread-safe container (keep insertion order + quick access by index + quick access by key)
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::QxCollection<Key, Value> : QxOrm thread-safe container (keep insertion order + quick access by in...
bool insert(const Key &key, const Value &value)
Add element 'value' at the end of the list indexed by 'key'.
const_reference_key getKeyByIndex(long index) const
Return the key associated with the element at index position 'index'.
void reserve(long size)
Request that the capacity of the allocated storage space for the items of the container be at least e...
long count() const
Return the number of items in the list (same as 'size()')
void clear()
Remove all items from the list.
const_reference_value getByIndex(long index) const
Return the item at index position 'index'.
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, qx::QxCollection< Key, Value > &t, const QString &format)
static qx_bool fromJson(const QJsonValue &j, qx::QxCollection< QString, Value > &t, const QString &format)
static qx_bool fromJson(const QJsonValue &j, qx::QxCollection< std::string, Value > &t, const QString &format)
static qx_bool fromJson(const QJsonValue &j, qx::QxCollection< std::wstring, Value > &t, const QString &format)
static QJsonValue toJson(const qx::QxCollection< Key, Value > &t, const QString &format)
static QJsonValue toJson(const qx::QxCollection< QString, Value > &t, const QString &format)
static QJsonValue toJson(const qx::QxCollection< std::string, Value > &t, const QString &format)
static QJsonValue toJson(const qx::QxCollection< std::wstring, Value > &t, const QString &format)