
qt5cpp.helpers-body.mustache Maven / Gradle / Ivy
{{>licenseInfo}}
#include "SWGHelpers.h"
#include "SWGModelFactory.h"
#include "SWGObject.h"
#include
#include
#include
#include
{{#cppNamespaceDeclarations}}
namespace {{this}} {
{{/cppNamespaceDeclarations}}
void
setValue(void* value, QJsonValue obj, QString type, QString complexType) {
if(value == nullptr) {
// can't set value with a null pointer
return;
}
if(QStringLiteral("bool").compare(type) == 0) {
bool * val = static_cast(value);
*val = obj.toBool();
}
else if(QStringLiteral("qint32").compare(type) == 0) {
qint32 *val = static_cast(value);
*val = obj.toInt();
}
else if(QStringLiteral("qint64").compare(type) == 0) {
qint64 *val = static_cast(value);
*val = obj.toVariant().toLongLong();
}
else if(QStringLiteral("float").compare(type) == 0) {
float *val = static_cast(value);
*val = obj.toDouble();
}
else if(QStringLiteral("double").compare(type) == 0) {
double *val = static_cast(value);
*val = obj.toDouble();
}
else if (QStringLiteral("QString").compare(type) == 0) {
QString **val = static_cast(value);
if(val != nullptr) {
if(!obj.isNull()) {
// create a new value and return
delete *val;
*val = new QString(obj.toString());
return;
}
else {
// set target to nullptr
delete *val;
*val = nullptr;
}
}
else {
qDebug() << "Can't set value because the target pointer is nullptr";
}
}
else if (QStringLiteral("QDateTime").compare(type) == 0) {
QDateTime **val = static_cast(value);
if(val != nullptr) {
if(!obj.isNull()) {
// create a new value and return
delete *val;
*val = new QDateTime(QDateTime::fromString(obj.toString(), Qt::ISODate));
return;
}
else {
// set target to nullptr
delete *val;
*val = nullptr;
}
}
else {
qDebug() << "Can't set value because the target pointer is nullptr";
}
}
else if (QStringLiteral("QDate").compare(type) == 0) {
QDate **val = static_cast(value);
if(val != nullptr) {
if(!obj.isNull()) {
// create a new value and return
delete *val;
*val = new QDate(QDate::fromString(obj.toString(), Qt::ISODate));
return;
}
else {
// set target to nullptr
delete *val;
*val = nullptr;
}
}
else {
qDebug() << "Can't set value because the target pointer is nullptr";
}
}
else if (QStringLiteral("QByteArray").compare(type) == 0) {
QByteArray **val = static_cast(value);
if(val != nullptr) {
if(!obj.isNull()) {
// create a new value and return
delete *val;
*val = new QByteArray(QByteArray::fromBase64(QByteArray::fromStdString(obj.toString().toStdString())));
return;
}
else {
// set target to nullptr
delete *val;
*val = nullptr;
}
}
else {
qDebug() << "Can't set value because the target pointer is nullptr";
}
}
else if(type.startsWith("SWG") && obj.isObject()) {
// complex type
QJsonObject jsonObj = obj.toObject();
SWGObject * so = (SWGObject*)::{{cppNamespace}}::create(type);
if(so != nullptr) {
so->fromJsonObject(jsonObj);
SWGObject **val = static_cast(value);
delete *val;
*val = so;
}
}
else if(type.startsWith("QList") && QString("").compare(complexType) != 0 && obj.isArray()) {
// list of values
if(complexType.startsWith("SWG")) {
QList* output = new QList();
QJsonArray arr = obj.toArray();
for (const QJsonValue & jval : arr) {
// it's an object
SWGObject * val = (SWGObject*)create(complexType);
QJsonObject t = jval.toObject();
val->fromJsonObject(t);
output->append(val);
}
QList **val = static_cast**>(value);
for (auto item : **val) {
delete item;
}
delete *val;
*val = output;
}
else if(QStringLiteral("qint32").compare(complexType) == 0) {
QList **output = reinterpret_cast **> (value);
(*output)->clear();
QJsonArray arr = obj.toArray();
for (const QJsonValue & jval : arr){
qint32 val;
setValue(&val, jval, QStringLiteral("qint32"), QStringLiteral(""));
(*output)->push_back(val);
}
}
else if(QStringLiteral("qint64").compare(complexType) == 0) {
QList **output = reinterpret_cast **> (value);
(*output)->clear();
QJsonArray arr = obj.toArray();
for (const QJsonValue & jval : arr){
qint64 val;
setValue(&val, jval, QStringLiteral("qint64"), QStringLiteral(""));
(*output)->push_back(val);
}
}
else if(QStringLiteral("bool").compare(complexType) == 0) {
QList **output = reinterpret_cast **> (value);
(*output)->clear();
QJsonArray arr = obj.toArray();
for (const QJsonValue & jval : arr){
bool val;
setValue(&val, jval, QStringLiteral("bool"), QStringLiteral(""));
(*output)->push_back(val);
}
}
else if(QStringLiteral("float").compare(complexType) == 0) {
QList **output = reinterpret_cast **> (value);
(*output)->clear();
QJsonArray arr = obj.toArray();
for (const QJsonValue & jval : arr){
float val;
setValue(&val, jval, QStringLiteral("float"), QStringLiteral(""));
(*output)->push_back(val);
}
}
else if(QStringLiteral("double").compare(complexType) == 0) {
QList **output = reinterpret_cast **> (value);
(*output)->clear();
QJsonArray arr = obj.toArray();
for (const QJsonValue & jval : arr){
double val;
setValue(&val, jval, QStringLiteral("double"), QStringLiteral(""));
(*output)->push_back(val);
}
}
else if(QStringLiteral("QString").compare(complexType) == 0) {
QList **output = reinterpret_cast **> (value);
for (auto item : **output) {
delete item;
}
(*output)->clear();
QJsonArray arr = obj.toArray();
for (const QJsonValue & jval : arr){
QString * val = new QString();
setValue(&val, jval, QStringLiteral("QString"), QStringLiteral(""));
(*output)->push_back(val);
}
}
else if(QStringLiteral("QDate").compare(complexType) == 0) {
QList **output = reinterpret_cast **> (value);
for (auto item : **output) {
delete item;
}
(*output)->clear();
QJsonArray arr = obj.toArray();
for (const QJsonValue & jval : arr){
QDate * val = new QDate();
setValue(&val, jval, QStringLiteral("QDate"), QStringLiteral(""));
(*output)->push_back(val);
}
}
else if(QStringLiteral("QDateTime").compare(complexType) == 0) {
QList **output = reinterpret_cast **> (value);
for (auto item : **output) {
delete item;
}
(*output)->clear();
QJsonArray arr = obj.toArray();
for (const QJsonValue & jval : arr){
QDateTime * val = new QDateTime();
setValue(&val, jval, QStringLiteral("QDateTime"), QStringLiteral(""));
(*output)->push_back(val);
}
}
}
}
void
toJsonValue(QString name, void* value, QJsonObject* output, QString type) {
if(value == nullptr) {
return;
}
if(type.startsWith("SWG")) {
SWGObject *swgObject = reinterpret_cast(value);
if(swgObject != nullptr) {
QJsonObject* o = (*swgObject).asJsonObject();
if(name != nullptr) {
output->insert(name, *o);
delete o;
}
else {
output->empty();
for(QString key : o->keys()) {
output->insert(key, o->value(key));
}
}
}
}
else if(QStringLiteral("QString").compare(type) == 0) {
QString* str = static_cast(value);
output->insert(name, QJsonValue(*str));
}
else if(QStringLiteral("qint32").compare(type) == 0) {
qint32* str = static_cast(value);
output->insert(name, QJsonValue(*str));
}
else if(QStringLiteral("qint64").compare(type) == 0) {
qint64* str = static_cast(value);
output->insert(name, QJsonValue(*str));
}
else if(QStringLiteral("bool").compare(type) == 0) {
bool* str = static_cast(value);
output->insert(name, QJsonValue(*str));
}
else if(QStringLiteral("float").compare(type) == 0) {
float* str = static_cast(value);
output->insert(name, QJsonValue((double)*str));
}
else if(QStringLiteral("double").compare(type) == 0) {
double* str = static_cast(value);
output->insert(name, QJsonValue(*str));
}
else if(QStringLiteral("QDate").compare(type) == 0) {
QDate* date = static_cast(value);
output->insert(name, QJsonValue(date->toString(Qt::ISODate)));
}
else if(QStringLiteral("QDateTime").compare(type) == 0) {
QDateTime* datetime = static_cast(value);
output->insert(name, QJsonValue(datetime->toString(Qt::ISODate)));
}
else if(QStringLiteral("QByteArray").compare(type) == 0) {
QByteArray* byteArray = static_cast(value);
output->insert(name, QJsonValue(QString(byteArray->toBase64())));
}
}
void
toJsonArray(QList* value, QJsonArray* output, QString innerName, QString innerType) {
if(innerType.startsWith("SWG")){
for(void* obj : *value) {
SWGObject *swgObject = reinterpret_cast(obj);
if(swgObject != nullptr) {
output->append(*(swgObject->asJsonObject()));
}
}
}
else if(QStringLiteral("QString").compare(innerType) == 0) {
for(QString* obj : *(reinterpret_cast*>(value))){
output->append(QJsonValue(*obj));
}
}
else if(QStringLiteral("QDate").compare(innerType) == 0) {
for(QDate* obj : *(reinterpret_cast*>(value))){
output->append(QJsonValue(obj->toString(Qt::ISODate)));
}
}
else if(QStringLiteral("QDateTime").compare(innerType) == 0) {
for(QDateTime* obj : *(reinterpret_cast*>(value))){
output->append(QJsonValue(obj->toString(Qt::ISODate))); }
}
else if(QStringLiteral("QByteArray").compare(innerType) == 0) {
for(QByteArray* obj : *(reinterpret_cast*>(value))){
output->append(QJsonValue(QString(obj->toBase64())));
}
}
else if(QStringLiteral("qint32").compare(innerType) == 0) {
for(qint32 obj : *(reinterpret_cast*>(value)))
output->append(QJsonValue(obj));
}
else if(QStringLiteral("qint64").compare(innerType) == 0) {
for(qint64 obj : *(reinterpret_cast*>(value)))
output->append(QJsonValue(obj));
}
else if(QStringLiteral("bool").compare(innerType) == 0) {
for(bool obj : *(reinterpret_cast*>(value)))
output->append(QJsonValue(obj));
}
else if(QStringLiteral("float").compare(innerType) == 0) {
for(float obj : *(reinterpret_cast*>(value)))
output->append(QJsonValue(obj));
}
else if(QStringLiteral("double").compare(innerType) == 0) {
for(double obj : *(reinterpret_cast*>(value)))
output->append(QJsonValue(obj));
}
}
QString
stringValue(QString* value) {
QString* str = static_cast(value);
return QString(*str);
}
QString
stringValue(qint32 value) {
return QString::number(value);
}
QString
stringValue(qint64 value) {
return QString::number(value);
}
QString
stringValue(bool value) {
return QString(value ? "true" : "false");
}
{{#cppNamespaceDeclarations}}
}
{{/cppNamespaceDeclarations}}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy