Commit 4a7738bd authored by EXT Arnaud Clère's avatar EXT Arnaud Clère
Browse files

Added ignored Person field

Fixed a few things
Simplified QBind<_,QJsonValue> reusing QBind<_,QJsonArray>
parent 972946b6
......@@ -259,7 +259,7 @@ struct QBind<TResult, bool> {
if (r) {
if (ba.toLower()=="true" ) dst = true ;
else if (ba.toLower()=="false") dst = false;
else r.reportError(qBindExpectedBoolean);
else r.reportError(qBindExpectedBoolean);
}
return r;
}
......
......@@ -155,7 +155,7 @@ protected:
//template<typename T> bool _bind(T& t, std::enable_if_t<std::is_arithmetic<T>::value>* = nullptr) { double d; if (_bind(d)) { t = d; return true; } return false; }
// Val<TResult> prevents QBind from getting out() of an outer Seq for instance
template<typename T> bool _bind(T& t,int min=1) { return QBind<TResult,T>::bind(Val<TResult>(TResult(*this)), t) || min<=0; } // TODO check why .value() does not work
template<typename T> bool _bind(T& t,int min=1) { if (QBind<TResult,T>::bind(Val<TResult>(TResult(*this)), t)) return true; if (min<=0) reportError("Ignore previous error"); return false; } // TODO check why .value() does not work
template<class T_> friend class Seq; // calls methods below
......@@ -205,11 +205,11 @@ protected:
io->write( "\"" ))|| min<=0; }
// Natively supported overloads
bool _bind( bool n,int min=1) { return io->write(n ? "true" : "false") || min<=0; }
bool _bind( float n,int min=1) { return io->write(QByteArray::number(n, 'g', std::numeric_limits< float>::max_digits10)) || min<=0; }
bool _bind( double n,int min=1) { return io->write(QByteArray::number(n, 'g', std::numeric_limits<double>::max_digits10)) || min<=0; }
bool _bind(qulonglong n,int min=1) { return io->write(QByteArray::number(n )) || min<=0; }
bool _bind( qlonglong n,int min=1) { return io->write(QByteArray::number(n )) || min<=0; }
bool _bind( bool n,int min=1) { return io->write(n ? "true" : "false") || min<=0; }
bool _bind( float n,int min=1) { return io->write(QByteArray::number(n, 'g', std::numeric_limits< float>::max_digits10)) || min<=0; }
bool _bind( double n,int min=1) { return io->write(QByteArray::number(n, 'g', std::numeric_limits<double>::max_digits10)) || min<=0; }
bool _bind( qulonglong n,int min=1) { return io->write(QByteArray::number(n )) || min<=0; }
bool _bind( qlonglong n,int min=1) { return io->write(QByteArray::number(n )) || min<=0; }
// This dispatch would be more simple with C++17 constexpr if
template<typename T> bool _bind(T t,int min=1,std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value>* =nullptr) { return _bind(qulonglong(t),min); }
......@@ -338,11 +338,18 @@ protected:
n = isNegative ? -d : d;
return true; }
bool _any() { QByteArray t; double d; bool b;
return ( _sequence(0) && _out() )
|| _bind(t , 0)
|| _bind(d , 0)
|| _bind(b , 0)
|| _null()
; }
friend class QScopedResult<QJsonReader, QJsonReaderImpl, BindMode::Read>;
// Val<TResult> prevents QBind from getting out() of an outer Seq for instance
template<typename T> bool _bind(T& t,int min=1) { return QBind<QJsonReader,T>::bind(QJsonReader(this).value(), t) || min<=0; }
template<typename T> bool _bind(T& t,int min=1) { if (QBind<QJsonReader,T>::bind(QJsonReader(this).value(), t)) return true; if (min<=0) reportError("Ignore previous error"); return false; }
// TODO static_assert on const T& and T&& ?
template<class T_> friend class Seq; // calls methods below
......@@ -361,16 +368,6 @@ protected:
}
return get(*level.end, "}"); }
private:
bool _any() {
QByteArray t; double d; bool b;
return
( _sequence(0) && _out() )
|| _bind(t , 0)
|| _bind(d , 0)
|| _bind(b , 0)
|| _null()
;
}
int getDigit(int base = 10) { Q_ASSERT(0<base);
int digit;
if ( 0 <= (digit=nextChar()-'0' ) && digit < base && base <= 10) { getChar(); return digit; }
......@@ -444,6 +441,7 @@ template<class TResult>
struct QBind<TResult, const QJsonValue> { static TResult bind(Val<TResult> dst, const QJsonValue& src) {
static_assert(TResult::Mode==Write,"Cannot Read from TResult into const QJsonValue&");
if (src.isArray ()) return dst.bind(src.toArray ());
if (src.isBool ()) return dst.bind(src.toBool ());
if (src.isDouble()) return dst.bind(src.toDouble());
if (src.isString()) return dst.bind(src.toString());
return dst.null();
......@@ -452,68 +450,18 @@ template<class TResult>
struct QBind<TResult, QJsonValue> {
static TResult bind(Val<TResult> dst, QJsonValue& src, std::enable_if_t<TResult::Mode==Write>* = nullptr) {
if (src.isArray ()) return dst.bind(src.toArray ());
if (src.isBool ()) return dst.bind(src.toBool ());
if (src.isDouble()) return dst.bind(src.toDouble());
if (src.isString()) return dst.bind(src.toString());
return dst.null();
}
static TResult bind(Val<TResult> src, QJsonValue& dst, std::enable_if_t<TResult::Mode==Read >* = nullptr) {
auto s(src.sequence(0));
if (s) {
QJsonArray array;
for (auto i = s.item(0); i; i = s.item(0)) {
QJsonValue v;
s = i.bind(v);
if (s)
array.append(v);
}
dst = array;
return s;
}
TResult r;
bool b; if (r = src.bind(b,0)) { dst = QJsonValue(b); return r; }
double d; if (r = src.bind(d,0)) { dst = QJsonValue(d); return r; }
QString t; if (r = src.bind(t,0)) { dst = QJsonValue(t); return r; }
dst = QJsonValue();
return src.null();
}
};
template<class TResult>
struct QBind<TResult, const QJsonValueRef> { static TResult bind(Val<TResult> dst, const QJsonValueRef& src) {
static_assert(TResult::Mode==Write,"Cannot Read from TResult into const QJsonValueRef&");
if (src.isArray ()) return dst.bind(src.toArray ());
if (src.isDouble()) return dst.bind(src.toDouble());
if (src.isString()) return dst.bind(src.toString());
return dst.null();
}};
template<class TResult>
struct QBind<TResult, QJsonValueRef> {
static TResult bind(Val<TResult> dst, QJsonValueRef& src, std::enable_if_t<TResult::Mode==Write>* = nullptr) {
if (src.isArray ()) return dst.bind(src.toArray ());
if (src.isDouble()) return dst.bind(src.toDouble());
if (src.isString()) return dst.bind(src.toString());
return dst.null();
}
static TResult bind(Val<TResult> src, QJsonValueRef& dst, std::enable_if_t<TResult::Mode==Read >* = nullptr) {
auto s(src.sequence());
if (s) {
QJsonArray array;
for (auto i = s.item(0); i; i = s.item(0)) {
QJsonValueRef v;
s = i.bind(v);
if (s)
array.append(v);
}
dst = array;
return s;
}
double d; QString t;
auto
result = src.bind(d); if (result) { dst = QJsonValueRef(d); return result; }
result = src.bind(t); if (result) { dst = QJsonValueRef(t); return result; }
dst = QJsonValueRef(); return src.null();
bool b; if (r = src.bind(b,0)) { dst = QJsonValue(b); return r; }
double d; if (r = src.bind(d,0)) { dst = QJsonValue(d); return r; }
QString t; if (r = src.bind(t,0)) { dst = QJsonValue(t); return r; }
QJsonArray a; if (r = src.bind(a,0)) { dst = a ; return r; }
dst = QJsonValue(); return src.null();
}
};
......@@ -532,16 +480,19 @@ template<class TResult>
struct QBind<TResult, QJsonArray> {
static TResult bind(Val<TResult> dst, QJsonArray& src, std::enable_if_t<TResult::Mode==Write>* = nullptr) {
auto s(dst.sequence());
for (auto item : src) {
for (QJsonValue&& item : src) {
s = s.bind(item);
}
return s;
}
static TResult bind(Val<TResult> src, QJsonArray& dst, std::enable_if_t<TResult::Mode==Read >* = nullptr) {
auto s(src.sequence());
QJsonValue v;
while (s = s.bind(v)) {
dst.append(v);
Val<Seq<TResult>> i;
while (i = s.item(0)) { // s = s.bind(v,0) would assign s an invalid Seq<TResult> on the last item
QJsonValue v;
s = i.bind(v);
if (s)
dst.append(v);
}
return s;
}
......
......@@ -58,11 +58,13 @@ struct Person
template<class TResult>
TResult bind(Val<TResult> value) { // since we only bind T& (no const T&), it works with TResult::Mode==Read as well as Write
double weight = -1.; // now ignored
return value
.sequence()
.bind(firstName)
.bind(lastName)
.bind(height)
.bind(weight,0)
.bind(phones) // recursive calls to QBind will take care of that part
; // automagically closes all opened structures
}
......@@ -308,7 +310,7 @@ int main()
{
QBuffer json;
json.open(QIODevice::ReadOnly);
json.buffer() = "_[ _\"John\" _, \"Doe\" , _1.75 , [ \"+44 1234567\" , \"+44 2345678\" ], \"superfluous item\" _] ";
json.buffer() = "_[ _\"John\" _, \"Doe\" , _1.75 , 69., [ \"+44 1234567\" , \"+44 2345678\" ], \"superfluous item\" _] ";
QJsonValue v;
Person p;
QVector<QJsonReaderImpl::Error> jsonReaderErrors ;
......
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