Remove generateJson function from the DocumentationGenerator and move sortJson function to json_helper

This commit is contained in:
Ylva Selling
2023-04-25 16:32:04 -04:00
parent b8b2867a20
commit 948fdd024b
15 changed files with 51 additions and 39 deletions
@@ -89,7 +89,7 @@ public:
*/
static DocumentationEngine& ref();
std::string generateJson() const override;
std::string generateJson() const;
nlohmann::json generateJsonJson() const;
@@ -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;
@@ -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<KeyWithModifier, std::string>& keyBindings() const;
+1 -1
View File
@@ -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;
+1 -1
View File
@@ -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;
};
+1 -1
View File
@@ -95,7 +95,7 @@ public:
std::vector<std::string> allLuaFunctions() const;
std::string generateJson() const override;
std::string generateJson() const;
nlohmann::json generateJsonJson() const;
private:
+1 -1
View File
@@ -109,7 +109,7 @@ public:
template <class T>
ghoul::TemplateFactory<T>* factory() const;
std::string generateJson() const override;
std::string generateJson() const;
nlohmann::json generateJsonJson() const;
private:
+11
View File
@@ -26,6 +26,7 @@
#define __OPENSPACE_CORE___JSON_HELPER___H__
#include <string>
#include <openspace/json.h>
namespace openspace {
@@ -65,6 +66,16 @@ std::string formatJsonNumber(double d);
template <typename T>
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"
@@ -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
+1 -1
View File
@@ -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";
+3 -3
View File
@@ -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";
+1 -1
View File
@@ -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);
+2 -2
View File
@@ -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;
+2 -2
View File
@@ -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
+25
View File
@@ -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