Use nlohmann::json for all documentation.

The printout is verified to be the same as before, except for 1) some escape characters which are no longer necessary and 2) strings that contained arrays have now become arrays in the json
This commit is contained in:
Ylva Selling
2023-01-10 17:20:20 -05:00
parent 424eb256b0
commit 38fee89eec
11 changed files with 177 additions and 105 deletions
@@ -28,6 +28,7 @@
#include <openspace/documentation/documentationgenerator.h>
#include <openspace/documentation/documentation.h>
#include <openspace/json.h>
#include <ghoul/misc/exception.h>
namespace openspace::documentation {
@@ -109,6 +110,8 @@ public:
std::string generateJson() const override;
nlohmann::json generateJsonJson() const;
private:
/// The list of all Documentation%s that are stored by the DocumentationEngine
@@ -27,6 +27,7 @@
#include <openspace/documentation/documentationgenerator.h>
#include <openspace/json.h>
#include <map>
#include <string>
#include <vector>
@@ -294,6 +295,8 @@ public:
// Generate JSON for documentation
std::string generateJson() const override;
nlohmann::json generateJsonJson() const;
protected:
/// The unique identifier of this PropertyOwner
std::string _identifier;
@@ -27,6 +27,7 @@
#include <openspace/documentation/documentationgenerator.h>
#include <openspace/json.h>
#include <vector>
namespace openspace {
@@ -35,6 +36,7 @@ class SceneLicenseWriter : public DocumentationGenerator {
public:
SceneLicenseWriter();
std::string generateJson() const override;
nlohmann::json generateJsonJson() const;
};
} // namespace openspace
+2 -1
View File
@@ -27,8 +27,8 @@
#include <openspace/util/syncable.h>
#include <openspace/documentation/documentationgenerator.h>
#include <openspace/scripting/lualibrary.h>
#include <openspace/json.h>
#include <ghoul/lua/luastate.h>
#include <ghoul/misc/boolean.h>
#include <filesystem>
@@ -96,6 +96,7 @@ public:
std::vector<std::string> allLuaFunctions() const;
std::string generateJson() const override;
nlohmann::json generateJsonJson() const;
private:
BooleanType(Replace);
+2
View File
@@ -27,6 +27,7 @@
#include <openspace/documentation/documentationgenerator.h>
#include <openspace/json.h>
#include <ghoul/misc/exception.h>
#include <ghoul/misc/templatefactory.h>
#include <memory>
@@ -110,6 +111,7 @@ public:
ghoul::TemplateFactory<T>* factory() const;
std::string generateJson() const override;
nlohmann::json generateJsonJson() const;
private:
/// Singleton member for the Factory Manager
+13 -2
View File
@@ -93,7 +93,7 @@ nlohmann::json generateJsonDocumentation(const Documentation& d) {
json["name"] = d.name;
json["id"] = d.id;
json["entries"];
json["entries"] = nlohmann::json::array();
for (const DocumentationEntry& p : d.entries) {
nlohmann::json entry;
@@ -143,12 +143,23 @@ std::string DocumentationEngine::generateJson() const {
nlohmann::json json;
for (const Documentation& d : _documentations) {
json.push_back(generateJsonDocumentation(d));
json["data"].push_back(generateJsonDocumentation(d));
}
return json.dump();
}
nlohmann::json DocumentationEngine::generateJsonJson() const {
nlohmann::json json;
for (const Documentation& d : _documentations) {
json.push_back(generateJsonDocumentation(d));
}
return json;
}
void DocumentationEngine::addDocumentation(Documentation documentation) {
if (documentation.id.empty()) {
_documentations.push_back(std::move(documentation));
+39 -42
View File
@@ -41,6 +41,7 @@
#include <openspace/interaction/interactionmonitor.h>
#include <openspace/interaction/keybindingmanager.h>
#include <openspace/interaction/sessionrecording.h>
#include <openspace/json.h>
#include <openspace/navigation/navigationhandler.h>
#include <openspace/navigation/orbitalnavigator.h>
#include <openspace/network/parallelpeer.h>
@@ -1023,13 +1024,13 @@ void OpenSpaceEngine::writeDocumentation() {
path = absPath(path).string() + '/';
// Start the async requests as soon as possible so they are finished when we need them
std::future<std::string> root = std::async(
&properties::PropertyOwner::generateJson,
std::future<nlohmann::json> root = std::async(
&properties::PropertyOwner::generateJsonJson,
global::rootPropertyOwner
);
std::future<std::string> scene = std::async(
&properties::PropertyOwner::generateJson,
std::future<nlohmann::json> scene = std::async(
&properties::PropertyOwner::generateJsonJson,
_scene.get()
);
@@ -1038,60 +1039,56 @@ void OpenSpaceEngine::writeDocumentation() {
DocEng.addHandlebarTemplates(FactoryManager::ref().templatesToRegister());
DocEng.addHandlebarTemplates(DocEng.templatesToRegister());
std::string json = "{\"documentation\":[";
nlohmann::json scripting;
scripting["name"] = "Scripting";
scripting["identifier"] = global::scriptEngine->jsonName();
scripting["data"] = global::scriptEngine->generateJsonJson();
json += fmt::format(
R"({{"name":"{}","identifier":"{}","data":{}}},)",
"Scripting",
global::scriptEngine->jsonName(),
global::scriptEngine->generateJson()
);
nlohmann::json topLevel;
topLevel["name"] = "Top Level";
topLevel["identifier"] = DocEng.jsonName();
topLevel["data"] = DocEng.generateJsonJson();
json += fmt::format(
R"({{"name":"{}","identifier":"{}","data":{}}},)",
"Top Level", DocEng.jsonName(), DocEng.generateJson()
);
nlohmann::json factory;
factory["name"] = "Factory";
factory["identifier"] = FactoryManager::ref().jsonName();
factory["data"] = FactoryManager::ref().generateJsonJson();
json += fmt::format(
R"({{"name":"{}","identifier":"{}","data":{}}},)",
"Factory", FactoryManager::ref().jsonName(), FactoryManager::ref().generateJson()
);
json += fmt::format(
/* t(
R"({{"name":"{}","identifier":"{}","data":{}}},)",
"Keybindings",
global::keybindingManager->jsonName(),
global::keybindingManager->generateJson()
);
*/
SceneLicenseWriter writer;
json += fmt::format(
R"({{"name":"{}","identifier":"{}","data":{}}},)",
"Scene License Information", writer.jsonName(), writer.generateJson()
);
nlohmann::json license;
license["name"] = "Scene License Information";
license["identifier"] = writer.jsonName();
license["data"] = writer.generateJsonJson();
json += fmt::format(
R"({{"name":"{}","identifier":"{}","data":{}}},)",
"Scene Properties", "propertylist", root.get()
);
nlohmann::json sceneProperties;
sceneProperties["name"] = "Scene Properties";
sceneProperties["identifier"] = "propertylist1";
sceneProperties["data"] = root.get();
json += fmt::format(
R"({{"name":"{}","identifier":"{}","data":{}}})",
"Scene Graph Information", "propertylist", scene.get()
);
nlohmann::json sceneGraph;
sceneGraph["name"] = "Scene Graph Information";
sceneGraph["identifier"] = "propertylist";
sceneGraph["data"] = scene.get();
json += "]}";
nlohmann::json documentation;
documentation["documentation"].push_back(scripting);
documentation["documentation"].push_back(topLevel);
documentation["documentation"].push_back(factory);
documentation["documentation"].push_back(license);
documentation["documentation"].push_back(sceneProperties);
documentation["documentation"].push_back(sceneGraph);
std::ofstream out("documentationData.js");
out << "export const data = " << json;
out << "export const data = " << documentation.dump();
out.close();
// Add templates for the JSONs we just registered
DocEng.addHandlebarTemplates(global::keybindingManager->templatesToRegister());
DocEng.addHandlebarTemplates(writer.templatesToRegister());
DocEng.addHandlebarTemplates(global::rootPropertyOwner->templatesToRegister());
DocEng.writeDocumentationHtml(path, json);
}
void OpenSpaceEngine::preSynchronization() {
+32 -55
View File
@@ -27,6 +27,7 @@
#include <openspace/engine/globals.h>
#include <openspace/events/event.h>
#include <openspace/events/eventengine.h>
#include <openspace/json.h>
#include <openspace/properties/property.h>
#include <openspace/scene/scene.h>
#include <openspace/util/json_helper.h>
@@ -40,66 +41,34 @@
namespace {
constexpr std::string_view _loggerCat = "PropertyOwner";
void createJson(openspace::properties::PropertyOwner* owner, std::vector<char>& buf) {
nlohmann::json createJson(openspace::properties::PropertyOwner* owner) {
ZoneScoped
using namespace openspace;
nlohmann::json json;
constexpr std::string_view replStr = R"("{}": "{}")";
json["name"] = owner->identifier();
json["properties"] = nlohmann::json::array();
json["propertyOwners"] = nlohmann::json::array();
buf.push_back('{');
fmt::format_to(std::back_inserter(buf), replStr, "name", owner->identifier());
buf.push_back(',');
constexpr std::string_view propertiesText = "\"properties\": [";
buf.insert(buf.end(), propertiesText.begin(), propertiesText.end());
const std::vector<properties::Property*>& properties = owner->properties();
for (properties::Property* p : properties) {
//json << "{";
buf.push_back('{');
//json << fmt::format(replStr, "id", p->identifier()) << ",";
fmt::format_to(std::back_inserter(buf), replStr, "id", p->identifier());
buf.push_back(',');
//json << fmt::format(replStr, "type", p->className()) << ",";
fmt::format_to(std::back_inserter(buf), replStr, "type", p->className());
buf.push_back(',');
fmt::format_to(
std::back_inserter(buf),
replStr, "fullyQualifiedId", p->fullyQualifiedIdentifier()
);
buf.push_back(',');
fmt::format_to(std::back_inserter(buf), replStr, "guiName", p->guiName());
buf.push_back(',');
fmt::format_to(
std::back_inserter(buf),
replStr, "description", escapedJson(p->description())
);
buf.push_back('}');
if (p != properties.back()) {
buf.push_back(',');
}
nlohmann::json propertyJson;
propertyJson["id"] = p->identifier();
propertyJson["type"] = p->className();
propertyJson["fullyQualifiedId"] = p->fullyQualifiedIdentifier();
propertyJson["guiName"] = p->guiName();
propertyJson["description"] = p->description();
json["properties"].push_back(propertyJson);
}
buf.push_back(']');
buf.push_back(',');
constexpr std::string_view propertyOwnersText = "\"propertyOwners\": [";
buf.insert(
buf.end(),
propertyOwnersText.begin(),
propertyOwnersText.end()
);
auto propertyOwners = owner->propertySubOwners();
for (properties::PropertyOwner* o : propertyOwners) {
createJson(o, buf);
if (o != propertyOwners.back()) {
buf.push_back(',');
}
nlohmann::json propertyOwner;
json["propertyOwners"].push_back(createJson(o));
}
buf.push_back(']');
buf.push_back('}');
return json;
}
} // namespace
@@ -424,17 +393,25 @@ void PropertyOwner::removeTag(const std::string& tag) {
std::string PropertyOwner::generateJson() const {
ZoneScoped
std::vector<char> res;
res.reserve(5 * 51024 * 1024); // 5 MB
res.push_back('[');
nlohmann::json json;
std::vector<PropertyOwner*> subOwners = propertySubOwners();
for (PropertyOwner* owner : subOwners) {
createJson(owner, res);
res.push_back(',');
json["data"].push_back(createJson(owner));
}
res.back() = ']';
return std::string(res.begin(), res.end());
return json.dump();
}
nlohmann::json PropertyOwner::generateJsonJson() const {
ZoneScoped
nlohmann::json json;
std::vector<PropertyOwner*> subOwners = propertySubOwners();
for (PropertyOwner* owner : subOwners) {
json.push_back(createJson(owner));
}
return json;
}
} // namespace openspace::properties
+53
View File
@@ -46,6 +46,59 @@ SceneLicenseWriter::SceneLicenseWriter()
)
{}
nlohmann::json SceneLicenseWriter::generateJsonJson() const {
nlohmann::json json = nlohmann::json::array();
std::vector<const Asset*> assets =
global::openSpaceEngine->assetManager().allAssets();
int metaTotal = 0;
int metaCount = 0;
for (const Asset* asset : assets) {
std::optional<Asset::MetaInformation> meta = asset->metaInformation();
if (!meta.has_value()) {
continue;
}
metaTotal++;
}
if (global::profile->meta.has_value()) {
metaTotal++;
nlohmann::json metaJson;
metaJson["name"] = global::profile->meta->name.value_or("");
metaJson["version"] = global::profile->meta->version.value_or("");
metaJson["description"] = global::profile->meta->description.value_or("");
metaJson["author"] = global::profile->meta->author.value_or("");
metaJson["url"] = global::profile->meta->url.value_or("");
metaJson["license"] = global::profile->meta->license.value_or("");
json.push_back(metaJson);
}
for (const Asset* asset : assets) {
std::optional<Asset::MetaInformation> meta = asset->metaInformation();
if (!meta.has_value()) {
continue;
}
constexpr std::string_view replStr = R"("{}": "{}", )";
constexpr std::string_view replStr2 = R"("{}": "{}")";
nlohmann::json assetJson;
assetJson["name"] = meta->name;
assetJson["version"] = meta->version;
assetJson["description"] = meta->description;
assetJson["author"] = meta->author;
assetJson["url"] = meta->url;
assetJson["license"] = meta->license;
assetJson["identifiers"] = meta->identifiers;
assetJson["path"] = asset->path().string();
json.push_back(assetJson);
}
return json;
}
std::string SceneLicenseWriter::generateJson() const {
ZoneScoped
+8 -4
View File
@@ -28,7 +28,6 @@
#include <openspace/engine/configuration.h>
#include <openspace/engine/globals.h>
#include <openspace/interaction/sessionrecording.h>
#include <openspace/json.h>
#include <openspace/network/parallelpeer.h>
#include <openspace/util/json_helper.h>
#include <openspace/util/syncbuffer.h>
@@ -87,6 +86,7 @@ namespace {
using namespace openspace::scripting;
nlohmann::json function;
function["name"] = f.name;
function["arguments"] = nlohmann::json::array();
for (const LuaLibrary::Function::Argument& arg : f.arguments) {
nlohmann::json argument;
@@ -454,6 +454,10 @@ std::vector<std::string> ScriptEngine::allLuaFunctions() const {
}
std::string ScriptEngine::generateJson() const {
return "";
}
nlohmann::json ScriptEngine::generateJsonJson() const {
ZoneScoped
nlohmann::json json;
@@ -462,8 +466,9 @@ std::string ScriptEngine::generateJson() const {
using namespace openspace;
using namespace openspace::scripting;
json["library"] = l.name;
nlohmann::json library;
library["library"] = l.name;
library["subLibraries"] = nlohmann::json::array();
for (const LuaLibrary::Function& f : l.functions) {
library["functions"].push_back(toJson(f));
@@ -474,8 +479,7 @@ std::string ScriptEngine::generateJson() const {
}
json.push_back(library);
}
return json.dump();
return json;
}
void ScriptEngine::writeLog(const std::string& script) {
+20 -1
View File
@@ -101,11 +101,30 @@ std::string FactoryManager::generateJson() const {
for (const std::string& c : registeredClasses) {
json["classes"].push_back(c);
}
json.push_back(factory);
json["data"].push_back(factory);
}
// I did not check the output of this for correctness ---abock
return json.dump();
}
nlohmann::json FactoryManager::generateJsonJson() const {
nlohmann::json json;
for (const FactoryInfo& factoryInfo : _factories) {
nlohmann::json factory;
factory["name"] = factoryInfo.name;
ghoul::TemplateFactoryBase* f = factoryInfo.factory.get();
const std::vector<std::string>& registeredClasses = f->registeredClasses();
for (const std::string& c : registeredClasses) {
factory["classes"].push_back(c);
}
json.push_back(factory);
}
// I did not check the output of this for correctness ---abock
return json;
}
} // namespace openspace