Commit c04b329c authored by Hans-Christian Ebke's avatar Hans-Christian Ebke
Browse files

Made MaterialNode's JSON (de)serialization Qt5 compatible.

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@19000 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 1d8bb755
...@@ -58,17 +58,28 @@ ...@@ -58,17 +58,28 @@
#include <cstdio> #include <cstdio>
#ifdef ENABLE_QJSON #include <Qt>
#if QT_VERSION >= 0x050000
#include <QJsonDocument>
#include <QJsonObject>
#elif defined(ENABLE_QJSON)
#include <QJson/Serializer> #include <QJson/Serializer>
#include <QJson/Parser> #include <QJson/Parser>
#endif #endif
#if defined(ENABLE_QJSON) || QT_VERSION >= 0x050000
#define JSON_SERIALIZABLE 1
#else
#define JSON_SERIALIZABLE 0
#endif
//== NAMESPACES =============================================================== //== NAMESPACES ===============================================================
namespace { namespace {
enum ClassProperties { enum ClassProperties {
#ifdef ENABLE_QJSON #if JSON_SERIALIZABLE
CP_JSON_SERIALIZABLE = 1 CP_JSON_SERIALIZABLE = 1
#else #else
CP_JSON_SERIALIZABLE = 0 CP_JSON_SERIALIZABLE = 0
...@@ -95,6 +106,12 @@ QVariantMap json_to_variant_map(QString json) { ...@@ -95,6 +106,12 @@ QVariantMap json_to_variant_map(QString json) {
QVariantMap matMap = parser.parse(json.toUtf8(), &ok).toMap(); QVariantMap matMap = parser.parse(json.toUtf8(), &ok).toMap();
if (!ok) return QVariantMap(); if (!ok) return QVariantMap();
return matMap; return matMap;
#elif QT_VERSION >= 0x050000
QJsonParseError error;
QJsonDocument jsonDoc = QJsonDocument::fromJson(json.toUtf8(), &error);
if (error.error != QJsonParseError::NoError || !jsonDoc.isObject())
return QVariantMap();
return jsonDoc.object().toVariantMap();
#else #else
return QVariantMap(); return QVariantMap();
#endif #endif
...@@ -110,7 +127,7 @@ bool Material::support_json_serialization() { ...@@ -110,7 +127,7 @@ bool Material::support_json_serialization() {
} }
QString Material::serializeToJson() const { QString Material::serializeToJson() const {
#ifdef ENABLE_QJSON #ifdef JSON_SERIALIZABLE
QVariantMap matMap; QVariantMap matMap;
matMap["baseColor"] = col2vl(baseColor_); matMap["baseColor"] = col2vl(baseColor_);
...@@ -133,9 +150,16 @@ QString Material::serializeToJson() const { ...@@ -133,9 +150,16 @@ QString Material::serializeToJson() const {
matMap["backfaceCulling"] = backfaceCulling_; matMap["backfaceCulling"] = backfaceCulling_;
matMap["multiSampling"] = multiSampling_; matMap["multiSampling"] = multiSampling_;
#ifdef ENABLE_QJSON
QJson::Serializer serializer; QJson::Serializer serializer;
QByteArray bytes = serializer.serialize(matMap); QByteArray bytes = serializer.serialize(matMap);
return QString::fromUtf8(bytes.constData(), bytes.size()); return QString::fromUtf8(bytes.constData(), bytes.size());
#elif QT_VERSION >= 0x050000
const QJsonDocument json_doc(QJsonObject::fromVariantMap(matMap));
return QString::fromUtf8(
json_doc.toJson(QJsonDocument::Indented));
#endif
#else #else
return QString("<No suitable serializer at the moment. Sorry.>"); return QString("<No suitable serializer at the moment. Sorry.>");
#endif #endif
...@@ -164,13 +188,7 @@ void Material::deserializeFromVariantMap(const QVariantMap &matMap) { ...@@ -164,13 +188,7 @@ void Material::deserializeFromVariantMap(const QVariantMap &matMap) {
} }
void Material::deserializeFromJson(const QString &json) { void Material::deserializeFromJson(const QString &json) {
#ifdef ENABLE_QJSON deserializeFromVariantMap(ACG::json_to_variant_map(json));
QJson::Parser parser;
bool ok;
QVariantMap matMap = parser.parse(json.toUtf8(), &ok).toMap();
if (!ok) return;
deserializeFromVariantMap(matMap);
#endif
} }
MaterialNode::MaterialNode( BaseNode* _parent, MaterialNode::MaterialNode( BaseNode* _parent,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment