Skip to content

Commit acbd689

Browse files
committed
avoid ascii cast warnings
1 parent 0e794d6 commit acbd689

File tree

18 files changed

+172
-168
lines changed

18 files changed

+172
-168
lines changed

src/abstractserializer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef SERIALIZER_H
22
#define SERIALIZER_H
33

4-
#include <QVariant>
4+
#include <QtCore/QVariant>
55
#include "serializer_global.h"
66

77
class AbstractSerializer

src/binaryserializer.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include "binaryserializer.h"
22

3-
#include <QMetaType>
4-
#include <QDataStream>
5-
#include <QJsonDocument>
6-
#include <QDebug>
3+
#include <QtCore/QMetaType>
4+
#include <QtCore/QDataStream>
5+
#include <QtCore/QJsonDocument>
6+
#include <QtCore/QDebug>
77

88
QDataStream &operator<<(QDataStream &stream, const QJsonDocument &doc)
99
{
@@ -54,5 +54,5 @@ QString BinarySerializer::toString(const QVariant &value) const
5454
QByteArray data;
5555
QDataStream ds(&data, QIODevice::WriteOnly);
5656
ds << copy;
57-
return QString(data.toBase64());
57+
return QString::fromUtf8(data.toBase64());
5858
}

src/binaryserializer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#define BINARYSERIALIZER_H
33

44
#include "abstractserializer.h"
5-
#include <QJsonDocument>
6-
#include <QDebug>
5+
#include <QtCore/QJsonDocument>
6+
#include <QtCore/QDebug>
77

