From e463e9e598457df4792e28bde66862736d092759 Mon Sep 17 00:00:00 2001 From: Ylva Selling Date: Fri, 17 Mar 2023 15:46:02 -0400 Subject: [PATCH] Sort json --- src/properties/propertyowner.cpp | 16 ++++++++++++++++ src/scene/scenelicensewriter.cpp | 16 +++++++++++++++- src/scripting/scriptengine.cpp | 17 +++++++++++++++++ src/util/factorymanager.cpp | 18 +++++++++++++++++- 4 files changed, 65 insertions(+), 2 deletions(-) diff --git a/src/properties/propertyowner.cpp b/src/properties/propertyowner.cpp index 5c0553c0ad..3a2c8f890c 100644 --- a/src/properties/propertyowner.cpp +++ b/src/properties/propertyowner.cpp @@ -41,6 +41,19 @@ namespace { constexpr std::string_view _loggerCat = "PropertyOwner"; + void sortJson(nlohmann::json& json) { + 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; + }); + } + nlohmann::json createJson(openspace::properties::PropertyOwner* owner) { ZoneScoped @@ -65,12 +78,14 @@ namespace { json["Properties"].push_back(propertyJson); } + sortJson(json["Properties"]); auto propertyOwners = owner->propertySubOwners(); for (properties::PropertyOwner* o : propertyOwners) { nlohmann::json propertyOwner; json["PropertyOwners"].push_back(createJson(o)); } + sortJson(json["PropertyOwners"]); return json; } @@ -414,6 +429,7 @@ nlohmann::json PropertyOwner::generateJsonJson() const { json.push_back(createJson(owner)); } } + sortJson(json); return json; } diff --git a/src/scene/scenelicensewriter.cpp b/src/scene/scenelicensewriter.cpp index e09357e3fb..27706a62ad 100644 --- a/src/scene/scenelicensewriter.cpp +++ b/src/scene/scenelicensewriter.cpp @@ -46,6 +46,19 @@ SceneLicenseWriter::SceneLicenseWriter() ) {} +void sortJson(nlohmann::json& json) { + 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; + }); +} + nlohmann::json SceneLicenseWriter::generateJsonJson() const { nlohmann::json json; @@ -108,6 +121,7 @@ nlohmann::json SceneLicenseWriter::generateJsonJson() const { assetLicenses[license].push_back(assetJson); } + nlohmann::json assetsJson; assetsJson["Name"] = "Assets"; assetsJson["Type"] = "Licenses"; @@ -116,10 +130,10 @@ nlohmann::json SceneLicenseWriter::generateJsonJson() const { nlohmann::json entry; entry["Name"] = assetLicense.first; entry["Assets"] = assetLicense.second; + sortJson(entry["Assets"]); assetsJson["Licenses"].push_back(entry); } json.push_back(assetsJson); - return json; } diff --git a/src/scripting/scriptengine.cpp b/src/scripting/scriptengine.cpp index 7a8b9a843f..82f90aa4dd 100644 --- a/src/scripting/scriptengine.cpp +++ b/src/scripting/scriptengine.cpp @@ -81,6 +81,19 @@ namespace { return result; } + void sortJson(nlohmann::json& json) { + 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; + }); + } + nlohmann::json toJson(const openspace::scripting::LuaLibrary::Function& f) { using namespace openspace; using namespace openspace::scripting; @@ -98,6 +111,7 @@ namespace { } function["Return Type"] = f.returnType; function["Help"] = f.helpText; + sortJson(function["Arguments"]); return function; } @@ -472,7 +486,10 @@ nlohmann::json ScriptEngine::generateJsonJson() const { for (const LuaLibrary::Function& f : l.documentations) { library["Functions"].push_back(toJson(f)); } + sortJson(library["Functions"]); json.push_back(library); + + sortJson(json); } return json; } diff --git a/src/util/factorymanager.cpp b/src/util/factorymanager.cpp index 28a153faa0..7e8c1849bb 100644 --- a/src/util/factorymanager.cpp +++ b/src/util/factorymanager.cpp @@ -42,6 +42,19 @@ namespace { using namespace openspace; using namespace openspace::documentation; +void sortJson(nlohmann::json& json) { + 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; + }); +} + nlohmann::json generateJsonDocumentation(const Documentation& d) { nlohmann::json json; @@ -89,6 +102,7 @@ nlohmann::json generateJsonDocumentation(const Documentation& d) { } json["Members"].push_back(entry); } + sortJson(json["Members"]); return json; } @@ -216,6 +230,7 @@ nlohmann::json FactoryManager::generateJsonJson() const { factory["Classes"].push_back(documentation); } } + sortJson(factory["Classes"]); json.push_back(factory); } // Add all leftover docs @@ -226,8 +241,9 @@ nlohmann::json FactoryManager::generateJsonJson() const { for (const Documentation& doc : docs) { leftovers["Classes"].push_back(generateJsonDocumentation(doc)); } + sortJson(leftovers["Classes"]); json.push_back(leftovers); - + sortJson(json); // I did not check the output of this for correctness ---abock return json; }