Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d5200d7

Browse files
nirvngithub-actions[bot]
authored andcommittedApr 14, 2023
Fix Map-based JSON handling in QgsField's convertCompatible()
1 parent a2b21c4 commit d5200d7

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed
 

‎src/core/qgsfield.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -570,16 +570,28 @@ bool QgsField::convertCompatible( QVariant &v, QString *errorMessage ) const
570570
}
571571
}
572572

573-
if ( d->type == QVariant::String && ( d->typeName.compare( QLatin1String( "json" ), Qt::CaseInsensitive ) == 0 || d->typeName == QLatin1String( "jsonb" ) ) )
573+
if ( d->typeName.compare( QLatin1String( "json" ), Qt::CaseInsensitive ) == 0 || d->typeName.compare( QLatin1String( "jsonb" ), Qt::CaseInsensitive ) == 0 )
574574
{
575-
const QJsonDocument doc = QJsonDocument::fromVariant( v );
576-
if ( !doc.isNull() )
575+
if ( d->type == QVariant::String )
577576
{
578-
v = QString::fromUtf8( doc.toJson( QJsonDocument::Compact ).constData() );
579-
return true;
577+
const QJsonDocument doc = QJsonDocument::fromVariant( v );
578+
if ( !doc.isNull() )
579+
{
580+
v = QString::fromUtf8( doc.toJson( QJsonDocument::Compact ).constData() );
581+
return true;
582+
}
583+
v = QVariant( d->type );
584+
return false;
585+
}
586+
else if ( d->type == QVariant::Map )
587+
{
588+
if ( v.type() == QVariant::StringList || v.type() == QVariant::List || v.type() == QVariant::Map )
589+
{
590+
return true;
591+
}
592+
v = QVariant( d->type );
593+
return false;
580594
}
581-
v = QVariant( d->type );
582-
return false;
583595
}
584596

585597
if ( ( d->type == QVariant::StringList || ( d->type == QVariant::List && d->subType == QVariant::String ) )

‎tests/src/core/testqgsfield.cpp

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -806,19 +806,37 @@ void TestQgsField::convertCompatible()
806806
QVariant vZero = 0;
807807
QVERIFY( intField.convertCompatible( vZero ) );
808808

809-
// Test json field conversion
810-
const QgsField jsonField( QStringLiteral( "json" ), QVariant::String, QStringLiteral( "json" ) );
811-
QVariant jsonValue = QVariant::fromValue( QVariantList() << 1 << 5 << 8 );
812-
QVERIFY( jsonField.convertCompatible( jsonValue ) );
813-
QCOMPARE( jsonValue.type(), QVariant::String );
814-
QCOMPARE( jsonValue, QString( "[1,5,8]" ) );
815-
QVariantMap variantMap;
816-
variantMap.insert( QStringLiteral( "a" ), 1 );
817-
variantMap.insert( QStringLiteral( "c" ), 3 );
818-
jsonValue = QVariant::fromValue( variantMap );
819-
QVERIFY( jsonField.convertCompatible( jsonValue ) );
820-
QCOMPARE( jsonValue.type(), QVariant::String );
821-
QCOMPARE( jsonValue, QString( "{\"a\":1,\"c\":3}" ) );
809+
// Test string-based json field conversion
810+
{
811+
const QgsField jsonField( QStringLiteral( "json" ), QVariant::String, QStringLiteral( "json" ) );
812+
QVariant jsonValue = QVariant::fromValue( QVariantList() << 1 << 5 << 8 );
813+
QVERIFY( jsonField.convertCompatible( jsonValue ) );
814+
QCOMPARE( jsonValue.type(), QVariant::String );
815+
QCOMPARE( jsonValue, QString( "[1,5,8]" ) );
816+
QVariantMap variantMap;
817+
variantMap.insert( QStringLiteral( "a" ), 1 );
818+
variantMap.insert( QStringLiteral( "c" ), 3 );
819+
jsonValue = QVariant::fromValue( variantMap );
820+
QVERIFY( jsonField.convertCompatible( jsonValue ) );
821+
QCOMPARE( jsonValue.type(), QVariant::String );
822+
QCOMPARE( jsonValue, QString( "{\"a\":1,\"c\":3}" ) );
823+
}
824+
825+
// Test map-based json field (i.e. OGR geopackage JSON fields) conversion
826+
{
827+
const QgsField jsonField( QStringLiteral( "json" ), QVariant::Map, QStringLiteral( "json" ) );
828+
QVariant jsonValue = QVariant::fromValue( QVariantList() << 1 << 5 << 8 );
829+
QVERIFY( jsonField.convertCompatible( jsonValue ) );
830+
QCOMPARE( jsonValue.type(), QVariant::List );
831+
QCOMPARE( jsonValue, QVariantList() << 1 << 5 << 8 );
832+
QVariantMap variantMap;
833+
variantMap.insert( QStringLiteral( "a" ), 1 );
834+
variantMap.insert( QStringLiteral( "c" ), 3 );
835+
jsonValue = QVariant::fromValue( variantMap );
836+
QVERIFY( jsonField.convertCompatible( jsonValue ) );
837+
QCOMPARE( jsonValue.type(), QVariant::Map );
838+
QCOMPARE( jsonValue, variantMap );
839+
}
822840

823841
// geometry field conversion
824842
const QgsField geometryField( QStringLiteral( "geometry" ), QVariant::UserType, QStringLiteral( "geometry" ) );

0 commit comments

Comments
 (0)
Please sign in to comment.