Skip to content

Commit

Permalink
reading ogr JSON basics
Browse files Browse the repository at this point in the history
  • Loading branch information
signedav committed Dec 11, 2018
1 parent 31b82de commit 5526975
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/core/qgsfield.cpp
Expand Up @@ -254,7 +254,7 @@ QString QgsField::displayString( const QVariant &v ) const
if ( ok )
return QLocale().toString( converted );
}
else if ( d->typeName == QLatin1String( "json" ) || d->typeName == QLatin1String( "jsonb" ) )
else if ( d->typeName == QLatin1String( "json" ) || d->typeName == QLatin1String( "jsonb" ) || d->typeName == QLatin1String( "JSON" ) )
{
QJsonDocument doc = QJsonDocument::fromVariant( v );
return QString::fromUtf8( doc.toJson().data() );
Expand Down
11 changes: 11 additions & 0 deletions src/core/qgsogrutils.cpp
Expand Up @@ -22,6 +22,7 @@
#include <QTextCodec>
#include <QUuid>
#include <cpl_error.h>
#include <QJsonDocument>

// Starting with GDAL 2.2, there are 2 concepts: unset fields and null fields
// whereas previously there was only unset fields. For QGIS purposes, both
Expand Down Expand Up @@ -236,6 +237,16 @@ QVariant QgsOgrUtils::getOgrFeatureAttribute( OGRFeatureH ogrFet, const QgsField
break;
}

case QVariant::Map:
{
//it has to be JSON
//it's null if no json format
if ( encoding )
value = QJsonDocument::fromJson( encoding->toUnicode( OGR_F_GetFieldAsString( ogrFet, attIndex ) ).toUtf8() ).toVariant();
else
value = QJsonDocument::fromJson( QString::fromUtf8( OGR_F_GetFieldAsString( ogrFet, attIndex ) ).toUtf8() ).toVariant();
break;
}
default:
Q_ASSERT_X( false, "QgsOgrUtils::getOgrFeatureAttribute", "unsupported field type" );
if ( ok )
Expand Down
18 changes: 16 additions & 2 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -470,7 +470,8 @@ QgsOgrProvider::QgsOgrProvider( QString const &uri, const ProviderOptions &optio
<< QgsVectorDataProvider::NativeType( tr( "Whole number (integer)" ), QStringLiteral( "integer" ), QVariant::Int, 0, nMaxIntLen )
<< QgsVectorDataProvider::NativeType( tr( "Whole number (integer 64 bit)" ), QStringLiteral( "integer64" ), QVariant::LongLong, 0, nMaxInt64Len )
<< QgsVectorDataProvider::NativeType( tr( "Decimal number (real)" ), QStringLiteral( "double" ), QVariant::Double, 0, nMaxDoubleLen, 0, nMaxDoublePrec )
<< QgsVectorDataProvider::NativeType( tr( "Text (string)" ), QStringLiteral( "string" ), QVariant::String, 0, 65535 );
<< QgsVectorDataProvider::NativeType( tr( "Text (string)" ), QStringLiteral( "string" ), QVariant::String, 0, 65535 )
<< QgsVectorDataProvider::NativeType( tr( "Map (json)" ), QStringLiteral( "json" ), QVariant::Map, -1, -1, -1, -1, QVariant::String );

bool supportsDate = true;
bool supportsTime = mGDALDriverName != QLatin1String( "ESRI Shapefile" ) && mGDALDriverName != QLatin1String( "GPKG" );
Expand Down Expand Up @@ -1009,6 +1010,7 @@ void QgsOgrProvider::loadFields()
OGRFieldSubType ogrSubType = OFSTNone;

QVariant::Type varType;
QVariant::Type varSubType;
switch ( ogrType )
{
case OFTInteger:
Expand Down Expand Up @@ -1041,6 +1043,18 @@ void QgsOgrProvider::loadFields()
break;

case OFTString:
if ( OGR_Fld_GetSubType( fldDef ) == OFSTJSON )
{
ogrSubType = OFSTJSON;
QgsDebugMsg( QStringLiteral( "JSON Field found" ) );
varType = QVariant::Map;
varSubType = QVariant::String;
}
else
{
varType = QVariant::String;
}
break;
default:
varType = QVariant::String; // other unsupported, leave it as a string
}
Expand Down Expand Up @@ -1081,7 +1095,7 @@ void QgsOgrProvider::loadFields()
#else
textEncoding()->toUnicode( typeName.toStdString().c_str() ),
#endif
width, prec
width, prec, QString(), varSubType
);

// check if field is nullable
Expand Down

0 comments on commit 5526975

Please sign in to comment.