Skip to content

Commit

Permalink
displayString on textoutput
Browse files Browse the repository at this point in the history
but only works if json contains a map...

and codestyle in python
  • Loading branch information
signedav authored and nyalldawson committed Sep 14, 2018
1 parent e972e21 commit 8d4ffac
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/core/qgsfield.cpp
Expand Up @@ -23,6 +23,8 @@
#include <QDataStream>
#include <QIcon>
#include <QLocale>
#include <QJsonDocument>
#include <QJsonObject>

/***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with
Expand Down Expand Up @@ -253,10 +255,13 @@ QString QgsField::displayString( const QVariant &v ) const
if ( ok )
return QLocale().toString( converted );
}
else if ( d->typeName.compare( "json" ) == 0 )
else if ( d->typeName.compare( "json" ) == 0 || d->typeName.compare( "jsonb" ) == 0 )
{
//QJsonDocument jsonDocument = variable.toJsonDocument();
//return QString::fromUtf8( jsonDocument.toJson() );
//this works only if the json/jsonb field contains a map - this has to be improved.
//toJsonDocument works only if the usertype is QJsonDocument
QJsonValue value = QJsonValue::fromVariant( v );
QJsonDocument doc( value.toObject() );
return QString::fromUtf8( doc.toJson( QJsonDocument::Indented ).data() );
}
// Fallback if special rules do not apply
return v.toString();
Expand Down
2 changes: 1 addition & 1 deletion src/gui/editorwidgets/qgslistwidgetfactory.cpp
Expand Up @@ -44,5 +44,5 @@ QgsEditorConfigWidget *QgsListWidgetFactory::configWidget( QgsVectorLayer *vl, i
unsigned int QgsListWidgetFactory::fieldScore( const QgsVectorLayer *vl, int fieldIdx ) const
{
const QgsField field = vl->fields().field( fieldIdx );
return ( field.type() == QVariant::List || field.type() == QVariant::StringList ) && field.subType() != QVariant::Invalid ? 20 : 0;
return ( field.type() == QVariant::List || field.type() == QVariant::StringList || field.typeName().compare( QLatin1String( "json" ) ) == 0 || field.typeName().compare( QLatin1String( "jsonb" ) ) == 0 ) && field.subType() != QVariant::Invalid ? 20 : 0;
}
6 changes: 3 additions & 3 deletions tests/src/providers/testqgspostgresprovider.cpp
Expand Up @@ -124,16 +124,16 @@ class TestQgsPostgresProvider: public QObject
qDebug() << "actual: " << decoded;
QCOMPARE( decoded.toMap(), expected );
}
/* now working yet
/*
void decodeJsonInt()
{
const QVariant decoded = QgsPostgresProvider::convertValue( QVariant::Map, QVariant::String, QStringLiteral( "'123'" ), QStringLiteral( "json" ) );
QCOMPARE( decoded.type(), QVariant::Int );
int expected;
expected=123;
expected="'123'";
qDebug() << "actual: " << decoded;
QCOMPARE( decoded.toInt(), expected );
QCOMPARE( decoded., expected );
}
void decodeJsonbInt()
{
Expand Down

0 comments on commit 8d4ffac

Please sign in to comment.