Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
json_utils.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
19#include "shamcomm/logs.hpp"
22
23namespace shamrock {
24
26 std::string log_json_changes(
27 const nlohmann::json &j_current,
28 const nlohmann::json &j,
29 bool has_used_defaults,
30 bool has_updated_config);
31
34 template<class T>
35 inline void get_to_if_contains(
36 const nlohmann::json &j, const std::string &key, T &value, bool &has_used_defaults) {
37 if (j.contains(key)) {
38 j.at(key).get_to(value);
39 } else {
40 has_used_defaults = true;
41 }
42 }
43
47 template<class T>
49 const nlohmann::json &j,
50 const std::string &key,
51 T &value,
52 std::initializer_list<const char *> fallbacks,
53 bool &has_used_defaults,
54 bool &has_updated_config) {
55
56 if (j.contains(key)) {
57 j.at(key).get_to(value);
58 } else {
59 for (const char *fallback : fallbacks) {
60 if (j.contains(fallback)) {
61 j.at(fallback).get_to(value);
62 has_updated_config = true;
63 if (shamcomm::world_rank() == 0) {
64 shamcomm::logs::warn_ln(
65 "SPH::SolverConfig",
66 "Updating old key [" + std::string(fallback) + "] to new key [" + key
67 + "] in from_json");
68 }
69 return;
70 }
71 }
72 has_used_defaults = true;
73 }
74 }
75
76} // namespace shamrock
77
86#define SHAMROCK_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \
87 template<typename BasicJsonType> \
88 inline void to_json(BasicJsonType &j, const ENUM_TYPE &e) { \
89 /* NOLINTNEXTLINE(modernize-type-traits) we use C++11 */ \
90 static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
91 /* NOLINTNEXTLINE(modernize-avoid-c-arrays) we don't want to depend on <array> */ \
92 static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
93 \
94 auto it = std::find_if( \
95 std::begin(m), \
96 std::end(m), \
97 [e](const std::pair<ENUM_TYPE, BasicJsonType> &ej_pair) -> bool { \
98 return ej_pair.first == e; \
99 }); \
100 if (it != std::end(m)) { \
101 j = it->second; \
102 } else { \
103 throw shambase::make_except_with_loc<std::runtime_error>( \
104 "Invalid " #ENUM_TYPE " value: " + std::to_string(e)); \
105 } \
106 } \
107 template<typename BasicJsonType> \
108 inline void from_json(const BasicJsonType &j, ENUM_TYPE &e) { \
109 /* NOLINTNEXTLINE(modernize-type-traits) we use C++11 */ \
110 static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
111 /* NOLINTNEXTLINE(modernize-avoid-c-arrays) we don't want to depend on <array> */ \
112 static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
113 \
114 auto it = std::find_if( \
115 std::begin(m), \
116 std::end(m), \
117 [&j](const std::pair<ENUM_TYPE, BasicJsonType> &ej_pair) -> bool { \
118 return ej_pair.second == j; \
119 }); \
120 if (it != std::end(m)) { \
121 e = it->first; \
122 } else { \
123 throw shambase::make_except_with_loc<std::runtime_error>( \
124 "Invalid " #ENUM_TYPE " value: " + std::to_string(e)); \
125 } \
126 }
i32 world_rank()
Gives the rank of the current process in the MPI communicator.
Definition worldInfo.cpp:40
namespace for the main framework
Definition __init__.py:1
void get_to_if_contains(const nlohmann::json &j, const std::string &key, T &value, bool &has_used_defaults)
std::string log_json_changes(const nlohmann::json &j_current, const nlohmann::json &j, bool has_used_defaults, bool has_updated_config)
Shown the changes between two JSON objects to log config changes.
void get_to_if_contains_fallbacks(const nlohmann::json &j, const std::string &key, T &value, std::initializer_list< const char * > fallbacks, bool &has_used_defaults, bool &has_updated_config)
Functions related to the MPI communicator.