Fix reading json.

This commit is contained in:
Roman Telezhynskyi 2022-10-29 14:46:10 +03:00
parent 9c7ab2fb4a
commit 60b4851753
4 changed files with 88 additions and 52 deletions

View File

@ -118,13 +118,16 @@ void AbstractTest::PassmarkDataFromJson(const QString &json, VPiecePassmarkData
try try
{ {
auto previousSAPoint = PointFromJson<VSAPoint>(passmarkData[QStringLiteral("previousSAPoint")].toObject()); VSAPoint previousSAPoint;
PointFromJson(passmarkData[QStringLiteral("previousSAPoint")].toObject(), previousSAPoint);
data.previousSAPoint = previousSAPoint; data.previousSAPoint = previousSAPoint;
auto passmarkSAPoint = PointFromJson<VSAPoint>(passmarkData[QStringLiteral("passmarkSAPoint")].toObject()); VSAPoint passmarkSAPoint;
PointFromJson(passmarkData[QStringLiteral("passmarkSAPoint")].toObject(), passmarkSAPoint);
data.passmarkSAPoint = passmarkSAPoint; data.passmarkSAPoint = passmarkSAPoint;
auto nextSAPoint = PointFromJson<VSAPoint>(passmarkData[QStringLiteral("nextSAPoint")].toObject()); VSAPoint nextSAPoint;
PointFromJson(passmarkData[QStringLiteral("nextSAPoint")].toObject(), nextSAPoint);
data.nextSAPoint = nextSAPoint; data.nextSAPoint = nextSAPoint;
} }
catch (const VException &e) catch (const VException &e)
@ -198,7 +201,7 @@ void AbstractTest::PassmarkShapeFromJson(const QString &json, QVector<QLineF> &s
QString type; QString type;
AbstractTest::ReadStringValue(lineObject, typeKey, type); AbstractTest::ReadStringValue(lineObject, typeKey, type);
if (type != typeid(QLineF).name()) if (type != QLatin1String("QLineF"))
{ {
const QString error = QStringLiteral("Invalid json file '%1'. Unexpected class '%2'.") const QString error = QStringLiteral("Invalid json file '%1'. Unexpected class '%2'.")
.arg(json, lineObject[typeKey].toString()); .arg(json, lineObject[typeKey].toString());
@ -213,7 +216,7 @@ void AbstractTest::PassmarkShapeFromJson(const QString &json, QVector<QLineF> &s
void AbstractTest::ComparePathsDistance(const QVector<QPointF> &ekv, const QVector<QPointF> &ekvOrig) const void AbstractTest::ComparePathsDistance(const QVector<QPointF> &ekv, const QVector<QPointF> &ekvOrig) const
{ {
// Begin comparison // Begin comparison
QCOMPARE(ekv.size(), ekvOrig.size());// First check if sizes equal QCOMPARE(ekv.size(), ekvOrig.size());// First check if sizes are equal
const qreal testAccuracy = MmToPixel(1.); const qreal testAccuracy = MmToPixel(1.);
for (int i=0; i < ekv.size(); i++) for (int i=0; i < ekv.size(); i++)
@ -526,7 +529,7 @@ void AbstractTest::ReadPointValue(const QJsonObject &itemObject, const QString &
{ {
if (itemObject.contains(attribute)) if (itemObject.contains(attribute))
{ {
value = PointFromJson<VPointF>(itemObject[attribute].toObject()); PointFromJson(itemObject[attribute].toObject(), value);
} }
else else
{ {
@ -728,8 +731,11 @@ void AbstractTest::ReadDoubleValue(const QJsonObject &itemObject, const QString
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
auto AbstractTest::QLineFromJson(const QJsonObject &itemObject) -> QLineF auto AbstractTest::QLineFromJson(const QJsonObject &itemObject) -> QLineF
{ {
return {PointFromJson<QPointF>(itemObject[QStringLiteral("p1")].toObject()), QPointF p1;
PointFromJson<QPointF>(itemObject[QStringLiteral("p2")].toObject())}; QPointF p2;
PointFromJson(itemObject[QStringLiteral("p1")].toObject(), p1);
PointFromJson(itemObject[QStringLiteral("p2")].toObject(), p2);
return {p1, p2};
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -808,9 +814,9 @@ void AbstractTest::DBFromJson(const QJsonObject &dbObject, QSharedPointer<VConta
if (dbObject.contains(itemsKey)) if (dbObject.contains(itemsKey))
{ {
QJsonArray items = dbObject[itemsKey].toArray(); QJsonArray items = dbObject[itemsKey].toArray();
for (int i = 0; i < items.size(); ++i) for (auto && item : items)
{ {
QJsonObject itemObject = items[i].toObject(); QJsonObject itemObject = item.toObject();
GOType objectType; GOType objectType;
AbstractTest::ReadDoubleValue(itemObject, QStringLiteral("type"), objectType); AbstractTest::ReadDoubleValue(itemObject, QStringLiteral("type"), objectType);
@ -818,7 +824,8 @@ void AbstractTest::DBFromJson(const QJsonObject &dbObject, QSharedPointer<VConta
{ {
case GOType::Point: case GOType::Point:
{ {
VPointF point = PointFromJson<VPointF>(itemObject); VPointF point;
PointFromJson(itemObject, point);
data->UpdateGObject(point.id(), new VPointF(point)); data->UpdateGObject(point.id(), new VPointF(point));
break; break;
} }

View File

@ -127,7 +127,7 @@ protected:
static auto ReadPointData(const QJsonObject &pointObject) -> T; static auto ReadPointData(const QJsonObject &pointObject) -> T;
template <class T> template <class T>
static auto PointFromJson(const QJsonObject &pointObject) -> T; static auto PointFromJson(const QJsonObject &pointObject, T &point) -> void;
auto QLineFromJson(const QJsonObject &itemObject) -> QLineF; auto QLineFromJson(const QJsonObject &itemObject) -> QLineF;
void SplineFromJson(const QJsonObject &itemObject, QSharedPointer<VContainer> &data); void SplineFromJson(const QJsonObject &itemObject, QSharedPointer<VContainer> &data);
@ -151,13 +151,16 @@ inline auto AbstractTest::VectorFromJson(const QString &json) -> QVector<T>
TestRoot(vectorObject, vectorKey, json); TestRoot(vectorObject, vectorKey, json);
QJsonArray vectorArray = vectorObject[vectorKey].toArray(); QJsonArray vectorArray = vectorObject[vectorKey].toArray();
QVector<T> vector(vectorArray.size()); QVector<T> vector;
vector.reserve(vectorArray.size());
for (auto && item : vectorArray) for (auto && item : vectorArray)
{ {
try try
{ {
vector.append(PointFromJson<T>(item.toObject())); T point;
PointFromJson(item.toObject(), point);
vector.append(point);
} }
catch (const VException &e) catch (const VException &e)
{ {
@ -177,7 +180,34 @@ inline void AbstractTest::CheckClassType(const QJsonObject &itemObject)
QString type; QString type;
AbstractTest::ReadStringValue(itemObject, typeKey, type); AbstractTest::ReadStringValue(itemObject, typeKey, type);
if (type != typeid(T).name()) const QStringList types
{
QStringLiteral("QPointF"), // 0
QStringLiteral("VLayoutPoint"), // 1
QStringLiteral("VRawSAPoint"), // 2
QStringLiteral("VSAPoint"), // 3
};
bool res = false;
switch (types.indexOf(type))
{
case 0:
res = (typeid(T) == typeid(QPointF));
break;
case 1:
res = (typeid(T) == typeid(VLayoutPoint));
break;
case 2:
res = (typeid(T) == typeid(VRawSAPoint));
break;
case 3:
res = (typeid(T) == typeid(VSAPoint));
break;
default:
break;
}
if (not res)
{ {
throw VException(QStringLiteral("Unexpected class '%2'.").arg(itemObject[typeKey].toString())); throw VException(QStringLiteral("Unexpected class '%2'.").arg(itemObject[typeKey].toString()));
} }
@ -201,18 +231,16 @@ inline auto AbstractTest::ReadPointData(const QJsonObject &pointObject) -> T
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
template<class T> template<class T>
inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject) -> T inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject, T &point) -> void
{ {
CheckClassType<T>(pointObject); CheckClassType<T>(pointObject);
return ReadPointData<T>(pointObject); point = ReadPointData<T>(pointObject);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
template<> template<>
inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject) -> VPointF inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject, VPointF &point) -> void
{ {
CheckClassType<VPointF>(pointObject);
vidtype id = NULL_ID; vidtype id = NULL_ID;
AbstractTest::ReadDoubleValue(pointObject, QStringLiteral("id"), id); AbstractTest::ReadDoubleValue(pointObject, QStringLiteral("id"), id);
@ -231,10 +259,8 @@ inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject) -> VPoin
qreal y = 0; qreal y = 0;
AbstractTest::ReadDoubleValue(pointObject, QChar('y'), y); AbstractTest::ReadDoubleValue(pointObject, QChar('y'), y);
VPointF point(x, y, name, mx, my); point = VPointF(x, y, name, mx, my);
point.setId(id); point.setId(id);
return point;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -256,19 +282,19 @@ inline auto AbstractTest::ReadPointData(const QJsonObject &pointObject) -> VLayo
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
template<> template<>
inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject) -> VLayoutPoint inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject, VLayoutPoint &point) -> void
{ {
CheckClassType<VLayoutPoint>(pointObject); CheckClassType<VLayoutPoint>(pointObject);
return ReadPointData<VLayoutPoint>(pointObject); point = ReadPointData<VLayoutPoint>(pointObject);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
template<> template<>
inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject) -> VSAPoint inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject, VSAPoint &point) -> void
{ {
CheckClassType<VSAPoint>(pointObject); CheckClassType<VSAPoint>(pointObject);
VSAPoint point(ReadPointData<VLayoutPoint>(pointObject)); point = VSAPoint(ReadPointData<VLayoutPoint>(pointObject));
qreal saBefore; qreal saBefore;
AbstractTest::ReadDoubleValue(pointObject, QStringLiteral("saBefore"), saBefore, QStringLiteral("-1")); AbstractTest::ReadDoubleValue(pointObject, QStringLiteral("saBefore"), saBefore, QStringLiteral("-1"));
@ -282,23 +308,19 @@ inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject) -> VSAPo
AbstractTest::ReadDoubleValue(pointObject, QStringLiteral("angle"), angleType, AbstractTest::ReadDoubleValue(pointObject, QStringLiteral("angle"), angleType,
QString::number(static_cast<int>(PieceNodeAngle::ByLength))); QString::number(static_cast<int>(PieceNodeAngle::ByLength)));
point.SetAngleType(angleType); point.SetAngleType(angleType);
return point;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
template<> template<>
inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject) -> VRawSAPoint inline auto AbstractTest::PointFromJson(const QJsonObject &pointObject, VRawSAPoint &point) -> void
{ {
CheckClassType<VRawSAPoint>(pointObject); CheckClassType<VRawSAPoint>(pointObject);
VRawSAPoint point(ReadPointData<VLayoutPoint>(pointObject)); point = VRawSAPoint(ReadPointData<VLayoutPoint>(pointObject));
bool loopPoint; bool loopPoint;
AbstractTest::ReadBooleanValue(pointObject, QStringLiteral("loopPoint"), loopPoint, QStringLiteral("0")); AbstractTest::ReadBooleanValue(pointObject, QStringLiteral("loopPoint"), loopPoint, QStringLiteral("0"));
point.SetLoopPoint(loopPoint); point.SetLoopPoint(loopPoint);
return point;
} }
#endif // ABSTRACTTEST_H #endif // ABSTRACTTEST_H

View File

@ -47,7 +47,7 @@ void TST_VAbstractPiece::EquidistantRemoveLoop_data()
QTest::addColumn<qreal>("width"); QTest::addColumn<qreal>("width");
QTest::addColumn<QVector<QPointF>>("ekvOrig"); QTest::addColumn<QVector<QPointF>>("ekvOrig");
auto ASSERT_TEST_CASE = [this](const char *title, const QString &input, const QString &output, qreal width) auto ASSERT_TEST_CASE = [](const char *title, const QString &input, const QString &output, qreal width)
{ {
QVector<VSAPoint> inputPoints = AbstractTest::VectorFromJson<VSAPoint>(input); QVector<VSAPoint> inputPoints = AbstractTest::VectorFromJson<VSAPoint>(input);
QVector<QPointF> outputPoints = AbstractTest::VectorFromJson<QPointF>(output); QVector<QPointF> outputPoints = AbstractTest::VectorFromJson<QPointF>(output);
@ -322,7 +322,7 @@ void TST_VAbstractPiece::LayoutAllowanceRemoveLoop_data()
QTest::addColumn<qreal>("width"); QTest::addColumn<qreal>("width");
QTest::addColumn<QVector<QPointF>>("ekvOrig"); QTest::addColumn<QVector<QPointF>>("ekvOrig");
auto ASSERT_TEST_CASE = [this](const char *title, const QString &input, const QString &output, qreal width) auto ASSERT_TEST_CASE = [](const char *title, const QString &input, const QString &output, qreal width)
{ {
QVector<VSAPoint> inputPoints = AbstractTest::VectorFromJson<VSAPoint>(input); QVector<VSAPoint> inputPoints = AbstractTest::VectorFromJson<VSAPoint>(input);
QVector<QPointF> outputPoints = AbstractTest::VectorFromJson<QPointF>(output); QVector<QPointF> outputPoints = AbstractTest::VectorFromJson<QPointF>(output);
@ -385,7 +385,7 @@ void TST_VAbstractPiece::RawPathRemoveLoop_data() const
QTest::addColumn<QVector<VRawSAPoint>>("path"); QTest::addColumn<QVector<VRawSAPoint>>("path");
QTest::addColumn<QVector<QPointF>>("expect"); QTest::addColumn<QVector<QPointF>>("expect");
auto ASSERT_TEST_CASE = [this](const char *title, const QString &input, const QString &output) auto ASSERT_TEST_CASE = [](const char *title, const QString &input, const QString &output)
{ {
QVector<VRawSAPoint> inputPoints = AbstractTest::VectorFromJson<VRawSAPoint>(input); QVector<VRawSAPoint> inputPoints = AbstractTest::VectorFromJson<VRawSAPoint>(input);
QVector<QPointF> outputPoints = AbstractTest::VectorFromJson<QPointF>(output); QVector<QPointF> outputPoints = AbstractTest::VectorFromJson<QPointF>(output);
@ -875,7 +875,7 @@ void TST_VAbstractPiece::BrokenDetailEquidistant_data()
QTest::addColumn<qreal>("width"); QTest::addColumn<qreal>("width");
QTest::addColumn<QVector<QPointF>>("ekvOrig"); QTest::addColumn<QVector<QPointF>>("ekvOrig");
auto ASSERT_TEST_CASE = [this](const char *title, const QString &input, const QString &output, qreal width) auto ASSERT_TEST_CASE = [](const char *title, const QString &input, const QString &output, qreal width)
{ {
QVector<VSAPoint> inputPoints = AbstractTest::VectorFromJson<VSAPoint>(input); QVector<VSAPoint> inputPoints = AbstractTest::VectorFromJson<VSAPoint>(input);
QVector<QPointF> outputPoints = AbstractTest::VectorFromJson<QPointF>(output); QVector<QPointF> outputPoints = AbstractTest::VectorFromJson<QPointF>(output);
@ -979,7 +979,7 @@ void TST_VAbstractPiece::EquidistantAngleType_data()
QTest::addColumn<qreal>("width"); QTest::addColumn<qreal>("width");
QTest::addColumn<QVector<QPointF>>("ekvOrig"); QTest::addColumn<QVector<QPointF>>("ekvOrig");
auto ASSERT_TEST_CASE = [this](const char *title, const QString &input, const QString &output, qreal width) auto ASSERT_TEST_CASE = [](const char *title, const QString &input, const QString &output, qreal width)
{ {
QVector<VSAPoint> inputPoints = AbstractTest::VectorFromJson<VSAPoint>(input); QVector<VSAPoint> inputPoints = AbstractTest::VectorFromJson<VSAPoint>(input);
QVector<QPointF> outputPoints = AbstractTest::VectorFromJson<QPointF>(output); QVector<QPointF> outputPoints = AbstractTest::VectorFromJson<QPointF>(output);
@ -1128,7 +1128,7 @@ void TST_VAbstractPiece::TestCorrectEquidistantPoints_data()
QTest::addColumn<QVector<QPointF>>("before"); QTest::addColumn<QVector<QPointF>>("before");
QTest::addColumn<QVector<QPointF>>("expect"); QTest::addColumn<QVector<QPointF>>("expect");
auto ASSERT_TEST_CASE = [this](const char *title, const QString &input, const QString &output) auto ASSERT_TEST_CASE = [](const char *title, const QString &input, const QString &output)
{ {
QVector<QPointF> inputPoints = AbstractTest::VectorFromJson<QPointF>(input); QVector<QPointF> inputPoints = AbstractTest::VectorFromJson<QPointF>(input);
QVector<QPointF> outputPoints = AbstractTest::VectorFromJson<QPointF>(output); QVector<QPointF> outputPoints = AbstractTest::VectorFromJson<QPointF>(output);
@ -1280,7 +1280,7 @@ void TST_VAbstractPiece::IsAllowanceValid_data() const
QTest::addColumn<QVector<QPointF>>("allowance"); QTest::addColumn<QVector<QPointF>>("allowance");
QTest::addColumn<bool>("valid"); QTest::addColumn<bool>("valid");
auto ASSERT_TEST_CASE = [this](const char *title, const QString &base, const QString &allowance, bool valid) auto ASSERT_TEST_CASE = [](const char *title, const QString &base, const QString &allowance, bool valid)
{ {
QVector<QPointF> basePoints = AbstractTest::VectorFromJson<QPointF>(base); QVector<QPointF> basePoints = AbstractTest::VectorFromJson<QPointF>(base);
QVector<QPointF> allowancePoints = AbstractTest::VectorFromJson<QPointF>(allowance); QVector<QPointF> allowancePoints = AbstractTest::VectorFromJson<QPointF>(allowance);

View File

@ -44,21 +44,28 @@ TST_VPiece::TST_VPiece(QObject *parent)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void TST_VPiece::Issue620() void TST_VPiece::Issue620()
{ {
// See file <root>/src/app/share/collection/bugs/Issue_#620.vit try
// Check main path {
const Unit unit = Unit::Cm; // See file <root>/src/app/share/collection/bugs/Issue_#620.vit
QSharedPointer<VContainer> data(new VContainer(nullptr, &unit, VContainer::UniqueNamespace())); // Check main path
VAbstractValApplication::VApp()->SetPatternUnits(unit); const Unit unit = Unit::Cm;
QSharedPointer<VContainer> data(new VContainer(nullptr, &unit, VContainer::UniqueNamespace()));
VAbstractValApplication::VApp()->SetPatternUnits(unit);
VPiece detail; VPiece detail;
AbstractTest::PieceFromJson(QStringLiteral("://Issue_620/input.json"), detail, data); AbstractTest::PieceFromJson(QStringLiteral("://Issue_620/input.json"), detail, data);
QVector<QPointF> pointsEkv; QVector<QPointF> pointsEkv;
CastTo(detail.MainPathPoints(data.data()), pointsEkv); CastTo(detail.MainPathPoints(data.data()), pointsEkv);
QVector<QPointF> origPoints = AbstractTest::VectorFromJson<QPointF>(QStringLiteral("://Issue_620/output.json")); QVector<QPointF> origPoints = AbstractTest::VectorFromJson<QPointF>(QStringLiteral("://Issue_620/output.json"));
// Begin comparison // Begin comparison
ComparePathsDistance(pointsEkv, origPoints); ComparePathsDistance(pointsEkv, origPoints);
}
catch (const VException &e)
{
QFAIL(qUtf8Printable(e.ErrorMessage()));
}
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------