Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
check for GDAL Version and exclude by ifdef
  • Loading branch information
signedav committed Dec 18, 2018
1 parent de12239 commit 40c6896
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/core/qgsogrutils.cpp
Expand Up @@ -152,11 +152,13 @@ QgsFields QgsOgrUtils::readOgrFields( OGRFeatureH ogrFet, QTextCodec *encoding )
varType = QVariant::DateTime;
break;
case OFTString:
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,4,0)
if ( OGR_Fld_GetSubType( fldDef ) == OFSTJSON )
varType = QVariant::Map;
else
varType = QVariant::String;
break;
#endif
default:
varType = QVariant::String; // other unsupported, leave it as a string
}
Expand Down
19 changes: 15 additions & 4 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -155,12 +155,12 @@ bool QgsOgrProvider::convertField( QgsField &field, const QTextCodec &encoding )
ogrType = OFTDateTime;
break;
//not sure if needed, but for consistency
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,4,0)
case QVariant::Map:
ogrType = OFTString;
ogrSubType = OFSTJSON;
break;
#endif
default:
return false;
}
Expand Down Expand Up @@ -1049,6 +1049,7 @@ void QgsOgrProvider::loadFields()
break;

case OFTString:
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,4,0)
if ( OGR_Fld_GetSubType( fldDef ) == OFSTJSON )
{
ogrSubType = OFSTJSON;
Expand All @@ -1061,6 +1062,7 @@ void QgsOgrProvider::loadFields()
varType = QVariant::String;
}
break;
#endif
default:
varType = QVariant::String; // other unsupported, leave it as a string
}
Expand Down Expand Up @@ -1531,6 +1533,8 @@ bool QgsOgrProvider::addFeaturePrivate( QgsFeature &f, Flags flags )
case OFTString:
{
QString stringValue;

#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,4,0)
if ( OGR_Fld_GetSubType( fldDef ) == OFSTJSON )
{
stringValue = QString::fromUtf8( QJsonDocument::fromVariant( attrVal.toMap() ).toJson().data() );
Expand All @@ -1543,7 +1547,9 @@ bool QgsOgrProvider::addFeaturePrivate( QgsFeature &f, Flags flags )
{
stringValue = attrVal.toString();
}

#else
stringValue = attrVal.toString();
#endif
QgsDebugMsgLevel( QStringLiteral( "Writing string attribute %1 with %2, encoding %3" )
.arg( qgisAttId )
.arg( attrVal.toString(),
Expand Down Expand Up @@ -1689,9 +1695,11 @@ bool QgsOgrProvider::addAttributeOGRLevel( const QgsField &field, bool &ignoreEr
case QVariant::Bool:
OGR_Fld_SetSubType( fielddefn.get(), OFSTBoolean );
break;
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,4,0)
case QVariant::Map:
OGR_Fld_SetSubType( fielddefn.get(), OFSTJSON );
break;
#endif
default:
break;
}
Expand Down Expand Up @@ -2121,6 +2129,7 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap &attr_
case OFTString:
{
QString stringValue;
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,4,0)
if ( OGR_Fld_GetSubType( fd ) == OFSTJSON )
{
stringValue = QString::fromUtf8( QJsonDocument::fromVariant( it2->toMap() ).toJson().data() );
Expand All @@ -2133,7 +2142,9 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap &attr_
{
stringValue = it2->toString();
}

#else
stringValue = it2->toString();
#endif
OGR_F_SetFieldString( of.get(), f, textEncoding()->fromUnicode( stringValue ).constData() );
break;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/src/python/test_provider_ogr_gpkg.py
Expand Up @@ -1231,6 +1231,9 @@ def testTransaction(self):
del vl2_external

def testJson(self):
if int(gdal.VersionInfo('VERSION_NUM')) < GDAL_COMPUTE_VERSION(2, 4, 0):
return

tmpfile = os.path.join(self.basetestpath, 'test_json.gpkg')
testdata_path = unitTestDataPath('provider')
shutil.copy(os.path.join(unitTestDataPath('provider'), 'test_json.gpkg'), tmpfile)
Expand Down

0 comments on commit 40c6896

Please sign in to comment.