88
class BinarySerializer : public AbstractSerializer
99
{

src/jsonserializer.cpp

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
#include <QtGui/QPolygon>
1515
#include <QtGui/QPolygonF>
1616

17-
#define VARIANT_TYPE "_type"
18-
#define VARIANT_VALUE "_value"
19-
#define CLASS_NAME(x) QString(#x)
17+
#define VARIANT_TYPE QStringLiteral("_type")
18+
#define VARIANT_VALUE QStringLiteral("_value")
19+
#define CLASS_NAME(x) QString::fromUtf8(#x)
2020

2121
template <typename T> bool registerQList()
2222
{
@@ -174,7 +174,7 @@ QJsonObject JsonSerializer::serialize(QObject *obj)
174174
QMetaProperty prop = obj->metaObject()->property(i);
175175
QJsonValue val;
176176
auto v = prop.read(obj);
177-
QString pps(prop.name());
177+
QString pps = QString::fromUtf8((prop.name()));
178178
if (v.isValid()) {
179179
if (prop.isEnumType())
180180
val = toJson(prop.enumerator(), obj->property(prop.name()));
@@ -184,7 +184,7 @@ QJsonObject JsonSerializer::serialize(QObject *obj)
184184

185185
if (val == QJsonValue())
186186
qDebug() << prop.name();
187-
json.insert(prop.name(), val);
187+
json.insert(pps, val);
188188
} // for
189189
return json;
190190
}
@@ -193,9 +193,9 @@ bool JsonSerializer::deserialize(const QJsonObject &json, QObject *obj)
193193
{
194194
for (int i = 0; i < obj->metaObject()->propertyCount(); i++) {
195195
QMetaProperty prop = obj->metaObject()->property(i);
196-
QJsonValue jsonValue = json.value(prop.name());
196+
QJsonValue jsonValue = json.value(QString::fromUtf8(prop.name()));
197197

198-
if (QString(prop.typeName()).endsWith("*")) {
198+
if (QString::fromUtf8(prop.typeName()).endsWith(QStringLiteral("*"))) {
199199
if (jsonValue == QJsonValue()) {
200200
prop.write(obj, QVariant::fromValue(nullptr));
201201
continue;
@@ -220,10 +220,10 @@ bool JsonSerializer::deserialize(const QJsonObject &json, QObject *obj)
220220
static_cast<QMetaType::Type>(prop.userType()), jsonValue);
221221

222222
QGenericArgument arg(prop.typeName(), propertyValue.data());
223-
QString pName(prop.name());
223+
QString pName = QString::fromUtf8(prop.name());
224224
pName[0] = pName[0].toUpper();
225-
pName.prepend("set");
226-
if (pName == "setObjectName") {
225+
pName.prepend(QStringLiteral("set"));
226+
if (pName == QStringLiteral("setObjectName")) {
227227
obj->setObjectName(propertyValue.toString());
228228
} else {
229229
// bool ok = obj->metaObject()->invokeMethod(obj,
@@ -234,7 +234,7 @@ bool JsonSerializer::deserialize(const QJsonObject &json, QObject *obj)
234234
}
235235
} // for
236236

237-
obj->setObjectName(json.value("objectName").toString());
237+
obj->setObjectName(json.value(QStringLiteral("objectName")).toString());
238238
return true;
239239
}
240240

@@ -272,9 +272,9 @@ QJsonValue JsonSerializer::toJson(QVariant v)
272272

273273
QJsonObject o;
274274

275-
QString typeName = QString(v.typeName());
275+
QString typeName = QString::fromUtf8(v.typeName());
276276

277-
if (typeName.endsWith("*")) {
277+
if (typeName.endsWith(QStringLiteral("*"))) {
278278
auto obj = v.value<QObject *>();
279279
if (!obj)
280280
return QJsonValue();
@@ -295,28 +295,28 @@ QJsonValue JsonSerializer::toJson(QVariant v)
295295
switch (v.type()) {
296296
case QVariant::Point: {
297297
auto pt = v.toPoint();
298-
return QJsonObject{{"x", pt.x()}, {"y", pt.y()}};
298+
return QJsonObject{{ QStringLiteral("x"), pt.x()}, { QStringLiteral("y"), pt.y()}};
299299
break;
300300
}
301301
case QVariant::PointF: {
302302
auto pt = v.toPointF();
303-
return QJsonObject{{"x", pt.x()}, {"y", pt.y()}};
303+
return QJsonObject{{ QStringLiteral("x"), pt.x()}, { QStringLiteral("y"), pt.y()}};
304304
break;
305305
}
306306
case QVariant::Rect: {
307307
auto rc = v.toRect();
308-
return QJsonObject{{"x", rc.x()},
309-
{"y", rc.y()},
310-
{"width", rc.width()},
311-
{"height", rc.height()}};
308+
return QJsonObject{{ QStringLiteral("x"), rc.x()},
309+
{ QStringLiteral("y"), rc.y()},
310+
{ QStringLiteral("width"), rc.width()},
311+
{ QStringLiteral("height"), rc.height()}};
312312
break;
313313
}
314314
case QVariant::RectF: {
315315
auto rc = v.toRectF();
316-
return QJsonObject{{"x", rc.x()},
317-
{"y", rc.y()},
318-
{"width", rc.width()},
319-
{"height", rc.height()}};
316+
return QJsonObject{{ QStringLiteral("x"), rc.x()},
317+
{ QStringLiteral("y"), rc.y()},
318+
{ QStringLiteral("width"), rc.width()},
319+
{ QStringLiteral("height"), rc.height()}};
320320
break;
321321
}
322322
case QVariant::Locale: {
@@ -398,27 +398,31 @@ QVariant JsonSerializer::fromJson(const QMetaType::Type &type,
398398
if (value == QJsonValue())
399399
return QVariant();
400400

401-
QString typeName = QMetaType::typeName(type);
401+
QString typeName = QString::fromUtf8(QMetaType::typeName(type));
402402

403403
switch (type) {
404404
case QMetaType::QPoint: {
405405
auto o = value.toObject();
406-
return QPoint(o.value("x").toInt(), o.value("y").toInt());
406+
return QPoint(o.value(QStringLiteral("x")).toInt(), o.value(QStringLiteral("y")).toInt());
407407
}
408408
case QMetaType::QPointF: {
409409
auto o = value.toObject();
410-
return QPointF(o.value("x").toDouble(), o.value("y").toDouble());
410+
return QPointF(o.value(QStringLiteral("x")).toDouble(),
411+
o.value(QStringLiteral("y")).toDouble());
411412
}
412413
case QMetaType::QRect: {
413414
auto o = value.toObject();
414-
return QRect(o.value("x").toInt(), o.value("y").toInt(),
415-
o.value("width").toInt(), o.value("height").toInt());
415+
return QRect(o.value(QStringLiteral("x")).toInt(),
416+
o.value(QStringLiteral("y")).toInt(),
417+
o.value(QStringLiteral("width")).toInt(),
418+
o.value(QStringLiteral("height")).toInt());
416419
}
417420
case QMetaType::QRectF: {
418421
auto o = value.toObject();
419-
return QRectF(o.value("x").toDouble(), o.value("y").toDouble(),
420-
o.value("width").toDouble(),
421-
o.value("height").toDouble());
422+
return QRectF(o.value(QStringLiteral("x")).toDouble(),
423+
o.value(QStringLiteral("y")).toDouble(),
424+
o.value(QStringLiteral("width")).toDouble(),
425+
o.value(QStringLiteral("height")).toDouble());
422426
}
423427
case QMetaType::QFont: {
424428
QFont f;
@@ -448,32 +452,34 @@ QVariant JsonSerializer::fromJson(const QMetaType::Type &type,
448452
default: break;
449453
}
450454
if (value.isArray()) {
451-
if (typeName.startsWith("QList<")) {
452-
auto name = typeName.replace("QList<", "").replace(">", "");
455+
if (typeName.startsWith(QStringLiteral("QList<"))) {
456+
auto name = typeName.replace(QStringLiteral("QList<"), QStringLiteral(""))
457+
.replace(QStringLiteral(">"), QStringLiteral(""));
453458
auto keyType = static_cast<QMetaType::Type>(
454459
QMetaType::type(name.toLatin1().data()));
455460
return fromJson(keyType, value.toArray());
456461
}
457-
if (typeName == "QStringList") {
462+
if (typeName == QStringLiteral("QStringList")) {
458463
auto keyType = static_cast<QMetaType::Type>(qMetaTypeId<QString>());
459464
return fromJson(keyType, value.toArray());
460465
}
461466
return fromJson(type, value.toArray());
462467
}
463468

464469
if (value.isObject()) {
465-
if (typeName.endsWith("*")) {
470+
if (typeName.endsWith(QStringLiteral("*"))) {
466471
const QMetaObject *metaObject = QMetaType::metaObjectForType(type);
467472
auto obj = metaObject->newInstance();
468473
deserialize(value.toObject(), obj);
469474
return QVariant::fromValue(obj);
470475
}
471476

472-
if (typeName.startsWith("QMap<")) {
477+
if (typeName.startsWith(QStringLiteral("QMap<"))) {
473478
// reinterpret_cast<QVariantMap*>(QMetaType::create(type));
474479

475-
auto parts
476-
= typeName.replace("QMap<", "").replace(">", "").split(",");
480+
auto parts = typeName.replace(QStringLiteral("QMap<"), QStringLiteral(""))
481+
.replace(QStringLiteral(">"), QStringLiteral(""))
482+
.split(QStringLiteral(","));
477483
auto keyType = static_cast<QMetaType::Type>(
478484
QMetaType::type(parts.at(0).toLatin1().data()));
479485
auto valueType = static_cast<QMetaType::Type>(
@@ -492,8 +498,8 @@ QVariant JsonSerializer::fromJson(const QMetaType::Type &type,
492498
// const QMetaObject *metaObject = QMetaType::metaObjectForType(type);
493499
// auto obj = metaObject->newInstance();
494500

495-
QString typeName
496-
= QMetaType::typeName(type); // = object[VARIANT_TYPE].toString();
501+
QString typeName = QString::fromUtf8(QMetaType::typeName(type));
502+
// = object[VARIANT_TYPE].toString();
497503
// QVariant::Type type =
498504
// QVariant::nameToType(typeName.toLatin1().data());
499505

@@ -512,7 +518,7 @@ QVariant JsonSerializer::fromJson(const QMetaType::Type &type,
512518
// }
513519

514520
// if(QMetaType::type(typeName.toLatin1().data())> 1024){
515-
if (typeName.endsWith("*")) {
521+
if (typeName.endsWith(QStringLiteral("*"))) {
516522
QObject *obj;
517523
QVariantMap map;
518524
QJsonObject mapObject
@@ -581,7 +587,7 @@ QVariantMap JsonSerializer::serializeQObject(QObject *obj)
581587
for (int i = 0; i < obj->metaObject()->propertyCount(); i++) {
582588
QMetaProperty property = obj->metaObject()->property(i);
583589
if (property.isReadable() && property.isWritable())
584-
map.insert(property.name(), property.read(obj).toString());
590+
map.insert(QString::fromUtf8(property.name()), property.read(obj).toString());
585591
}
586592

587593
return map;
@@ -592,6 +598,6 @@ void JsonSerializer::deserializeQObject(QObject *obj, QVariantMap map)
592598
for (int i = 0; i < obj->metaObject()->propertyCount(); i++) {
593599
QMetaProperty property = obj->metaObject()->property(i);
594600
if (property.isReadable() && property.isWritable())
595-
property.write(obj, map[property.name()].toString());
601+
property.write(obj, map[QString::fromUtf8(property.name())].toString());
596602
}
597603
}

src/jsonserializer.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#ifndef JSONSERIALIZER_H
22
#define JSONSERIALIZER_H
33

4-
#include <QJsonObject>
5-
#include <QObject>
6-
#include <QVariant>
7-
//#include <QMetaProperty>
8-
//#include <QDebug>
4+
#include <QtCore/QJsonObject>
5+
#include <QtCore/QObject>
6+
#include <QtCore/QVariant>
97

108
class JsonSerializer
119
{

src/sqlserializer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
#include <QDebug>
2+
#include <QtCore/QDebug>
33
#include "sqlserializer.h"
44

55
SqlSerializer::SqlSerializer() :StringSerializer ()
@@ -33,7 +33,7 @@ QString SqlSerializer::escapeString(const QString &str) const
3333
// }
3434
// return ret;
3535
QString ret(str);
36-
return ret.replace("'", "''");
36+
return ret.replace(QStringLiteral("'"), QStringLiteral("''"));
3737
}
3838

3939
QString SqlSerializer::unescapeString(const QString &str) const
@@ -82,5 +82,5 @@ QString SqlSerializer::unescapeString(const QString &str) const
8282

8383
// return ret;
8484
QString ret(str);
85-
return ret.replace("''", "'");
85+
return ret.replace(QStringLiteral("''"), QStringLiteral("'"));
8686
}

0 commit comments

Comments
 (0)