From 806c9af82beb755c50d7c393e967a570f5046536 Mon Sep 17 00:00:00 2001 From: Kristian Bendiksen Date: Tue, 18 Oct 2022 10:07:16 +0200 Subject: [PATCH] 9172 Remove dependency on QtScript * #9172 Update json encoding/decoding to avoid QtScript * Janitor: remove unused debug code * Janitor: minor cleanup * Remove dependencies to Qt Script Co-authored-by: Magne Sjaastad --- ApplicationExeCode/CMakeLists.txt | 2 - ApplicationLibCode/CMakeLists.txt | 2 - .../FileInterface/RifJsonEncodeDecode.cpp | 115 ++---------------- .../FileInterface/RifJsonEncodeDecode.h | 31 +---- .../FileInterface/RifWellPathImporter.cpp | 14 +-- CMakeLists.txt | 9 +- 6 files changed, 17 insertions(+), 156 deletions(-) diff --git a/ApplicationExeCode/CMakeLists.txt b/ApplicationExeCode/CMakeLists.txt index 6212817d17..7fb931713a 100644 --- a/ApplicationExeCode/CMakeLists.txt +++ b/ApplicationExeCode/CMakeLists.txt @@ -30,7 +30,6 @@ if(Qt5Core_FOUND) Gui OpenGL Network - Script Widgets Xml Concurrent @@ -43,7 +42,6 @@ if(Qt5Core_FOUND) Qt5::Gui Qt5::Network Qt5::OpenGL - Qt5::Script Qt5::Widgets Qt5::Xml Qt5::Concurrent diff --git a/ApplicationLibCode/CMakeLists.txt b/ApplicationLibCode/CMakeLists.txt index 7623a3eb23..ec77d7e61d 100644 --- a/ApplicationLibCode/CMakeLists.txt +++ b/ApplicationLibCode/CMakeLists.txt @@ -34,7 +34,6 @@ if(Qt5Core_FOUND) Gui OpenGL Network - Script Widgets Xml Concurrent @@ -47,7 +46,6 @@ if(Qt5Core_FOUND) Qt5::Gui Qt5::Network Qt5::OpenGL - Qt5::Script Qt5::Widgets Qt5::Xml Qt5::Concurrent diff --git a/ApplicationLibCode/FileInterface/RifJsonEncodeDecode.cpp b/ApplicationLibCode/FileInterface/RifJsonEncodeDecode.cpp index 5f1fe11b00..fc86fadc0e 100644 --- a/ApplicationLibCode/FileInterface/RifJsonEncodeDecode.cpp +++ b/ApplicationLibCode/FileInterface/RifJsonEncodeDecode.cpp @@ -16,15 +16,12 @@ // ///////////////////////////////////////////////////////////////////////////////// -// Json parser based on code example found on: -// http://stackoverflow.com/questions/4169988/easiest-way-to-parse-json-in-qt-4-7 - #include "RifJsonEncodeDecode.h" #include +#include +#include #include -#include -#include namespace ResInsightInternalJson { @@ -35,117 +32,25 @@ QMap JsonReader::decodeFile( QString filePath ) file.open( QIODevice::ReadOnly ); QByteArray byteArray = file.readAll(); file.close(); - QString jsonString( byteArray ); - Json json; - return json.decode( jsonString ); + return Json::decode( byteArray ); } -#if IMPL_DUMP_TO_FILE -void JsonReader::dumpToFile( std::vector& points, QString filePath ) -{ - QFile file; - file.setFileName( filePath ); - file.open( QIODevice::WriteOnly ); - for ( size_t idx = 0; idx < points.size(); idx++ ) - { - cvf::Vec3d point = points[idx]; - QString string; - string.sprintf( "(%0.10e, %0.10e, %0.10e)\n", point.x(), point.y(), point.z() ); - QByteArray byteArray( string.toLatin1() ); - file.write( byteArray ); - } - file.close(); -} -#endif - QString Json::encode( const QMap& map, bool prettify ) { - QScriptEngine engine; - if ( prettify ) - { - engine.evaluate( "function toString() { return JSON.stringify(this, null, ' ') }" ); - } - else - { - engine.evaluate( "function toString() { return JSON.stringify(this) }" ); - } + QJsonDocument doc( QJsonObject::fromVariantMap( map ) ); - QScriptValue toString = engine.globalObject().property( "toString" ); - QScriptValue obj = encodeInner( map, &engine ); - return toString.call( obj ).toString(); + QJsonDocument::JsonFormat format = prettify ? QJsonDocument::JsonFormat::Indented : QJsonDocument::JsonFormat::Compact; + return doc.toJson( format ); } QMap Json::decode( const QString& jsonStr ) { - QScriptValue object; - QScriptEngine engine; - object = engine.evaluate( "(" + jsonStr + ")" ); - return decodeInner( object ); + return Json::decode( jsonStr.toUtf8() ); } -QScriptValue Json::encodeInner( const QMap& map, QScriptEngine* engine ) +QMap Json::decode( const QByteArray& byteArray ) { - QScriptValue obj = engine->newObject(); - QMapIterator i( map ); - while ( i.hasNext() ) - { - i.next(); - if ( i.value().type() == QVariant::String ) - obj.setProperty( i.key(), i.value().toString() ); - else if ( i.value().type() == QVariant::Int ) - obj.setProperty( i.key(), i.value().toInt() ); - else if ( i.value().type() == QVariant::Double ) - obj.setProperty( i.key(), i.value().toDouble() ); - else if ( i.value().type() == QVariant::List ) - obj.setProperty( i.key(), qScriptValueFromSequence( engine, i.value().toList() ) ); - else if ( i.value().type() == QVariant::Map ) - obj.setProperty( i.key(), encodeInner( i.value().toMap(), engine ) ); - } - return obj; + return QJsonDocument::fromJson( byteArray ).object().toVariantMap(); } -QMap Json::decodeInner( QScriptValue object ) -{ - QMap map; - QScriptValueIterator it( object ); - while ( it.hasNext() ) - { - it.next(); - if ( it.value().isArray() ) - map.insert( it.name(), QVariant( decodeInnerToList( it.value() ) ) ); - else if ( it.value().isNumber() ) - map.insert( it.name(), QVariant( it.value().toNumber() ) ); - else if ( it.value().isString() ) - map.insert( it.name(), QVariant( it.value().toString() ) ); - else if ( it.value().isNull() ) - map.insert( it.name(), QVariant() ); - else if ( it.value().isObject() ) - map.insert( it.name(), QVariant( decodeInner( it.value() ) ) ); - } - return map; -} - -QList Json::decodeInnerToList( QScriptValue arrayValue ) -{ - QList list; - QScriptValueIterator it( arrayValue ); - while ( it.hasNext() ) - { - it.next(); - if ( it.name() == "length" ) continue; - - if ( it.value().isArray() ) - list.append( QVariant( decodeInnerToList( it.value() ) ) ); - else if ( it.value().isNumber() ) - list.append( QVariant( it.value().toNumber() ) ); - else if ( it.value().isString() ) - list.append( QVariant( it.value().toString() ) ); - else if ( it.value().isNull() ) - list.append( QVariant() ); - else if ( it.value().isObject() ) - list.append( QVariant( decodeInner( it.value() ) ) ); - } - return list; -} - -} // namespace ResInsightInternalJson \ No newline at end of file +} // namespace ResInsightInternalJson diff --git a/ApplicationLibCode/FileInterface/RifJsonEncodeDecode.h b/ApplicationLibCode/FileInterface/RifJsonEncodeDecode.h index a2f59d47d5..97beecd8db 100644 --- a/ApplicationLibCode/FileInterface/RifJsonEncodeDecode.h +++ b/ApplicationLibCode/FileInterface/RifJsonEncodeDecode.h @@ -18,23 +18,9 @@ #pragma once -// Json parser based on code example found on: -// http://stackoverflow.com/questions/4169988/easiest-way-to-parse-json-in-qt-4-7 - -//#define IMPL_DUMP_TO_FILE - -#include #include #include #include -#include - -#if IMPL_DUMP_TO_FILE -#include -#include -#endif - -class QScriptEngine; // Encapsulate the JSON code in a namespace to avoid issues with JSON classes used in opm-parser namespace ResInsightInternalJson @@ -43,23 +29,14 @@ class JsonReader { public: QMap decodeFile( QString filePath ); - -#if IMPL_DUMP_TO_FILE - void dumpToFile( std::vector& points, QString filePath ); -#endif }; class Json { public: - Json(){}; - QString encode( const QMap& map, bool prettify ); - QMap decode( const QString& jsonStr ); - -private: - QScriptValue encodeInner( const QMap& map, QScriptEngine* engine ); - QMap decodeInner( QScriptValue object ); - QList decodeInnerToList( QScriptValue arrayValue ); + static QString encode( const QMap& map, bool prettify ); + static QMap decode( const QString& jsonStr ); + static QMap decode( const QByteArray& byteArray ); }; -} // namespace ResInsightInternalJson \ No newline at end of file +} // namespace ResInsightInternalJson diff --git a/ApplicationLibCode/FileInterface/RifWellPathImporter.cpp b/ApplicationLibCode/FileInterface/RifWellPathImporter.cpp index 6dc70e30b5..ba6c124891 100644 --- a/ApplicationLibCode/FileInterface/RifWellPathImporter.cpp +++ b/ApplicationLibCode/FileInterface/RifWellPathImporter.cpp @@ -22,14 +22,13 @@ #include "RifJsonEncodeDecode.h" -#include - #include "cafUtils.h" #include #include #include +#include #define ASCII_FILE_DEFAULT_START_INDEX 0 @@ -109,7 +108,6 @@ size_t RifWellPathImporter::wellDataCount( const QString& filePath ) CVF_ASSERT( it != m_fileNameToWellDataGroupMap.end() ); return it->second.size(); - ; } } @@ -119,15 +117,7 @@ size_t RifWellPathImporter::wellDataCount( const QString& filePath ) bool RifWellPathImporter::isJsonFile( const QString& filePath ) { QFileInfo fileInfo( filePath ); - - if ( fileInfo.suffix().compare( "json" ) == 0 ) - { - return true; - } - else - { - return false; - } + return ( fileInfo.suffix().compare( "json" ) == 0 ); } //-------------------------------------------------------------------------------------------------- diff --git a/CMakeLists.txt b/CMakeLists.txt index 26154330f1..73b25e9448 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -401,14 +401,7 @@ list(APPEND THIRD_PARTY_LIBRARIES NRLib) set(RI_QT_MINIMUM_VERSION 5.12) find_package( - Qt5 ${RI_QT_MINIMUM_VERSION} - COMPONENTS Core - Gui - OpenGL - Network - Script - ScriptTools - Widgets + Qt5 ${RI_QT_MINIMUM_VERSION} COMPONENTS Core Gui OpenGL Network Widgets ) # Open GL