Skip to content

Commit

Permalink
take schemaPath as a path, defaults to empty. Call `defaultSchemaPath…
Browse files Browse the repository at this point in the history
…(openstudio::IddFileType filetype)` rather

Addresses #4199 (comment)
  • Loading branch information
jmarrec committed Mar 3, 2021
1 parent f28e51f commit c630048
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
44 changes: 30 additions & 14 deletions src/epjson/epJSONTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,15 @@ auto getGroupName(std::map<std::pair<std::string, std::string>, std::pair<std::s
return cache_result("", false);
}

openstudio::path schemaPath() {
return openstudio::getEnergyPlusDirectory() / openstudio::toPath("Energy+.schema.epJSON");
openstudio::path defaultSchemaPath(openstudio::IddFileType filetype) {
openstudio::path schemaPath;
if (filetype == openstudio::IddFileType::EnergyPlus) {
schemaPath = openstudio::getEnergyPlusDirectory() / openstudio::toPath("Energy+.schema.epJSON");
} else {
LOG_FREE(LogLevel::Error, "epJSONTranslator", "At the moment, only IddFileType::EnergyPlus is supported");
// Later: use OpenStudio.epJSON
}
return schemaPath;
}

Json::Value loadJSON(const openstudio::path& path) {
Expand Down Expand Up @@ -281,7 +288,16 @@ std::string getFieldName(const bool is_array, const IddObject& iddObject, const
return lookedUpFieldName.asString();
}

Json::Value toJSON(const openstudio::IdfFile& idf, const openstudio::path& schemaToLoad) {
Json::Value toJSON(const openstudio::IdfFile& idf, const openstudio::path& t_schemaPath) {

openstudio::path schemaToLoad = t_schemaPath;
if (schemaToLoad.empty()) {
schemaToLoad = defaultSchemaPath(idf.iddFileType());
if (schemaToLoad.empty()) {
return Json::Value::null;
}
}

std::map<std::string, int> type_counts;

Json::Value result;
Expand All @@ -290,6 +306,11 @@ Json::Value toJSON(const openstudio::IdfFile& idf, const openstudio::path& schem
std::map<std::string, std::string> field_names;

Json::Value schema = loadJSON(schemaToLoad);
if (schema.isNull()) {
LOG_FREE(LogLevel::Error, "epJSONTranslator", "Schema is invalid at path=" << schemaToLoad);
return Json::Value::null;
}


result["Version"]["Version 1"]["version_identifier"] = fmt::format("{}.{}", idf.version().major(), idf.version().minor());

Expand Down Expand Up @@ -430,21 +451,16 @@ Json::Value toJSON(const openstudio::IdfFile& idf, const openstudio::path& schem
return result;
}

Json::Value toJSON(const openstudio::Workspace& workspace, const openstudio::path& schemaToLoad) {
if (workspace.iddFileType() != openstudio::IddFileType::EnergyPlus) {
LOG_FREE(LogLevel::Error, "epJSONTranslator", "At the moment, only IddFileType::EnergyPlus is supported");
return Json::Value();
}

return toJSON(workspace.toIdfFile(), schemaToLoad);
Json::Value toJSON(const openstudio::Workspace& workspace, const openstudio::path& t_schemaPath) {
return toJSON(workspace.toIdfFile(), t_schemaPath);
}

std::string toJSONString(const openstudio::IdfFile& idfFile, const openstudio::path& schemaToLoad) {
return toJSON(idfFile, schemaToLoad).toStyledString();
std::string toJSONString(const openstudio::IdfFile& idfFile, const openstudio::path& t_schemaPath) {
return toJSON(idfFile, t_schemaPath).toStyledString();
}

std::string toJSONString(const openstudio::Workspace& workspace, const openstudio::path& schemaToLoad) {
return toJSON(workspace, schemaToLoad).toStyledString();
std::string toJSONString(const openstudio::Workspace& workspace, const openstudio::path& t_schemaPath) {
return toJSON(workspace, t_schemaPath).toStyledString();
}

} // namespace openstudio::epJSON
11 changes: 6 additions & 5 deletions src/epjson/epJSONTranslator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,20 @@ class Value;
namespace openstudio {
class IdfFile;
class Workspace;
class IddFileType;
} // namespace openstudio

namespace openstudio::epJSON {

EPJSON_API openstudio::path schemaPath();
EPJSON_API openstudio::path defaultSchemaPath(openstudio::IddFileType filetype);

EPJSON_API Json::Value loadJSON(const openstudio::path& path);

EPJSON_API Json::Value toJSON(const openstudio::IdfFile& inputFile, const openstudio::path& schema = schemaPath());
EPJSON_API std::string toJSONString(const openstudio::IdfFile& inputFile, const openstudio::path& schema = schemaPath());
EPJSON_API Json::Value toJSON(const openstudio::IdfFile& inputFile, const openstudio::path& schemaPath = openstudio::path());
EPJSON_API std::string toJSONString(const openstudio::IdfFile& inputFile, const openstudio::path& schemaPath = openstudio::path());

EPJSON_API Json::Value toJSON(const openstudio::Workspace& workspace, const openstudio::path& schema = schemaPath());
EPJSON_API std::string toJSONString(const openstudio::Workspace& workspace, const openstudio::path& schema = schemaPath());
EPJSON_API Json::Value toJSON(const openstudio::Workspace& workspace, const openstudio::path& schemaPath = openstudio::path());
EPJSON_API std::string toJSONString(const openstudio::Workspace& workspace, const openstudio::path& schemaPath = openstudio::path());

} // namespace openstudio::epJSON

Expand Down

0 comments on commit c630048

Please sign in to comment.