Add skipThoseWithBadParents functionality to octree visitor; reviewer suggestions

This commit is contained in:
Simon Walton 2018-11-08 18:20:20 -08:00
parent 6f6c92e647
commit d183968175
7 changed files with 18 additions and 42 deletions

View file

@ -13,10 +13,11 @@
#include "EntityItemProperties.h" #include "EntityItemProperties.h"
RecurseOctreeToJSONOperator::RecurseOctreeToJSONOperator(const OctreeElementPointer&, QScriptEngine* engine, RecurseOctreeToJSONOperator::RecurseOctreeToJSONOperator(const OctreeElementPointer&, QScriptEngine* engine,
QString jsonPrefix /* = QString() */, bool skipDefaults /* = true */) QString jsonPrefix, bool skipDefaults, bool skipThoseWithBadParents):
: _engine(engine) _engine(engine),
, _json(jsonPrefix) _json(jsonPrefix),
, _skipDefaults(skipDefaults) _skipDefaults(skipDefaults),
_skipThoseWithBadParents(skipThoseWithBadParents)
{ {
_toStringMethod = _engine->evaluate("(function() { return JSON.stringify(this, null, ' ') })"); _toStringMethod = _engine->evaluate("(function() { return JSON.stringify(this, null, ' ') })");
} }
@ -29,18 +30,21 @@ bool RecurseOctreeToJSONOperator::postRecursion(const OctreeElementPointer& elem
} }
void RecurseOctreeToJSONOperator::processEntity(const EntityItemPointer& entity) { void RecurseOctreeToJSONOperator::processEntity(const EntityItemPointer& entity) {
if (_skipThoseWithBadParents && !entity->isParentIDValid()) {
return; // we weren't able to resolve a parent from _parentID, so don't save this entity.
}
QScriptValue qScriptValues = _skipDefaults QScriptValue qScriptValues = _skipDefaults
? EntityItemNonDefaultPropertiesToScriptValue(_engine, entity->getProperties()) ? EntityItemNonDefaultPropertiesToScriptValue(_engine, entity->getProperties())
: EntityItemPropertiesToScriptValue(_engine, entity->getProperties()); : EntityItemPropertiesToScriptValue(_engine, entity->getProperties());
if (comma) { if (_comma) {
_json += ','; _json += ',';
}; };
comma = true; _comma = true;
_json += "\n "; _json += "\n ";
// Override default toString(): // Override default toString():
qScriptValues.setProperty("toString", _toStringMethod); qScriptValues.setProperty("toString", _toStringMethod);
_json += qScriptValues.toString(); _json += qScriptValues.toString();
//auto exceptionString2 = _engine->uncaughtException().toString();
} }

View file

@ -13,7 +13,8 @@
class RecurseOctreeToJSONOperator : public RecurseOctreeOperator { class RecurseOctreeToJSONOperator : public RecurseOctreeOperator {
public: public:
RecurseOctreeToJSONOperator(const OctreeElementPointer&, QScriptEngine* engine, QString jsonPrefix = QString(), bool skipDefaults = true); RecurseOctreeToJSONOperator(const OctreeElementPointer&, QScriptEngine* engine, QString jsonPrefix = QString(), bool skipDefaults = true,
bool skipThoseWithBadParents = false);
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 +28,6 @@ private:
QString _json; QString _json;
const bool _skipDefaults; const bool _skipDefaults;
bool comma { false }; bool _skipThoseWithBadParents;
bool _comma { false };
}; };

View file

@ -925,9 +925,6 @@ bool Octree::toJSONString(QString& jsonString, const OctreeElementPointer& eleme
} }
bool Octree::toJSON(QByteArray* data, const OctreeElementPointer& element, bool doGzip) { bool Octree::toJSON(QByteArray* data, const OctreeElementPointer& element, bool doGzip) {
#define HIFI_USE_DIRECT_TO_JSON
#ifdef HIFI_USE_DIRECT_TO_JSON
QString jsonString; QString jsonString;
toJSONString(jsonString); toJSONString(jsonString);
@ -940,29 +937,6 @@ bool Octree::toJSON(QByteArray* data, const OctreeElementPointer& element, bool
*data = jsonString.toUtf8(); *data = jsonString.toUtf8();
} }
#else
QJsonDocument doc;
if (!toJSONDocument(&doc, element)) {
qCritical("Failed to convert Entities to JSON document.");
return false;
}
QString jsonString;
toJSONString(jsonString);
if (doGzip) {
QByteArray jsonData = doc.toJson();
if (!gzip(jsonData, *data, -1)) {
qCritical("Unable to gzip data while saving to json.");
return false;
}
} else {
*data = doc.toJson();
}
#endif // HIFI_USE_DIRECT_TO_JSON
return true; return true;
} }

View file

@ -56,7 +56,7 @@ bool OctreeUtils::RawOctreeData::readOctreeDataInfoFromJSON(QJsonObject root) {
return true; return true;
} }
bool OctreeUtils::RawOctreeData::readOctreeDataInfoFromMap(QVariantMap map) { bool OctreeUtils::RawOctreeData::readOctreeDataInfoFromMap(const QVariantMap& map) {
if (map.contains("Id") && map.contains("DataVersion") && map.contains("Version")) { if (map.contains("Id") && map.contains("DataVersion") && map.contains("Version")) {
id = map["Id"].toUuid(); id = map["Id"].toUuid();
dataVersion = map["DataVersion"].toInt(); dataVersion = map["DataVersion"].toInt();

View file

@ -43,7 +43,7 @@ public:
bool readOctreeDataInfoFromData(QByteArray data); bool readOctreeDataInfoFromData(QByteArray data);
bool readOctreeDataInfoFromFile(QString path); bool readOctreeDataInfoFromFile(QString path);
bool readOctreeDataInfoFromJSON(QJsonObject root); bool readOctreeDataInfoFromJSON(QJsonObject root);
bool readOctreeDataInfoFromMap(QVariantMap map); bool readOctreeDataInfoFromMap(const QVariantMap& map);
}; };
class RawEntityData : public RawOctreeData { class RawEntityData : public RawOctreeData {

View file

@ -19,9 +19,6 @@
using std::string; using std::string;
OctreeEntitiesFileParser::OctreeEntitiesFileParser() {
}
std::string OctreeEntitiesFileParser::getErrorString() const { std::string OctreeEntitiesFileParser::getErrorString() const {
std::ostringstream err; std::ostringstream err;
if (_errorString.size() != 0) { if (_errorString.size() != 0) {
@ -86,7 +83,7 @@ bool OctreeEntitiesFileParser::parseEntities(QVariantMap& parsedEntities) {
return false; return false;
} }
parsedEntities["Entities"] = entitiesValue; parsedEntities["Entities"] = std::move(entitiesValue);
gotEntities = true; gotEntities = true;
} else if (key == "Id") { } else if (key == "Id") {
if (gotId) { if (gotId) {

View file

@ -19,7 +19,6 @@
class OctreeEntitiesFileParser { class OctreeEntitiesFileParser {
public: public:
OctreeEntitiesFileParser();
void setEntitiesString(const QByteArray& entitiesContents); void setEntitiesString(const QByteArray& entitiesContents);
bool parseEntities(QVariantMap& parsedEntities); bool parseEntities(QVariantMap& parsedEntities);
std::string getErrorString() const; std::string getErrorString() const;