diff --git a/libraries/entities/src/EntityTree.cpp b/libraries/entities/src/EntityTree.cpp index aa2167029d..19182d8b22 100644 --- a/libraries/entities/src/EntityTree.cpp +++ b/libraries/entities/src/EntityTree.cpp @@ -2659,7 +2659,7 @@ bool EntityTree::readFromMap(QVariantMap& map) { return success; } -bool EntityTree::writeToJSON(QString & jsonString, const OctreeElementPointer & element) { +bool EntityTree::writeToJSON(QString& jsonString, const OctreeElementPointer& element) { QScriptEngine scriptEngine; RecurseOctreeToJSONOperator theOperator(element, &scriptEngine, jsonString); withReadLock([&] { diff --git a/libraries/entities/src/RecurseOctreeToJSONOperator.cpp b/libraries/entities/src/RecurseOctreeToJSONOperator.cpp index 2db1a2b228..d67e7176a3 100644 --- a/libraries/entities/src/RecurseOctreeToJSONOperator.cpp +++ b/libraries/entities/src/RecurseOctreeToJSONOperator.cpp @@ -10,14 +10,14 @@ // #include "RecurseOctreeToJSONOperator.h" - #include "EntityItemProperties.h" RecurseOctreeToJSONOperator::RecurseOctreeToJSONOperator(const OctreeElementPointer& top, QScriptEngine* engine, - QString jsonPrefix /* = QString() */) + QString jsonPrefix /* = QString() */, bool skipDefaults /* = true */) : _top(top) , _engine(engine) , _json(jsonPrefix) + , _skipDefaults(skipDefaults) { _toStringMethod = _engine->evaluate("(function() { return JSON.stringify(this, null, ' ') })"); } @@ -30,7 +30,10 @@ bool RecurseOctreeToJSONOperator::postRecursion(const OctreeElementPointer& elem } void RecurseOctreeToJSONOperator::processEntity(const EntityItemPointer& entity) { - QScriptValue qScriptValues = EntityItemNonDefaultPropertiesToScriptValue(_engine, entity->getProperties()); + QScriptValue qScriptValues = _skipDefaults + ? EntityItemNonDefaultPropertiesToScriptValue(_engine, entity->getProperties()) + : EntityItemPropertiesToScriptValue(_engine, entity->getProperties()); + if (comma) { _json += ','; }; @@ -39,7 +42,6 @@ void RecurseOctreeToJSONOperator::processEntity(const EntityItemPointer& entity) // Override default toString(): qScriptValues.setProperty("toString", _toStringMethod); - QString jsonResult = qScriptValues.toString(); + _json += qScriptValues.toString(); //auto exceptionString2 = _engine->uncaughtException().toString(); - _json += jsonResult; } diff --git a/libraries/entities/src/RecurseOctreeToJSONOperator.h b/libraries/entities/src/RecurseOctreeToJSONOperator.h index 92ce6bfdca..f661e08d5b 100644 --- a/libraries/entities/src/RecurseOctreeToJSONOperator.h +++ b/libraries/entities/src/RecurseOctreeToJSONOperator.h @@ -13,7 +13,7 @@ class RecurseOctreeToJSONOperator : public RecurseOctreeOperator { public: - RecurseOctreeToJSONOperator(const OctreeElementPointer& top, QScriptEngine* engine, QString jsonPrefix = QString()); + RecurseOctreeToJSONOperator(const OctreeElementPointer& top, QScriptEngine* engine, QString jsonPrefix = QString(), bool skipDefaults = true); virtual bool preRecursion(const OctreeElementPointer& element) override { return true; }; virtual bool postRecursion(const OctreeElementPointer& element) override; @@ -27,5 +27,6 @@ private: QScriptValue _toStringMethod; QString _json; + const bool _skipDefaults; bool comma { false }; }; diff --git a/libraries/octree/src/Octree.cpp b/libraries/octree/src/Octree.cpp index 40d2449a38..d583901b41 100644 --- a/libraries/octree/src/Octree.cpp +++ b/libraries/octree/src/Octree.cpp @@ -956,8 +956,8 @@ bool Octree::toJSON(QByteArray* data, const OctreeElementPointer& element, bool } else { *data = doc.toJson(); } -#endif // HIFI_USE_DIRECT_TO_JSON +#endif // HIFI_USE_DIRECT_TO_JSON return true; } diff --git a/libraries/octree/src/OctreeDataUtils.cpp b/libraries/octree/src/OctreeDataUtils.cpp index 44a56fe97a..4b1700d593 100644 --- a/libraries/octree/src/OctreeDataUtils.cpp +++ b/libraries/octree/src/OctreeDataUtils.cpp @@ -35,7 +35,11 @@ bool readOctreeFile(QString path, QJsonDocument* doc) { jsonData = data; } - *doc = QJsonDocument::fromJson(jsonData); + QJsonParseError parserError; + *doc = QJsonDocument::fromJson(jsonData, &parserError); + if (parserError.error != QJsonParseError::NoError) { + qWarning() << "Error reading JSON file" << path << "-" << parserError.errorString(); + } return !doc->isNull(); }