19#include "nlohmann/json.hpp"
23namespace shammodels::common {
26 inline py::object to_py_json(
const T &self) {
27 auto json_loads = py::module_::import(
"json").attr(
"loads");
28 nlohmann::json j = self;
29 return json_loads(j.dump());
33 inline T from_py_json(py::object json_data) {
34 auto json_dumps = py::module_::import(
"json").attr(
"dumps");
35 std::string j = json_dumps(json_data).cast<std::string>();
36 return nlohmann::json::parse(j).get<T>();
39 template<
class TConfig>
40 inline void add_json_defs(py::class_<TConfig> &cls) {
44 return shammodels::common::to_py_json(self);
46 "Converts the config to a json like dictionary");
50 [](TConfig &self, py::object json_data) {
51 self = shammodels::common::from_py_json<TConfig>(json_data);
53 "Converts a json like dictionary to a config");
Pybind11 include and definitions.