Include the skipDefaults option in case we ever need it

Also print errors from parsing entities file; other tweaks
This commit is contained in:
Simon Walton 2018-10-12 11:18:38 -07:00
parent 0eec0e376c
commit ad9f7f3a1d
5 changed files with 16 additions and 9 deletions

View file

@ -2659,7 +2659,7 @@ bool EntityTree::readFromMap(QVariantMap& map) {
return success; return success;
} }
bool EntityTree::writeToJSON(QString & jsonString, const OctreeElementPointer & element) { bool EntityTree::writeToJSON(QString& jsonString, const OctreeElementPointer& element) {
QScriptEngine scriptEngine; QScriptEngine scriptEngine;
RecurseOctreeToJSONOperator theOperator(element, &scriptEngine, jsonString); RecurseOctreeToJSONOperator theOperator(element, &scriptEngine, jsonString);
withReadLock([&] { withReadLock([&] {

View file

@ -10,14 +10,14 @@
// //
#include "RecurseOctreeToJSONOperator.h" #include "RecurseOctreeToJSONOperator.h"
#include "EntityItemProperties.h" #include "EntityItemProperties.h"
RecurseOctreeToJSONOperator::RecurseOctreeToJSONOperator(const OctreeElementPointer& top, QScriptEngine* engine, RecurseOctreeToJSONOperator::RecurseOctreeToJSONOperator(const OctreeElementPointer& top, QScriptEngine* engine,
QString jsonPrefix /* = QString() */) QString jsonPrefix /* = QString() */, bool skipDefaults /* = true */)
: _top(top) : _top(top)
, _engine(engine) , _engine(engine)
, _json(jsonPrefix) , _json(jsonPrefix)
, _skipDefaults(skipDefaults)
{ {
_toStringMethod = _engine->evaluate("(function() { return JSON.stringify(this, null, ' ') })"); _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) { 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) { if (comma) {
_json += ','; _json += ',';
}; };
@ -39,7 +42,6 @@ void RecurseOctreeToJSONOperator::processEntity(const EntityItemPointer& entity)
// Override default toString(): // Override default toString():
qScriptValues.setProperty("toString", _toStringMethod); qScriptValues.setProperty("toString", _toStringMethod);
QString jsonResult = qScriptValues.toString(); _json += qScriptValues.toString();
//auto exceptionString2 = _engine->uncaughtException().toString(); //auto exceptionString2 = _engine->uncaughtException().toString();
_json += jsonResult;
} }

View file

@ -13,7 +13,7 @@
class RecurseOctreeToJSONOperator : public RecurseOctreeOperator { class RecurseOctreeToJSONOperator : public RecurseOctreeOperator {
public: 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 preRecursion(const OctreeElementPointer& element) override { return true; };
virtual bool postRecursion(const OctreeElementPointer& element) override; virtual bool postRecursion(const OctreeElementPointer& element) override;
@ -27,5 +27,6 @@ private:
QScriptValue _toStringMethod; QScriptValue _toStringMethod;
QString _json; QString _json;
const bool _skipDefaults;
bool comma { false }; bool comma { false };
}; };

View file

@ -956,8 +956,8 @@ bool Octree::toJSON(QByteArray* data, const OctreeElementPointer& element, bool
} else { } else {
*data = doc.toJson(); *data = doc.toJson();
} }
#endif // HIFI_USE_DIRECT_TO_JSON
#endif // HIFI_USE_DIRECT_TO_JSON
return true; return true;
} }

View file

@ -35,7 +35,11 @@ bool readOctreeFile(QString path, QJsonDocument* doc) {
jsonData = data; 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(); return !doc->isNull();
} }