OpenPFC  0.1.4
Phase Field Crystal simulation framework
Loading...
Searching...
No Matches
toml_to_json.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 VTT Technical Research Centre of Finland Ltd
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
15#ifndef PFC_TOML_TO_JSON_HPP
16#define PFC_TOML_TO_JSON_HPP
17
18#include <nlohmann/json.hpp>
19#include <toml++/toml.hpp>
20
21namespace pfc {
22namespace utils {
23
24// Forward declaration
25inline nlohmann::json toml_to_json(const toml::node &node_ref);
26
36inline nlohmann::json toml_to_json(const toml::table &table) {
37 nlohmann::json j = nlohmann::json::object();
38
39 for (const auto &[key, value] : table) {
40 std::string key_str(key.str());
41 // value is a const toml::node& when iterating over table
42 j[key_str] = toml_to_json(value);
43 }
44
45 return j;
46}
47
57inline nlohmann::json toml_to_json(const toml::node &node_ref) {
58 switch (node_ref.type()) {
60 return toml_to_json(*node_ref.as_table());
61 }
62 case toml::node_type::array: {
63 nlohmann::json arr = nlohmann::json::array();
64 for (const auto &elem : *node_ref.as_array()) {
65 // elem is a node_view, convert it directly
66 arr.push_back(toml_to_json(elem));
67 }
68 return arr;
69 }
70 case toml::node_type::string: {
71 return std::string(node_ref.as_string()->get());
72 }
74 return node_ref.as_integer()->get();
75 }
77 return node_ref.as_floating_point()->get();
78 }
80 return node_ref.as_boolean()->get();
81 }
83 auto date = node_ref.as_date()->get();
84 // Convert date to ISO 8601 string format
85 std::string date_str = std::to_string(date.year) + "-" +
86 (date.month < 10 ? "0" : "") +
87 std::to_string(date.month) + "-" +
88 (date.day < 10 ? "0" : "") + std::to_string(date.day);
89 return date_str;
90 }
92 auto time = node_ref.as_time()->get();
93 // Convert time to ISO 8601 string format
94 std::string time_str =
95 (time.hour < 10 ? "0" : "") + std::to_string(time.hour) + ":" +
96 (time.minute < 10 ? "0" : "") + std::to_string(time.minute) + ":" +
97 (time.second < 10 ? "0" : "") + std::to_string(time.second);
98 if (time.nanosecond > 0) {
99 time_str += "." + std::to_string(time.nanosecond);
100 }
101 return time_str;
102 }
104 auto dt = node_ref.as_date_time()->get();
105 // Convert date-time to ISO 8601 string format
106 std::string dt_str =
107 std::to_string(dt.date.year) + "-" + (dt.date.month < 10 ? "0" : "") +
108 std::to_string(dt.date.month) + "-" + (dt.date.day < 10 ? "0" : "") +
109 std::to_string(dt.date.day) + "T" + (dt.time.hour < 10 ? "0" : "") +
110 std::to_string(dt.time.hour) + ":" + (dt.time.minute < 10 ? "0" : "") +
111 std::to_string(dt.time.minute) + ":" + (dt.time.second < 10 ? "0" : "") +
112 std::to_string(dt.time.second);
113 if (dt.time.nanosecond > 0) {
114 dt_str += "." + std::to_string(dt.time.nanosecond);
115 }
116 if (dt.offset.has_value()) {
117 auto offset = dt.offset.value();
118 int offset_hours = offset.minutes / 60;
119 int offset_minutes = offset.minutes % 60;
120 dt_str += std::string(offset_hours >= 0 ? "+" : "-") +
121 std::string(std::abs(offset_hours) < 10 ? "0" : "") +
122 std::to_string(std::abs(offset_hours)) + ":" +
123 std::string(std::abs(offset_minutes) < 10 ? "0" : "") +
124 std::to_string(std::abs(offset_minutes));
125 }
126 return dt_str;
127 }
128 default: {
129 // For unknown types, try to convert to string or return null
130 if (node_ref.is_string()) {
131 return std::string(node_ref.as_string()->get());
132 }
133 return nlohmann::json(nullptr);
134 }
135 }
136}
137
146inline nlohmann::json
148 const toml::node *node = node_view.node();
149 if (!node) {
150 return nlohmann::json(nullptr);
151 }
152 return toml_to_json(*node);
153}
154
155} // namespace utils
156} // namespace pfc
157
158#endif // PFC_TOML_TO_JSON_HPP
Represents the global simulation domain (the "world").
Definition world.hpp:91
World(const Int3 &lower, const Int3 &upper, const CoordinateSystem< T > &cs)
Constructs a World object.
nlohmann::json toml_to_json(const toml::node &node_ref)
Convert a TOML node to nlohmann::json.
Definition toml_to_json.hpp:57