OpenPFC  0.1.4
Phase Field Crystal simulation framework
Loading...
Searching...
No Matches
json_helpers.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
16#ifndef PFC_UI_JSON_HELPERS_HPP
17#define PFC_UI_JSON_HELPERS_HPP
18
19#include <nlohmann/json.hpp>
20#include <string>
21
22namespace pfc {
23namespace ui {
24
25using json = nlohmann::json;
26
38inline json get_json_value(const json &j, const std::string &key,
39 const std::string &section = "") {
40 // First try direct access
41 if (j.contains(key)) {
42 return j[key];
43 }
44 // If section is specified, try nested access
45 if (!section.empty() && j.contains(section) && j[section].is_object() &&
46 j[section].contains(key)) {
47 return j[section][key];
48 }
49 // Try common sections if not specified
50 if (section.empty()) {
51 // Try domain section for domain-related keys
52 if (j.contains("domain") && j["domain"].is_object() &&
53 j["domain"].contains(key)) {
54 return j["domain"][key];
55 }
56 // Try timestepping section for time-related keys
57 if (j.contains("timestepping") && j["timestepping"].is_object() &&
58 j["timestepping"].contains(key)) {
59 return j["timestepping"][key];
60 }
61 }
62 return json(nullptr);
63}
64
65} // namespace ui
66} // namespace pfc
67
68#endif // PFC_UI_JSON_HELPERS_HPP
json get_json_value(const json &j, const std::string &key, const std::string &section="")
Helper function to get a JSON value from either flat or nested structure.
Definition json_helpers.hpp:38
Represents the global simulation domain (the "world").
Definition world.hpp:91