Skip to content
Permalink
Browse files

reading ogr JSON basics

  • Loading branch information
signedav committed Dec 11, 2018
1 parent 31b82de commit 55269754030c5fb85e36e6de869064e6fc70ea6f
Showing with 28 additions and 3 deletions.
  1. +1 −1 src/core/qgsfield.cpp
  2. +11 −0 src/core/qgsogrutils.cpp
  3. +16 −2 src/providers/ogr/qgsogrprovider.cpp
@@ -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() );
@@ -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
@@ -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 )
@@ -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" );
@@ -1009,6 +1010,7 @@ void QgsOgrProvider::loadFields()
OGRFieldSubType ogrSubType = OFSTNone;

QVariant::Type varType;
QVariant::Type varSubType;
switch ( ogrType )
{
case OFTInteger:
@@ -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
}
@@ -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

0 comments on commit 5526975

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