Skip to content

Commit

Permalink
basic writing json gpkg
Browse files Browse the repository at this point in the history
  • Loading branch information
signedav committed Dec 11, 2018
1 parent 607dffd commit 8f5a732
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/core/qgsogrutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ QgsFields QgsOgrUtils::readOgrFields( OGRFeatureH ogrFet, QTextCodec *encoding )
varType = QVariant::DateTime;
break;
case OFTString:
if ( OGR_Fld_GetSubType( fldDef ) == OFSTJSON )
varType = QVariant::Map;
else
varType = QVariant::String;
break;
default:
varType = QVariant::String; // other unsupported, leave it as a string
}
Expand Down
31 changes: 28 additions & 3 deletions src/providers/ogr/qgsogrprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ bool QgsOgrProvider::convertField( QgsField &field, const QTextCodec &encoding )
ogrType = OFTDateTime;
break;
//not sure if needed, but for consistency
case QVariant::Map:
ogrType = OFTString;
ogrSubType = OFSTJSON;
break;
default:
return false;
}
Expand Down Expand Up @@ -1523,6 +1529,10 @@ bool QgsOgrProvider::addFeaturePrivate( QgsFeature &f, Flags flags )
break;

case OFTString:
if ( OGR_Fld_GetSubType( fldDef ) == OFSTJSON )
{
QgsDebugMsgLevel( QStringLiteral( "Does it need to be decoded or something DAVE? JSON Stuff" ), 3 );
}
QgsDebugMsgLevel( QStringLiteral( "Writing string attribute %1 with %2, encoding %3" )
.arg( qgisAttId )
.arg( attrVal.toString(),
Expand Down Expand Up @@ -1647,7 +1657,9 @@ bool QgsOgrProvider::addAttributeOGRLevel( const QgsField &field, bool &ignoreEr
case QVariant::ByteArray:
type = OFTBinary;
break;

case QVariant::Map:
type = OFTString;
break;
default:
pushError( tr( "type %1 for field %2 not found" ).arg( field.typeName(), field.name() ) );
ignoreErrorOut = true;
Expand All @@ -1666,7 +1678,9 @@ bool QgsOgrProvider::addAttributeOGRLevel( const QgsField &field, bool &ignoreEr
case QVariant::Bool:
OGR_Fld_SetSubType( fielddefn.get(), OFSTBoolean );
break;

case QVariant::Map:
OGR_Fld_SetSubType( fielddefn.get(), OFSTJSON );
break;
default:
break;
}
Expand Down Expand Up @@ -2094,8 +2108,19 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap &attr_
0 );
break;
case OFTString:
OGR_F_SetFieldString( of.get(), f, textEncoding()->fromUnicode( it2->toString() ).constData() );
{
QString stringValue;
if ( OGR_Fld_GetSubType( fd ) == OFSTJSON )
{
stringValue = QString::fromUtf8( QJsonDocument::fromVariant( it2->toMap() ).toJson().data() );
}
else
{
stringValue = it2->toString();
}
OGR_F_SetFieldString( of.get(), f, textEncoding()->fromUnicode( stringValue ).constData() );
break;
}

case OFTBinary:
{
Expand Down

0 comments on commit 8f5a732

Please sign in to comment.