Majoranapp
A C++ library for studying MZM in non-interacting systems
ConnectionsParser.hpp
Go to the documentation of this file.
1 #ifndef PARSERS_CONNECTIONSPARSER_HPP
2 #define PARSERS_CONNECTIONSPARSER_HPP
3 
4 #include <nlohmann/json.hpp>
6 
7 #include "../QuantumSystem/Parameters.hpp"
8 #include "../QuantumSystem/Connections.hpp"
9 
15 {
16 private:
24  static bool HasArray(json &connection)
25  {
26  for (auto element : connection)
27  {
28  if (element.is_array())
29  return true;
30  }
31  return false;
32  }
33 
41  static void GetConnection(json &jsonConnection, Connection &connection, double &value)
42  {
43  // value and array has been provided
44  if (HasArray(jsonConnection))
45  for (auto el : jsonConnection)
46  {
47  if (el.is_array())
48  connection = el.get<Connection>();
49 
50  else if (el.is_number_float() or el.is_number_integer())
51  value = el.get<double>();
52  }
53  // no value has been provided, provided numbers are sites coordinates
54  else
55  connection = jsonConnection.get<Connection>();
56  }
57 
65  static Connections GetAllConnections(json &parameterConnections, double defaultValue)
66  {
67  Connections connections;
68  for (auto jsonConnection : parameterConnections)
69  {
70  Connection connection;
71  double value{defaultValue};
72  GetConnection(jsonConnection, connection, value);
73  connections.map[connection] = value;
74  }
75  return connections;
76  }
77 
78 public:
87  template <class T>
88  static ParametersConnections Parse(json &jsonConnections, T &parameters)
89  {
90  ParametersConnections parConn;
91  for (auto parameterConnectionsItem : jsonConnections.items())
92  {
93  json &parameterConnections = parameterConnectionsItem.value();
94  const std::string &key = parameterConnectionsItem.key();
95  double defaultValue = parameters[key];
96  parConn.map[key] = GetAllConnections(parameterConnections, defaultValue);
97  }
98 
99  return parConn;
100  }
101 };
102 
103 #endif
nlohmann::json json
Definition: ConnectionsParser.hpp:5
static ParametersConnections Parse(json &jsonConnections, T &parameters)
Parsing json object to ParametersConnetions.
Definition: ConnectionsParser.hpp:88
static Connections GetAllConnections(json &parameterConnections, double defaultValue)
Get the all Connections objects from json object.
Definition: ConnectionsParser.hpp:65
parsing connections from JSON file
Definition: ConnectionsParser.hpp:14
static void GetConnection(json &jsonConnection, Connection &connection, double &value)
Get the Connection object.
Definition: ConnectionsParser.hpp:41
static bool HasArray(json &connection)
checks if one of the item in json is an array
Definition: ConnectionsParser.hpp:24