Skip to content
Permalink
Browse files

improve value conversion to json-string

  • Loading branch information
signedav committed Dec 20, 2018
1 parent 9ff514b commit ebb25e6b493a969e6a6590d1f27965bdff61d73c
@@ -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;
}
@@ -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
@@ -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

0 comments on commit ebb25e6

Please sign in to comment.
You can’t perform that action at this time.