Merge pull request #3518 from pvalsecc/qt5_json

Use the Qt5 JSON classes
This commit is contained in:
Nyall Dawson 2016-09-23 14:30:31 +10:00 committed by GitHub
commit e1386d41ef
2 changed files with 21 additions and 100 deletions

View File

@ -23,6 +23,10 @@
#include "qgsrelationmanager.h" #include "qgsrelationmanager.h"
#include "qgsproject.h" #include "qgsproject.h"
#include "qgscsexception.h" #include "qgscsexception.h"
#include "qgslogger.h"
#include <QJsonDocument>
#include <QJsonArray>
QgsJSONExporter::QgsJSONExporter( const QgsVectorLayer* vectorLayer, int precision ) QgsJSONExporter::QgsJSONExporter( const QgsVectorLayer* vectorLayer, int precision )
: mPrecision( precision ) : mPrecision( precision )
@ -246,38 +250,9 @@ QString QgsJSONUtils::encodeValue( const QVariant &value )
return value.toBool() ? "true" : "false"; return value.toBool() ? "true" : "false";
case QVariant::StringList: case QVariant::StringList:
{
QStringList input = value.toStringList();
QStringList output;
Q_FOREACH ( const QString& string, input )
{
output << encodeValue( string );
}
return output.join( "," ).prepend( '[' ).append( ']' );
}
case QVariant::List: case QVariant::List:
{
QVariantList input = value.toList();
QStringList output;
Q_FOREACH ( const QVariant& v, input )
{
output << encodeValue( v );
}
return output.join( "," ).prepend( '[' ).append( ']' );
}
case QVariant::Map: case QVariant::Map:
{ return QString::fromUtf8( QJsonDocument::fromVariant( value ).toJson( QJsonDocument::Compact ) );
QMap< QString, QVariant > input = value.toMap();
QStringList output;
QMap< QString, QVariant >::const_iterator it = input.constBegin();
for ( ; it != input.constEnd(); ++it )
{
output << encodeValue( it.key() ) + ':' + encodeValue( it.value() );
}
return output.join( ",\n" ).prepend( '{' ).append( '}' );
}
default: default:
case QVariant::String: case QVariant::String:
@ -309,82 +284,28 @@ QString QgsJSONUtils::exportAttributes( const QgsFeature& feature )
return attrs.prepend( '{' ).append( '}' ); return attrs.prepend( '{' ).append( '}' );
} }
namespace //TODO: remove when we switch off Qt4
{
void jumpSpace( const QString& txt, int& i )
{
while ( i < txt.length() && txt.at( i ).isSpace() ) ++i;
}
static QString getNextString( const QString& txt, int& i )
{
jumpSpace( txt, i );
QString cur = txt.mid( i );
if ( cur.startsWith( '"' ) )
{ //quoted element
QRegExp stringRe( "^\"((?:\\\\.|[^\"\\\\])*)\".*" );
if ( !stringRe.exactMatch( cur ) )
{
return QString::null;
}
i += stringRe.cap( 1 ).length() + 2;
jumpSpace( txt, i );
if ( !txt.mid( i ).startsWith( ',' ) && !txt.mid( i ).startsWith( ']' ) && i < txt.length() )
{
return QString::null;
}
i += 1; // jump the separator
return stringRe.cap( 1 )
.replace( "\\\"", "\"" )
.replace( "\\r", "\r" )
.replace( "\\b", "\b" )
.replace( "\\t", "\t" )
.replace( "\\/", "/" )
.replace( "\\n", "\n" )
.replace( "\\\\", "\\" );
}
else
{ //unquoted element
QString ret;
int sepPos = cur.indexOf( ',' );
if ( sepPos < 0 ) sepPos = cur.indexOf( ']' );
if ( sepPos < 0 )
{
i += cur.length();
return cur.trimmed();
}
i += sepPos + 1;
return cur.left( sepPos ).trimmed();
}
}
}
QVariantList QgsJSONUtils::parseArray( const QString& json, QVariant::Type type ) QVariantList QgsJSONUtils::parseArray( const QString& json, QVariant::Type type )
{ {
// TODO: switch to the Qt parser when we switch off Qt4 QJsonParseError error;
const QJsonDocument jsonDoc = QJsonDocument::fromJson( json.toUtf8(), &error );
QVariantList result; QVariantList result;
int i = 0; if ( error.error != QJsonParseError::NoError )
jumpSpace( json, i );
if ( json.at( i++ ) != '[' )
{ {
QgsLogger::warning( QString( "Cannot parse json (%1): %2" ).arg( error.errorString() ).arg( json ) );
return result; return result;
} }
while ( i < json.length() ) if ( !jsonDoc.isArray() )
{ {
jumpSpace( json, i ); QgsLogger::warning( QString( "Cannot parse json (%1) as array: %2" ).arg( error.errorString() ).arg( json ) );
if ( json.at( i ) == ']' ) return result;
{ }
return result; Q_FOREACH ( const QJsonValue cur, jsonDoc.array() )
} {
const QString value = getNextString( json, i ); QVariant curVariant = cur.toVariant();
if ( value.isNull() ) if ( curVariant.convert( type ) )
{ result.append( curVariant );
break; else
} QgsLogger::warning( QString( "Cannot convert json array element: %1" ).arg( cur.toString() ) );
QVariant variant( value );
if ( variant.convert( type ) ) result.append( variant );
} }
return result; return result;
} }

View File

@ -54,7 +54,7 @@ class TestQgsJSONUtils : public QObject
} }
{ // check invalid entries are ignored { // check invalid entries are ignored
const QVariantList back = QgsJSONUtils::parseArray( "[1,a,-2]", QVariant::Int ); const QVariantList back = QgsJSONUtils::parseArray( "[1,\"a\",-2]", QVariant::Int );
QCOMPARE( back, list ); QCOMPARE( back, list );
} }
} }