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 c287d2d

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

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
@@ -572,16 +572,28 @@ bool QgsField::convertCompatible( QVariant &v, QString *errorMessage ) const
572572
}
573573
}
574574

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

587599
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
@@ -784,19 +784,37 @@ void TestQgsField::convertCompatible()
784784
QVariant vZero = 0;
785785
QVERIFY( intField.convertCompatible( vZero ) );
786786

787-
// Test json field conversion
788-
const QgsField jsonField( QStringLiteral( "json" ), QVariant::String, QStringLiteral( "json" ) );
789-
QVariant jsonValue = QVariant::fromValue( QVariantList() << 1 << 5 << 8 );
790-
QVERIFY( jsonField.convertCompatible( jsonValue ) );
791-
QCOMPARE( jsonValue.type(), QVariant::String );
792-
QCOMPARE( jsonValue, QString( "[1,5,8]" ) );
793-
QVariantMap variantMap;
794-
variantMap.insert( QStringLiteral( "a" ), 1 );
795-
variantMap.insert( QStringLiteral( "c" ), 3 );
796-
jsonValue = QVariant::fromValue( variantMap );
797-
QVERIFY( jsonField.convertCompatible( jsonValue ) );
798-
QCOMPARE( jsonValue.type(), QVariant::String );
799-
QCOMPARE( jsonValue, QString( "{\"a\":1,\"c\":3}" ) );
787+
// Test string-based json field conversion
788+
{
789+
const QgsField jsonField( QStringLiteral( "json" ), QVariant::String, QStringLiteral( "json" ) );
790+
QVariant jsonValue = QVariant::fromValue( QVariantList() << 1 << 5 << 8 );
791+
QVERIFY( jsonField.convertCompatible( jsonValue ) );
792+
QCOMPARE( jsonValue.type(), QVariant::String );
793+
QCOMPARE( jsonValue, QString( "[1,5,8]" ) );
794+
QVariantMap variantMap;
795+
variantMap.insert( QStringLiteral( "a" ), 1 );
796+
variantMap.insert( QStringLiteral( "c" ), 3 );
797+
jsonValue = QVariant::fromValue( variantMap );
798+
QVERIFY( jsonField.convertCompatible( jsonValue ) );
799+
QCOMPARE( jsonValue.type(), QVariant::String );
800+
QCOMPARE( jsonValue, QString( "{\"a\":1,\"c\":3}" ) );
801+
}
802+
803+
// Test map-based json field (i.e. OGR geopackage JSON fields) conversion
804+
{
805+
const QgsField jsonField( QStringLiteral( "json" ), QVariant::Map, QStringLiteral( "json" ) );
806+
QVariant jsonValue = QVariant::fromValue( QVariantList() << 1 << 5 << 8 );
807+
QVERIFY( jsonField.convertCompatible( jsonValue ) );
808+
QCOMPARE( jsonValue.type(), QVariant::List );
809+
QCOMPARE( jsonValue, QVariantList() << 1 << 5 << 8 );
810+
QVariantMap variantMap;
811+
variantMap.insert( QStringLiteral( "a" ), 1 );
812+
variantMap.insert( QStringLiteral( "c" ), 3 );
813+
jsonValue = QVariant::fromValue( variantMap );
814+
QVERIFY( jsonField.convertCompatible( jsonValue ) );
815+
QCOMPARE( jsonValue.type(), QVariant::Map );
816+
QCOMPARE( jsonValue, variantMap );
817+
}
800818

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

0 commit comments

Comments
 (0)
Please sign in to comment.