mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
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 <magne.sjaastad@ceetronsolutions.com>
This commit is contained in:
committed by
GitHub
parent
760bfaeec2
commit
806c9af82b
@@ -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
|
||||
|
@@ -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
|
||||
|
@@ -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 <QtCore/QFile>
|
||||
#include <QtCore/QJsonDocument>
|
||||
#include <QtCore/QJsonObject>
|
||||
#include <QtCore/QString>
|
||||
#include <QtScript/QScriptEngine>
|
||||
#include <QtScript/QScriptValueIterator>
|
||||
|
||||
namespace ResInsightInternalJson
|
||||
{
|
||||
@@ -35,117 +32,25 @@ QMap<QString, QVariant> 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<cvf::Vec3d>& 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<QString, QVariant>& 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<QString, QVariant> 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<QString, QVariant>& map, QScriptEngine* engine )
|
||||
QMap<QString, QVariant> Json::decode( const QByteArray& byteArray )
|
||||
{
|
||||
QScriptValue obj = engine->newObject();
|
||||
QMapIterator<QString, QVariant> 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<QString, QVariant> Json::decodeInner( QScriptValue object )
|
||||
{
|
||||
QMap<QString, QVariant> 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<QVariant> Json::decodeInnerToList( QScriptValue arrayValue )
|
||||
{
|
||||
QList<QVariant> 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
|
||||
} // namespace ResInsightInternalJson
|
||||
|
@@ -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 <QtCore/QList>
|
||||
#include <QtCore/QMap>
|
||||
#include <QtCore/QString>
|
||||
#include <QtCore/QVariant>
|
||||
#include <QtScript/QScriptValue>
|
||||
|
||||
#if IMPL_DUMP_TO_FILE
|
||||
#include <cvfVector3.h>
|
||||
#include <vector>
|
||||
#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<QString, QVariant> decodeFile( QString filePath );
|
||||
|
||||
#if IMPL_DUMP_TO_FILE
|
||||
void dumpToFile( std::vector<cvf::Vec3d>& points, QString filePath );
|
||||
#endif
|
||||
};
|
||||
|
||||
class Json
|
||||
{
|
||||
public:
|
||||
Json(){};
|
||||
QString encode( const QMap<QString, QVariant>& map, bool prettify );
|
||||
QMap<QString, QVariant> decode( const QString& jsonStr );
|
||||
|
||||
private:
|
||||
QScriptValue encodeInner( const QMap<QString, QVariant>& map, QScriptEngine* engine );
|
||||
QMap<QString, QVariant> decodeInner( QScriptValue object );
|
||||
QList<QVariant> decodeInnerToList( QScriptValue arrayValue );
|
||||
static QString encode( const QMap<QString, QVariant>& map, bool prettify );
|
||||
static QMap<QString, QVariant> decode( const QString& jsonStr );
|
||||
static QMap<QString, QVariant> decode( const QByteArray& byteArray );
|
||||
};
|
||||
|
||||
} // namespace ResInsightInternalJson
|
||||
} // namespace ResInsightInternalJson
|
||||
|
@@ -22,14 +22,13 @@
|
||||
|
||||
#include "RifJsonEncodeDecode.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include "cafUtils.h"
|
||||
|
||||
#include <QFileInfo>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <fstream>
|
||||
|
||||
#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 );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user