From ebb25e6b493a969e6a6590d1f27965bdff61d73c Mon Sep 17 00:00:00 2001 From: signedav Date: Thu, 20 Dec 2018 10:34:58 +0100 Subject: [PATCH] improve value conversion to json-string --- .../editorwidgets/qgslistwidgetfactory.cpp | 2 +- src/providers/ogr/qgsogrprovider.cpp | 21 +++++++++++-------- src/providers/ogr/qgsogrprovider.h | 4 ++-- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/gui/editorwidgets/qgslistwidgetfactory.cpp b/src/gui/editorwidgets/qgslistwidgetfactory.cpp index 49261d32a35..c7a4c68e197 100644 --- a/src/gui/editorwidgets/qgslistwidgetfactory.cpp +++ b/src/gui/editorwidgets/qgslistwidgetfactory.cpp @@ -44,5 +44,5 @@ QgsEditorConfigWidget *QgsListWidgetFactory::configWidget( QgsVectorLayer *vl, i unsigned int QgsListWidgetFactory::fieldScore( const QgsVectorLayer *vl, int fieldIdx ) const { const QgsField field = vl->fields().field( fieldIdx ); - return ( field.type() == QVariant::List || field.type() == QVariant::StringList || field.typeName().compare( QLatin1String( "json" ), Qt::CaseInsensitive ) == 0 || field.typeName() == QLatin1String( "jsonb" ) ) && field.subType() != QVariant::Invalid ? 20 : 0; + return ( field.type() == QVariant::List || field.type() == QVariant::StringList || QVariant::Map ) && field.subType() != QVariant::Invalid ? 20 : 0; } diff --git a/src/providers/ogr/qgsogrprovider.cpp b/src/providers/ogr/qgsogrprovider.cpp index 4f561b54e19..d0eef19f950 100644 --- a/src/providers/ogr/qgsogrprovider.cpp +++ b/src/providers/ogr/qgsogrprovider.cpp @@ -1408,14 +1408,17 @@ OGRGeometryH QgsOgrProvider::ConvertGeometryIfNecessary( OGRGeometryH hGeom ) return OGR_G_ForceTo( hGeom, layerGeomType, nullptr ); } -QString QgsOgrProvider::jsonStringValue( const QVariant &value ) +QString QgsOgrProvider::jsonStringValue( const QVariant &value, const QVariant::Type &type ) { - QString stringValue = QString::fromUtf8( QJsonDocument::fromVariant( value.toMap() ).toJson().data() ); - if ( stringValue == QStringLiteral( "{\n}\n" ) ) - stringValue = QString::fromUtf8( QJsonDocument::fromVariant( value.toList() ).toJson().data() ); - if ( stringValue == QStringLiteral( "[\n]\n" ) && !value.toString().isEmpty() ) - stringValue = value.toString(); - return stringValue; + switch ( type ) + { + case QVariant::Map: + return QString::fromUtf8( QJsonDocument::fromVariant( value.toMap() ).toJson().data() ); + case QVariant::List: + return QString::fromUtf8( QJsonDocument::fromVariant( value.toList() ).toJson().data() ); + default: + return value.toString(); + } } bool QgsOgrProvider::addFeaturePrivate( QgsFeature &f, Flags flags ) @@ -1547,7 +1550,7 @@ bool QgsOgrProvider::addFeaturePrivate( QgsFeature &f, Flags flags ) #if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,4,0) if ( OGR_Fld_GetSubType( fldDef ) == OFSTJSON ) - stringValue = jsonStringValue( attrVal ); + stringValue = jsonStringValue( attrVal, attrVal.type() ); else { stringValue = attrVal.toString(); @@ -2136,7 +2139,7 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap &attr_ QString stringValue; #if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,4,0) if ( OGR_Fld_GetSubType( fd ) == OFSTJSON ) - stringValue = jsonStringValue( it2.value() ); + stringValue = jsonStringValue( it2.value(), it2->type() ); else stringValue = it2->toString(); #else diff --git a/src/providers/ogr/qgsogrprovider.h b/src/providers/ogr/qgsogrprovider.h index a5c7f148dd1..0a4c22f634b 100644 --- a/src/providers/ogr/qgsogrprovider.h +++ b/src/providers/ogr/qgsogrprovider.h @@ -284,8 +284,8 @@ class QgsOgrProvider : public QgsVectorDataProvider mutable QStringList mSubLayerList; - //! convert from json QVariant to QString - QString jsonStringValue( const QVariant &value ); + //! converts \a value from json QVariant to QString regarding to \a type + QString jsonStringValue( const QVariant &value, const QVariant::Type &type ); bool addFeaturePrivate( QgsFeature &f, QgsFeatureSink::Flags flags ); //! Deletes one feature