代码拉取完成,页面将自动刷新
#include <QDebug>
#include "BSON.h"
#include "QMongoDriver.h"
QVariantMap fromBson(mongo::BSONObj bson) {
QVariantMap obj;
for(mongo::BSONObjIterator i(bson); i.more();) {
mongo::BSONElement e = i.next();
QString name = QString::fromStdString(e.fieldName());
switch(e.type()) {
case mongo::EOO:
return obj;
case mongo::NumberDouble:
obj[name] = e.numberDouble();
break;
case mongo::String:
obj[name] = QString::fromStdString(e.str());
break;
case mongo::Object:
obj[name] = fromBson(e.embeddedObject());
break;
case mongo::Date:
obj[name] = QDateTime::fromTime_t(e.date());
break;
case mongo::Undefined:
obj[name] = QVariant();
break;
case mongo::jstOID:
obj[name] = QString::fromStdString(e.__oid().str());
break;
case mongo::jstNULL:
obj[name] = QVariant();
break;
case mongo::Bool:
obj[name] = e.boolean();
break;
case mongo::CodeWScope:
obj[name] = "MongoCode["+QString::fromStdString(e.codeWScopeCode())+"]";
break;
case mongo::NumberInt:
obj[name] = e.numberInt();
break;
default:
qCritical() << "fromBson() type" << e.type() << "unknown!";
Q_ASSERT(false);
}
}
return obj;
}
mongo::BSONObj toBson(QVariantMap obj) {
mongo::BSONObjBuilder b;
QVariantMap::iterator it = obj.begin();
while(it != obj.end()) {
QByteArray byteName = it.key().toStdString().c_str();
const char* name = byteName.constData();
QVariant v = it.value();
int type = v.type();
// qDebug() << "toBson converting type" << v.typeName();
if (v.canConvert<QObject*>()) {
QMongoType* mongo = qobject_cast<QMongoType*>( v.value<QObject*>() );
Q_ASSERT(mongo->o.nFields() == 1);
b.appendAs(mongo->o.firstElement(), name);
++it;
continue;
}
bool ok = true;
switch(type) {
case QVariant::Int:
b.append(name, v.toInt(&ok));
break;
case QVariant::String:
b.append(name, v.toString().toStdString());
break;
case QVariant::LongLong:
b.append(name, v.toLongLong(&ok));
break;
case QVariant::UInt:
b.append(name, v.toUInt(&ok));
break;
case QVariant::Map:
b.append(name, toBson(v.toMap()));
break;
case QVariant::Double:
b.append(name, v.toDouble(&ok));
break;
case QVariant::Bool:
b.appendBool(name, v.toBool());
break;
case QVariant::Time:
b.appendTimeT(name, v.toDateTime().toTime_t());
break;
case QVariant::Invalid:
b.appendUndefined(name);
break;
default:
qCritical() << "toBson() failed to convert" << obj << "for" << name << "with type" << v.typeName();
ok = false;
}
Q_ASSERT(ok);
++it;
}
return b.obj();
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。