Commit 186694ed authored by EXT Arnaud Clère's avatar EXT Arnaud Clère
Browse files

WIP Use Phone from Person (FIX JsonReader to get Phone::Type string

value)
parent 3108f8b0
......@@ -749,14 +749,25 @@ template<class T, class TResult>
TResult qbind(Val<TResult>&& v, T* t) {
auto rw = v->mode();
auto mo = T::staticMetaObject;
auto r = v.meta({{qmName,mo.className()}}).record();
auto r = v.record(mo.className());
for (int i = 0; i<mo.propertyCount(); i++) {
auto p = mo.property(i);
if (p.isStored()) {
if (rw==Read) {
QVariant pv;
r = r.bind(p.name(),pv);
/* TODO if (p.isEnumType()) keyToValue(pv.toString()) */
auto name = p.name();
r = r.bind(name,pv);
int converted = 0;
bool isConverted = false;
if (p.isFlagType()) {
converted = p.enumerator().keysToValue(pv.toString().toUtf8(), &isConverted);
}
else if (p.isEnumType()) {
converted = p.enumerator().keyToValue(pv.toString().toUtf8(), &isConverted);
}
if (isConverted) {
pv = converted;
}
if ((!r && p.isResettable() && !p.resetOnGadget(t) )||
( r && !p.writeOnGadget(t, pv))) {
v->reportError("TODO");
......@@ -764,7 +775,10 @@ TResult qbind(Val<TResult>&& v, T* t) {
}
if (rw==Write) {
QVariant pv = p.readOnGadget(t);
if (p.isEnumType() /*&& pv.canConvert<int>()*/) {
if (p.isFlagType() /*&& pv.canConvert<int>()*/) {
r = r.bind(p.name(), p.enumerator().valueToKeys(pv.toInt()));
}
else if (p.isEnumType() /*&& pv.canConvert<int>()*/) {
r = r.bind(p.name(), p.enumerator().valueToKey(pv.toInt()));
}
else {
......
......@@ -89,7 +89,7 @@ protected:
bool _out ( ) { io->writeEndElement(); return true; }
bool _item( ) { return true; }
bool _item(QName n) { name=n; return true; }
void _meta(QMetaData& m) { metaData.insert(m.cbegin(), m.cend()); auto n=metaData.find(qmName); if (n!=metaData.end()) { name=n->second; n->second=QString(); } }
void _meta(QMetaData& m) { auto merged=m; merged.insert(metaData.cbegin(), metaData.cend()); metaData=std::move(merged); auto n=metaData.find(qmName); if (n!=metaData.end()) { name=n->second; n->second=QString(); } }
private:
QString tag(QName def) { if (name.isNull()) return def; auto n=name; name=QString(); return n; }
void att() { for (auto a : metaData) { if (!a.second.isNull()) io->writeAttribute(a.first, a.second); } }
......
#include "data.h"
// for moc
#include<QtCore/qdebug.h>
QDebug &operator<<(QDebug &out, const Phone &p) { return out << "Phone(" << p._t << ", " << p._n <<")" ; }
#include<QtCore/qdatastream.h>
QDataStream &operator<<(QDataStream &out, const Phone &p) { return out << quint8(p._t) << p._n ; }
QDataStream &operator>>(QDataStream &out, Phone &p) { quint8 t; out >> t >> p._n ; p._t=Phone::Type(t); return out; }
......@@ -11,10 +11,21 @@ class Phone {
public:
QBIND_GADGET_WITH_METAOBJECT(Phone) // making it (de)serializable and printable using reflection
enum Type { Unknown=0, Mobile, Home, Office } type;
enum Type : quint8 { Unknown=0, Mobile, Home, Office };
Q_ENUM(Type)
Phone(Type t=Unknown, QString n=QString()) : _t(t), _n(n) {}
private:
//private:
Type _t; QString _n;
};
Q_DECLARE_METATYPE(Phone)
// For comparison purposes:
#include<QtCore/qdebug.h>
QDebug &operator<<(QDebug &out, const Phone &p);
#include<QtCore/qdatastream.h>
QDataStream &operator<<(QDataStream &out, const Phone &p);
QDataStream &operator>>(QDataStream &out, Phone &p);
......@@ -77,7 +77,7 @@
struct Person
{
QString firstName, lastName; double height; int age; QVector<QString> phones; QString comments; QList<Person> children;
QString firstName, lastName; double height; int age; QVector<Phone> phones; QString comments; QList<Person> children;
template<class TResult> //=Cursor
TResult bind(Val<TResult>&& value) { // works with value->mode()==Read as well as Write
......@@ -254,7 +254,7 @@ int main(int argc, char *argv[])
2./3, 1./3, 2./3, 1.,
1./3, 2./3, 1./3, 1.,
0. , 0. , 0. , 1.};
Person person{"John","Doe",1.75,18,{"+44 1234567","+44 2345678"},"unicode is likely U+01 \x01 + U+1F \x1F + U+A4 ¤ U+B0 ° U+D8 Ø U+FF ÿ",QList<Person>()};
Person person{"John","Doe",1.75,18,{{Phone::Home,"+44 1234567"},{Phone::Office,"+44 2345678"}},"unicode is likely U+01 \x01 + U+1F \x1F + U+A4 ¤ U+B0 ° U+D8 Ø U+FF ÿ",QList<Person>()};
const double PI=3.141592653589793;
// Temporary buffers
......@@ -557,7 +557,10 @@ int main(int argc, char *argv[])
int size = person.phones.size();
s.startArray(quint64(size));
for (int i=0; i < size; i++) {
s.append(person.phones[i]);
s.startMap();
s.append("type");s.append(person.phones[i]._t);
s.append("number");s.append(person.phones[i]._n);
s.endMap();
};
s.endArray();
s.append("comments");s.append(person.comments.toUtf8().constData());
......@@ -630,7 +633,7 @@ int main(int argc, char *argv[])
GROUP("Person<>Json")//====================================================
{
QBuffer json; json.open(QIODevice::ReadOnly);
json.buffer() = "_{ \"names\": [ _\"John\" _, \"Doe\"] , \"age\"_:_null, \"height\":_1.75, \"phones\": [ \"+44 1234567\" , \"+44 2345678\" ], \"\":\"superfluous item\" _} ";
json.buffer() = "_{ \"names\": [ _\"John\" _, \"Doe\"] , \"age\"_:_null, \"height\":_1.75, \"phones\": [ {\"type\":\"Home\", \"number\":\"+44 1234567\"} , {\"type\":\"Office\", \"number\":\"+44 2345678\"} ], \"\":\"superfluous item\" _} ";
// 0 22 41 55 70
Person p;
QBuffer roundtrip; roundtrip.open(QIODevice::ReadWrite);
......@@ -693,7 +696,7 @@ int main(int argc, char *argv[])
QBuffer cbor; cbor.open(QIODevice::ReadOnly);
cbor.buffer() = QByteArray::fromHex(
"A5656E616D657382644A6F686E63446F6563616765F666686569676874F93F006670686F6E6573826B2B343420313233343536376B2B3434203233343536373860707375706572666C756F7573206974656D");
// { "names": [ "John" , "Doe" ],"age":null,"height": 1.75, "phones": [ "+44 1234567" , "+44 2345678" ], "":"superfluous item" }
// { "names": [ "John" , "Doe" ],"age":null,"height": 1.75, "phones": [ {"type":"Home","+44 1234567"} , {"type":"Home","+44 2345678"} ], "":"superfluous item" }
Person p;
QBuffer roundtrip; roundtrip.open(QIODevice::ReadWrite);
QJsonValue jv;
......@@ -764,17 +767,24 @@ int main(int argc, char *argv[])
reader.readString().data=="phones" &&
reader.readString().status==QCborStreamReader::EndOfString &&
reader.isArray() &&
reader.enterContainer() &&
reader.hasNext() &&
reader.isString())
p.phones.append(reader.readString().data);
if (reader.readString().status==QCborStreamReader::EndOfString &&
reader.hasNext() &&
reader.isString())
p.phones.append(reader.readString().data);
if (reader.readString().status==QCborStreamReader::EndOfString &&
!reader.hasNext() &&
reader.leaveContainer() &&
reader.enterContainer())
while(reader.hasNext()) {
Phone ph;
if (reader.isMap() &&
reader.enterContainer() &&
reader.hasNext() &&
reader.isInteger())
ph._t=Phone::Type(reader.toInteger());
if (reader.next() &&
reader.hasNext() &&
reader.isString())
ph._n=reader.readString().data;
if (reader.readString().status==QCborStreamReader::EndOfString &&
!reader.hasNext() &&
reader.leaveContainer())
p.phones.append(ph);
}
if (reader.leaveContainer() &&
reader.hasNext() &&
reader.isString() &&
reader.readString().data=="comments" &&
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment