2013-05-15 16:00:36 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// Copyright (C) 2011-2013 Statoil ASA, Ceetron AS
|
2019-09-06 10:40:57 +02:00
|
|
|
//
|
2013-05-15 16:00:36 +02:00
|
|
|
// ResInsight is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
2019-09-06 10:40:57 +02:00
|
|
|
//
|
2013-05-15 16:00:36 +02:00
|
|
|
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE.
|
2019-09-06 10:40:57 +02:00
|
|
|
//
|
|
|
|
|
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
|
2013-05-15 16:00:36 +02:00
|
|
|
// for more details.
|
|
|
|
|
//
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
#include "RifJsonEncodeDecode.h"
|
|
|
|
|
|
|
|
|
|
#include <QtCore/QFile>
|
2022-10-18 10:07:16 +02:00
|
|
|
#include <QtCore/QJsonDocument>
|
|
|
|
|
#include <QtCore/QJsonObject>
|
2013-05-15 16:00:36 +02:00
|
|
|
#include <QtCore/QString>
|
|
|
|
|
|
2019-09-06 10:40:57 +02:00
|
|
|
namespace ResInsightInternalJson
|
|
|
|
|
{
|
|
|
|
|
QMap<QString, QVariant> JsonReader::decodeFile( QString filePath )
|
2013-05-15 16:00:36 +02:00
|
|
|
{
|
|
|
|
|
QFile file;
|
2019-09-06 10:40:57 +02:00
|
|
|
file.setFileName( filePath );
|
|
|
|
|
file.open( QIODevice::ReadOnly );
|
2013-05-15 16:00:36 +02:00
|
|
|
QByteArray byteArray = file.readAll();
|
|
|
|
|
file.close();
|
2022-10-18 10:07:16 +02:00
|
|
|
return Json::decode( byteArray );
|
2013-05-15 16:00:36 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-06 10:40:57 +02:00
|
|
|
QString Json::encode( const QMap<QString, QVariant>& map, bool prettify )
|
2013-05-15 16:00:36 +02:00
|
|
|
{
|
2022-10-18 10:07:16 +02:00
|
|
|
QJsonDocument doc( QJsonObject::fromVariantMap( map ) );
|
2013-05-15 16:00:36 +02:00
|
|
|
|
2022-10-18 10:07:16 +02:00
|
|
|
QJsonDocument::JsonFormat format = prettify ? QJsonDocument::JsonFormat::Indented : QJsonDocument::JsonFormat::Compact;
|
|
|
|
|
return doc.toJson( format );
|
2013-05-15 16:00:36 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-06 10:40:57 +02:00
|
|
|
QMap<QString, QVariant> Json::decode( const QString& jsonStr )
|
2013-05-15 16:00:36 +02:00
|
|
|
{
|
2022-10-18 10:07:16 +02:00
|
|
|
return Json::decode( jsonStr.toUtf8() );
|
2013-05-15 16:00:36 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-18 10:07:16 +02:00
|
|
|
QMap<QString, QVariant> Json::decode( const QByteArray& byteArray )
|
2013-05-15 16:00:36 +02:00
|
|
|
{
|
2022-10-18 10:07:16 +02:00
|
|
|
return QJsonDocument::fromJson( byteArray ).object().toVariantMap();
|
2013-05-21 11:18:28 +02:00
|
|
|
}
|
2016-06-06 10:43:36 +02:00
|
|
|
|
2022-10-18 10:07:16 +02:00
|
|
|
} // namespace ResInsightInternalJson
|