mirror of
https://github.com/qgis/QGIS.git
synced 2025-03-31 00:03:42 -04:00
reading ogr JSON basics
This commit is contained in:
parent
31b82de5e3
commit
5526975403
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user