Skip to content

Commit e1386d4

Browse files
authoredSep 23, 2016
Merge pull request #3518 from pvalsecc/qt5_json
Use the Qt5 JSON classes
2 parents a21946a + 1a71ee5 commit e1386d4

File tree

2 files changed

+21
-100
lines changed

2 files changed

+21
-100
lines changed
 

‎src/core/qgsjsonutils.cpp

Lines changed: 20 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
#include "qgsrelationmanager.h"
2424
#include "qgsproject.h"
2525
#include "qgscsexception.h"
26+
#include "qgslogger.h"
27+
28+
#include <QJsonDocument>
29+
#include <QJsonArray>
2630

2731
QgsJSONExporter::QgsJSONExporter( const QgsVectorLayer* vectorLayer, int precision )
2832
: mPrecision( precision )
@@ -246,38 +250,9 @@ QString QgsJSONUtils::encodeValue( const QVariant &value )
246250
return value.toBool() ? "true" : "false";
247251

248252
case QVariant::StringList:
249-
{
250-
QStringList input = value.toStringList();
251-
QStringList output;
252-
Q_FOREACH ( const QString& string, input )
253-
{
254-
output << encodeValue( string );
255-
}
256-
return output.join( "," ).prepend( '[' ).append( ']' );
257-
}
258-
259253
case QVariant::List:
260-
{
261-
QVariantList input = value.toList();
262-
QStringList output;
263-
Q_FOREACH ( const QVariant& v, input )
264-
{
265-
output << encodeValue( v );
266-
}
267-
return output.join( "," ).prepend( '[' ).append( ']' );
268-
}
269-
270254
case QVariant::Map:
271-
{
272-
QMap< QString, QVariant > input = value.toMap();
273-
QStringList output;
274-
QMap< QString, QVariant >::const_iterator it = input.constBegin();
275-
for ( ; it != input.constEnd(); ++it )
276-
{
277-
output << encodeValue( it.key() ) + ':' + encodeValue( it.value() );
278-
}
279-
return output.join( ",\n" ).prepend( '{' ).append( '}' );
280-
}
255+
return QString::fromUtf8( QJsonDocument::fromVariant( value ).toJson( QJsonDocument::Compact ) );
281256

282257
default:
283258
case QVariant::String:
@@ -309,82 +284,28 @@ QString QgsJSONUtils::exportAttributes( const QgsFeature& feature )
309284
return attrs.prepend( '{' ).append( '}' );
310285
}
311286

312-
313-
namespace //TODO: remove when we switch off Qt4
314-
{
315-
void jumpSpace( const QString& txt, int& i )
316-
{
317-
while ( i < txt.length() && txt.at( i ).isSpace() ) ++i;
318-
}
319-
320-
static QString getNextString( const QString& txt, int& i )
321-
{
322-
jumpSpace( txt, i );
323-
QString cur = txt.mid( i );
324-
if ( cur.startsWith( '"' ) )
325-
{ //quoted element
326-
QRegExp stringRe( "^\"((?:\\\\.|[^\"\\\\])*)\".*" );
327-
if ( !stringRe.exactMatch( cur ) )
328-
{
329-
return QString::null;
330-
}
331-
i += stringRe.cap( 1 ).length() + 2;
332-
jumpSpace( txt, i );
333-
if ( !txt.mid( i ).startsWith( ',' ) && !txt.mid( i ).startsWith( ']' ) && i < txt.length() )
334-
{
335-
return QString::null;
336-
}
337-
i += 1; // jump the separator
338-
339-
return stringRe.cap( 1 )
340-
.replace( "\\\"", "\"" )
341-
.replace( "\\r", "\r" )
342-
.replace( "\\b", "\b" )
343-
.replace( "\\t", "\t" )
344-
.replace( "\\/", "/" )
345-
.replace( "\\n", "\n" )
346-
.replace( "\\\\", "\\" );
347-
}
348-
else
349-
{ //unquoted element
350-
QString ret;
351-
int sepPos = cur.indexOf( ',' );
352-
if ( sepPos < 0 ) sepPos = cur.indexOf( ']' );
353-
if ( sepPos < 0 )
354-
{
355-
i += cur.length();
356-
return cur.trimmed();
357-
}
358-
i += sepPos + 1;
359-
return cur.left( sepPos ).trimmed();
360-
}
361-
}
362-
}
363-
364287
QVariantList QgsJSONUtils::parseArray( const QString& json, QVariant::Type type )
365288
{
366-
// TODO: switch to the Qt parser when we switch off Qt4
289+
QJsonParseError error;
290+
const QJsonDocument jsonDoc = QJsonDocument::fromJson( json.toUtf8(), &error );
367291
QVariantList result;
368-
int i = 0;
369-
jumpSpace( json, i );
370-
if ( json.at( i++ ) != '[' )
292+
if ( error.error != QJsonParseError::NoError )
371293
{
294+
QgsLogger::warning( QString( "Cannot parse json (%1): %2" ).arg( error.errorString() ).arg( json ) );
372295
return result;
373296
}
374-
while ( i < json.length() )
297+
if ( !jsonDoc.isArray() )
375298
{
376-
jumpSpace( json, i );
377-
if ( json.at( i ) == ']' )
378-
{
379-
return result;
380-
}
381-
const QString value = getNextString( json, i );
382-
if ( value.isNull() )
383-
{
384-
break;
385-
}
386-
QVariant variant( value );
387-
if ( variant.convert( type ) ) result.append( variant );
299+
QgsLogger::warning( QString( "Cannot parse json (%1) as array: %2" ).arg( error.errorString() ).arg( json ) );
300+
return result;
301+
}
302+
Q_FOREACH ( const QJsonValue cur, jsonDoc.array() )
303+
{
304+
QVariant curVariant = cur.toVariant();
305+
if ( curVariant.convert( type ) )
306+
result.append( curVariant );
307+
else
308+
QgsLogger::warning( QString( "Cannot convert json array element: %1" ).arg( cur.toString() ) );
388309
}
389310
return result;
390311
}

‎tests/src/core/testqgsjsonutils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class TestQgsJSONUtils : public QObject
5454
}
5555

5656
{ // check invalid entries are ignored
57-
const QVariantList back = QgsJSONUtils::parseArray( "[1,a,-2]", QVariant::Int );
57+
const QVariantList back = QgsJSONUtils::parseArray( "[1,\"a\",-2]", QVariant::Int );
5858
QCOMPARE( back, list );
5959
}
6060
}

0 commit comments

Comments
 (0)
Please sign in to comment.