From 948fdd024bbeff37e7b2da18b27239d608fa8d87 Mon Sep 17 00:00:00 2001 From: Ylva Selling Date: Tue, 25 Apr 2023 16:32:04 -0400 Subject: [PATCH] Remove generateJson function from the DocumentationGenerator and move sortJson function to json_helper --- .../documentation/documentationengine.h | 2 +- .../documentation/documentationgenerator.h | 1 - .../openspace/interaction/keybindingmanager.h | 2 +- include/openspace/properties/propertyowner.h | 2 +- include/openspace/scene/scenelicensewriter.h | 2 +- include/openspace/scripting/scriptengine.h | 2 +- include/openspace/util/factorymanager.h | 2 +- include/openspace/util/json_helper.h | 11 ++++++++ src/documentation/documentationgenerator.cpp | 23 ----------------- src/interaction/keybindingmanager.cpp | 2 +- src/properties/propertyowner.cpp | 6 ++--- src/scene/scenelicensewriter.cpp | 2 +- src/scripting/scriptengine.cpp | 4 +-- src/util/factorymanager.cpp | 4 +-- src/util/json_helper.cpp | 25 +++++++++++++++++++ 15 files changed, 51 insertions(+), 39 deletions(-) diff --git a/include/openspace/documentation/documentationengine.h b/include/openspace/documentation/documentationengine.h index 8336b39c21..1be6497f01 100644 --- a/include/openspace/documentation/documentationengine.h +++ b/include/openspace/documentation/documentationengine.h @@ -89,7 +89,7 @@ public: */ static DocumentationEngine& ref(); - std::string generateJson() const override; + std::string generateJson() const; nlohmann::json generateJsonJson() const; diff --git a/include/openspace/documentation/documentationgenerator.h b/include/openspace/documentation/documentationgenerator.h index dfe5e56b54..3d6e2c7b66 100644 --- a/include/openspace/documentation/documentationgenerator.h +++ b/include/openspace/documentation/documentationgenerator.h @@ -79,7 +79,6 @@ public: */ virtual std::string generateJson() const = 0; - void sortJson(nlohmann::json& json) const; private: const std::string _name; const std::string _jsonName; diff --git a/include/openspace/interaction/keybindingmanager.h b/include/openspace/interaction/keybindingmanager.h index 405c77f88b..f036a9b3f6 100644 --- a/include/openspace/interaction/keybindingmanager.h +++ b/include/openspace/interaction/keybindingmanager.h @@ -55,7 +55,7 @@ public: void keyboardCallback(Key key, KeyModifier modifier, KeyAction action); - std::string generateJson() const override; + std::string generateJson() const; nlohmann::json generateJsonJson() const; const std::multimap& keyBindings() const; diff --git a/include/openspace/properties/propertyowner.h b/include/openspace/properties/propertyowner.h index 3628e69db2..6c4599613b 100644 --- a/include/openspace/properties/propertyowner.h +++ b/include/openspace/properties/propertyowner.h @@ -297,7 +297,7 @@ public: void removeTag(const std::string& tag); // Generate JSON for documentation - std::string generateJson() const override; + std::string generateJson() const; nlohmann::json generateJsonJson() const; diff --git a/include/openspace/scene/scenelicensewriter.h b/include/openspace/scene/scenelicensewriter.h index a93a5aa9ab..9ed5af60c5 100644 --- a/include/openspace/scene/scenelicensewriter.h +++ b/include/openspace/scene/scenelicensewriter.h @@ -35,7 +35,7 @@ namespace openspace { class SceneLicenseWriter : public DocumentationGenerator { public: SceneLicenseWriter(); - std::string generateJson() const override; + std::string generateJson() const; nlohmann::json generateJsonJson() const; }; diff --git a/include/openspace/scripting/scriptengine.h b/include/openspace/scripting/scriptengine.h index 7239dd45e9..5d7602fbe8 100644 --- a/include/openspace/scripting/scriptengine.h +++ b/include/openspace/scripting/scriptengine.h @@ -95,7 +95,7 @@ public: std::vector allLuaFunctions() const; - std::string generateJson() const override; + std::string generateJson() const; nlohmann::json generateJsonJson() const; private: diff --git a/include/openspace/util/factorymanager.h b/include/openspace/util/factorymanager.h index 932d3d3d6f..07a388f68a 100644 --- a/include/openspace/util/factorymanager.h +++ b/include/openspace/util/factorymanager.h @@ -109,7 +109,7 @@ public: template ghoul::TemplateFactory* factory() const; - std::string generateJson() const override; + std::string generateJson() const; nlohmann::json generateJsonJson() const; private: diff --git a/include/openspace/util/json_helper.h b/include/openspace/util/json_helper.h index 9cb285a817..b7909d1b9b 100644 --- a/include/openspace/util/json_helper.h +++ b/include/openspace/util/json_helper.h @@ -26,6 +26,7 @@ #define __OPENSPACE_CORE___JSON_HELPER___H__ #include +#include namespace openspace { @@ -65,6 +66,16 @@ std::string formatJsonNumber(double d); template std::string formatJson(T value); +/** + * Sort a json object that is an array of objects with the structure + * [ key = {}, key = {} ...]. Sorts it by the provided key + * + * \param json The json to sort + * \param key The key the json should be sorted by + * \return The sorted JSON + */ +void sortJson(nlohmann::json& json, const std::string& key); + } // namespace openspace #include "json_helper.inl" diff --git a/src/documentation/documentationgenerator.cpp b/src/documentation/documentationgenerator.cpp index f79f13bf3a..826f3a1b30 100644 --- a/src/documentation/documentationgenerator.cpp +++ b/src/documentation/documentationgenerator.cpp @@ -49,28 +49,5 @@ std::string DocumentationGenerator::jsonName() { return _jsonName; } -void DocumentationGenerator::sortJson(nlohmann::json& json) const { - std::sort( - json.begin(), - json.end(), - [](const nlohmann::json& lhs, const nlohmann::json& rhs) { - std::string lhsString = lhs["Name"]; - std::string rhsString = rhs["Name"]; - std::transform( - lhsString.begin(), - lhsString.end(), - lhsString.begin(), - [](unsigned char c) { return std::tolower(c); } - ); - std::transform( - rhsString.begin(), - rhsString.end(), - rhsString.begin(), - [](unsigned char c) { return std::tolower(c); } - ); - - return rhsString > lhsString; - }); -} } // namespace openspace diff --git a/src/interaction/keybindingmanager.cpp b/src/interaction/keybindingmanager.cpp index e51f057dd1..498afeba94 100644 --- a/src/interaction/keybindingmanager.cpp +++ b/src/interaction/keybindingmanager.cpp @@ -148,7 +148,7 @@ nlohmann::json KeybindingManager::generateJsonJson() const { keybind["Action"] = p.second; json.push_back(std::move(keybind)); } - sortJson(json); + sortJson(json, "Name"); nlohmann::json result; result[NameTag] = "Keybindings"; diff --git a/src/properties/propertyowner.cpp b/src/properties/propertyowner.cpp index c30dd4592b..e019dc15ef 100644 --- a/src/properties/propertyowner.cpp +++ b/src/properties/propertyowner.cpp @@ -407,13 +407,13 @@ nlohmann::json PropertyOwner::generateJsonJson() const { for (PropertyOwner* owner : subOwners) { if (owner->identifier() != "Scene") { nlohmann::json jsonOwner = createJson(owner); - sortJson(jsonOwner["Properties"]); - sortJson(jsonOwner["PropertyOwners"]); + sortJson(jsonOwner["Properties"], "Name"); + sortJson(jsonOwner["PropertyOwners"], "Name"); json.push_back(jsonOwner); } } - sortJson(json); + sortJson(json, "Name"); nlohmann::json result; result[NameTag] = "PropertyOwner"; diff --git a/src/scene/scenelicensewriter.cpp b/src/scene/scenelicensewriter.cpp index e719753bf8..f9067692f8 100644 --- a/src/scene/scenelicensewriter.cpp +++ b/src/scene/scenelicensewriter.cpp @@ -113,7 +113,7 @@ nlohmann::json SceneLicenseWriter::generateJsonJson() const { nlohmann::json entry; entry["Name"] = assetLicense.first; entry["Assets"] = assetLicense.second; - sortJson(entry["Assets"]); + sortJson(entry["Assets"], "Name"); assetsJson["Licenses"].push_back(entry); } json.push_back(assetsJson); diff --git a/src/scripting/scriptengine.cpp b/src/scripting/scriptengine.cpp index fbe2c9c595..1d559ee7fd 100644 --- a/src/scripting/scriptengine.cpp +++ b/src/scripting/scriptengine.cpp @@ -570,10 +570,10 @@ nlohmann::json ScriptEngine::generateJsonJson() const { for (const LuaLibrary::Function& f : l.documentations) { library["Functions"].push_back(toJson(f)); } - sortJson(library["Functions"]); + sortJson(library["Functions"], "Name"); json.push_back(library); - sortJson(json); + sortJson(json, "Name"); } nlohmann::json result; diff --git a/src/util/factorymanager.cpp b/src/util/factorymanager.cpp index fed2f0d6b3..c03c8e7d7d 100644 --- a/src/util/factorymanager.cpp +++ b/src/util/factorymanager.cpp @@ -213,7 +213,7 @@ nlohmann::json FactoryManager::generateJsonJson() const { factory["Classes"].push_back(documentation); } } - sortJson(factory["Classes"]); + sortJson(factory["Classes"], "Name"); json.push_back(factory); } // Add all leftover docs @@ -224,7 +224,7 @@ nlohmann::json FactoryManager::generateJsonJson() const { for (const Documentation& doc : docs) { leftovers["Classes"].push_back(generateJsonDocumentation(doc)); } - sortJson(json["Classes"]["Members"]); + sortJson(json["Classes"]["Members"], "Name"); json.push_back(leftovers); // I did not check the output of this for correctness ---abock diff --git a/src/util/json_helper.cpp b/src/util/json_helper.cpp index 1625e27da3..6ec2a6b81b 100644 --- a/src/util/json_helper.cpp +++ b/src/util/json_helper.cpp @@ -84,4 +84,29 @@ std::string formatJsonNumber(double d) { return std::to_string(d); } +void sortJson(nlohmann::json& json, const std::string& key) { + std::sort( + json.begin(), + json.end(), + [&key](const nlohmann::json& lhs, const nlohmann::json& rhs) { + std::string lhsString = lhs[key]; + std::string rhsString = rhs[key]; + std::transform( + lhsString.begin(), + lhsString.end(), + lhsString.begin(), + [](unsigned char c) { return std::tolower(c); } + ); + std::transform( + rhsString.begin(), + rhsString.end(), + rhsString.begin(), + [](unsigned char c) { return std::tolower(c); } + ); + + return rhsString > lhsString; + } + ); +} + } // namespace openspace