-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Convert YAML to JSON * Fix YAML CMake * Add YAML to JSON to main
- Loading branch information
Showing
6 changed files
with
133 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# yaml-cpp (https://github.com/jbeder/yaml-cpp) | ||
# License: MIT | ||
|
||
if(TARGET yaml-cpp::yaml-cpp) | ||
return() | ||
endif() | ||
|
||
message(STATUS "Third-party: creating target 'yaml-cpp::yaml-cpp'") | ||
|
||
include(CPM) | ||
CPMAddPackage("gh:jbeder/yaml-cpp#0.8.0") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Original source code from https://github.com/mircodezorzi/tojson (MIT License) | ||
|
||
#include "YamlToJson.hpp" | ||
|
||
#include <yaml-cpp/yaml.h> | ||
|
||
namespace polyfem::io | ||
{ | ||
namespace | ||
{ | ||
inline nlohmann::json parse_scalar(const YAML::Node &node) | ||
{ | ||
int i; | ||
double d; | ||
bool b; | ||
std::string s; | ||
|
||
if (YAML::convert<int>::decode(node, i)) | ||
return i; | ||
if (YAML::convert<double>::decode(node, d)) | ||
return d; | ||
if (YAML::convert<bool>::decode(node, b)) | ||
return b; | ||
if (YAML::convert<std::string>::decode(node, s)) | ||
return s; | ||
|
||
return nullptr; | ||
} | ||
|
||
/// @todo refactor and pass nlohmann::json down by reference instead of returning it | ||
inline nlohmann::json yaml_to_json(const YAML::Node &root) | ||
{ | ||
nlohmann::json j{}; | ||
|
||
switch (root.Type()) | ||
{ | ||
case YAML::NodeType::Null: | ||
break; | ||
case YAML::NodeType::Scalar: | ||
return parse_scalar(root); | ||
case YAML::NodeType::Sequence: | ||
for (auto &&node : root) | ||
j.emplace_back(yaml_to_json(node)); | ||
break; | ||
case YAML::NodeType::Map: | ||
for (auto &&it : root) | ||
j[it.first.as<std::string>()] = yaml_to_json(it.second); | ||
break; | ||
default: | ||
break; | ||
} | ||
return j; | ||
} | ||
} // namespace | ||
|
||
json yaml_string_to_json(const std::string &yaml_str) | ||
{ | ||
YAML::Node root = YAML::Load(yaml_str); | ||
return yaml_to_json(root); | ||
} | ||
|
||
json yaml_file_to_json(const std::string &yaml_filepath) | ||
{ | ||
YAML::Node root = YAML::LoadFile(yaml_filepath); | ||
return yaml_to_json(root); | ||
} | ||
} // namespace polyfem::io |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#pragma once | ||
|
||
#include <polyfem/Common.hpp> | ||
|
||
namespace polyfem::io | ||
{ | ||
/// @brief Convert YAML string to JSON. | ||
json yaml_string_to_json(const std::string &yaml_str); | ||
|
||
/// @brief Load a YAML file to JSON. | ||
json yaml_file_to_json(const std::string &yaml_filepath); | ||
} // namespace polyfem::io |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters