Skip to content

Commit 5526975

Browse files
committedDec 11, 2018
reading ogr JSON basics
1 parent 31b82de commit 5526975

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed
 

‎src/core/qgsfield.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ QString QgsField::displayString( const QVariant &v ) const
254254
if ( ok )
255255
return QLocale().toString( converted );
256256
}
257-
else if ( d->typeName == QLatin1String( "json" ) || d->typeName == QLatin1String( "jsonb" ) )
257+
else if ( d->typeName == QLatin1String( "json" ) || d->typeName == QLatin1String( "jsonb" ) || d->typeName == QLatin1String( "JSON" ) )
258258
{
259259
QJsonDocument doc = QJsonDocument::fromVariant( v );
260260
return QString::fromUtf8( doc.toJson().data() );

‎src/core/qgsogrutils.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <QTextCodec>
2323
#include <QUuid>
2424
#include <cpl_error.h>
25+
#include <QJsonDocument>
2526

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

240+
case QVariant::Map:
241+
{
242+
//it has to be JSON
243+
//it's null if no json format
244+
if ( encoding )
245+
value = QJsonDocument::fromJson( encoding->toUnicode( OGR_F_GetFieldAsString( ogrFet, attIndex ) ).toUtf8() ).toVariant();
246+
else
247+
value = QJsonDocument::fromJson( QString::fromUtf8( OGR_F_GetFieldAsString( ogrFet, attIndex ) ).toUtf8() ).toVariant();
248+
break;
249+
}
239250
default:
240251
Q_ASSERT_X( false, "QgsOgrUtils::getOgrFeatureAttribute", "unsupported field type" );
241252
if ( ok )

‎src/providers/ogr/qgsogrprovider.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,8 @@ QgsOgrProvider::QgsOgrProvider( QString const &uri, const ProviderOptions &optio
470470
<< QgsVectorDataProvider::NativeType( tr( "Whole number (integer)" ), QStringLiteral( "integer" ), QVariant::Int, 0, nMaxIntLen )
471471
<< QgsVectorDataProvider::NativeType( tr( "Whole number (integer 64 bit)" ), QStringLiteral( "integer64" ), QVariant::LongLong, 0, nMaxInt64Len )
472472
<< QgsVectorDataProvider::NativeType( tr( "Decimal number (real)" ), QStringLiteral( "double" ), QVariant::Double, 0, nMaxDoubleLen, 0, nMaxDoublePrec )
473-
<< QgsVectorDataProvider::NativeType( tr( "Text (string)" ), QStringLiteral( "string" ), QVariant::String, 0, 65535 );
473+
<< QgsVectorDataProvider::NativeType( tr( "Text (string)" ), QStringLiteral( "string" ), QVariant::String, 0, 65535 )
474+
<< QgsVectorDataProvider::NativeType( tr( "Map (json)" ), QStringLiteral( "json" ), QVariant::Map, -1, -1, -1, -1, QVariant::String );
474475

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

10111012
QVariant::Type varType;
1013+
QVariant::Type varSubType;
10121014
switch ( ogrType )
10131015
{
10141016
case OFTInteger:
@@ -1041,6 +1043,18 @@ void QgsOgrProvider::loadFields()
10411043
break;
10421044

10431045
case OFTString:
1046+
if ( OGR_Fld_GetSubType( fldDef ) == OFSTJSON )
1047+
{
1048+
ogrSubType = OFSTJSON;
1049+
QgsDebugMsg( QStringLiteral( "JSON Field found" ) );
1050+
varType = QVariant::Map;
1051+
varSubType = QVariant::String;
1052+
}
1053+
else
1054+
{
1055+
varType = QVariant::String;
1056+
}
1057+
break;
10441058
default:
10451059
varType = QVariant::String; // other unsupported, leave it as a string
10461060
}
@@ -1081,7 +1095,7 @@ void QgsOgrProvider::loadFields()
10811095
#else
10821096
textEncoding()->toUnicode( typeName.toStdString().c_str() ),
10831097
#endif
1084-
width, prec
1098+
width, prec, QString(), varSubType
10851099
);
10861100

10871101
// check if field is nullable

0 commit comments

Comments
 (0)
Please sign in to comment.