CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
container.hh
Go to the documentation of this file.
1 #ifndef CASM_jsonIO_container
2 #define CASM_jsonIO_container
3 
6 
7 //#include "casm/container/Array.hh"
8 //#include "casm/container/LinearAlgebra.hh"
9 
10 namespace CASM {
11  /*
12  // --- Array<T> ------------------------------
13 
15  template<typename T>
16  jsonParser &to_json(const Array<T> &value, jsonParser &json) {
17  return json.put_array(value.begin(), value.end());
18  }
19 
21  template<typename T>
22  void from_json(Array<T> &value, const jsonParser &json) {
23  value.resize(json.size());
24  for(int i = 0; i < json.size(); i++)
25  from_json(value[i], json[i]);
26  }
27 
28  // --- Matrix & Vector -----------------------
29 
31  template <typename T, Index N>
32  jsonParser &to_json(const Matrix<T, N> &value, jsonParser &json) {
33  json.put_array();
34  for(Index i = 0; i < value.num_rows(); i++) {
35  jsonParser json_row;
36  json_row.put_array();
37  for(Index j = 0; j < value.num_cols(); j++) {
38  json_row.push_back(value(i, j));
39  }
40  json.push_back(json_row);
41  }
42  return json;
43  }
44 
45  template <typename T, Index N>
46  void from_json(Matrix<T, N> &value, const jsonParser &json) {
47  for(Index i = 0; i < value.num_rows(); i++) {
48  for(Index j = 0; j < value.num_cols(); j++) {
49  from_json(value.at(i, j), json[i][j]);
50  }
51  }
52  }
53 
55  template <typename T, Index N>
56  jsonParser &to_json(const Vector<T, N> &value, jsonParser &json) {
57  json.put_array();
58  for(Index i = 0; i < value.size(); i++)
59  json.push_back(value[i]);
60  return json;
61  }
62 
63  template <typename T, Index N>
64  void from_json(Vector<T, N> &value, const jsonParser &json) {
65  for(Index i = 0; i < value.size(); i++)
66  from_json(value.at(i), json[i]);
67  }
68 
69 
70  // --- Permutation ---------------------------
71 
72  class Permutation;
73 
74  jsonParser &to_json(const Permutation &perm, jsonParser &json);
75 
76  template<>
77  Permutation from_json(const jsonParser &json);
78 
79  void from_json(Permutation &perm, const jsonParser &json);
80  */
81 
87  // --- std::map<std::string, T> --------------
88 
90  template<typename T>
91  jsonParser &to_json(const std::map<std::string, T> &map, jsonParser &json) {
92  return json.put_obj(map.begin(), map.end());
93  }
94 
98  template<typename T>
99  void from_json(std::map<std::string, T> &map, const jsonParser &json) {
100  map.clear();
101  for(auto it = json.begin(); it != json.end(); ++it) {
102  from_json(map[it.name()], *it);
103  }
104  }
105 
106  // --- std::vector<T> --------------
107 
109  template<typename T>
110  jsonParser &to_json(const std::vector<T> &vec, jsonParser &json) {
111  return json.put_array(vec.begin(), vec.end());
112  }
113 
117  template<typename T>
118  void from_json(std::vector<T> &vec, const jsonParser &json) {
119  vec.resize(json.size());
120  int i = 0;
121  for(auto it = json.begin(); it != json.end(); ++it, ++i) {
122  from_json(vec[i], *it);
123  }
124  }
125 
126  // --- std::array<T,std::size_t N> --------------
127 
129  template<typename T, std::size_t N>
130  jsonParser &to_json(const std::array<T, N> &arr, jsonParser &json) {
131  return json.put_array(arr.begin(), arr.end());
132  }
133 
137  template<typename T, std::size_t N>
138  void from_json(std::array<T, N> &arr, const jsonParser &json) {
139  if(json.size() != N) {
140  std::ostringstream ss;
141  ss << "Attempting to to initialize std::array of size " << N << " from JSON array of size " << json.size() << ":\n " << json << "\n\n";
142  throw std::range_error(ss.str());
143  }
144 
145  std::size_t i = 0;
146  for(auto it = json.begin(); it != json.end(); ++it, ++i) {
147  from_json(arr[i], *it);
148  }
149  }
150 
151  // --- std::set<T> --------------
152 
154  template<typename T, typename Compare>
155  jsonParser &to_json(const std::set<T, Compare> &set, jsonParser &json) {
156  return json.put_array(set.begin(), set.end());
157  }
158 
162  template<typename T, typename Compare, typename...Args>
163  void from_json(std::set<T, Compare> &set, const jsonParser &json, Args...args) {
164  set.clear();
165  for(auto it = json.begin(); it != json.end(); ++it) {
166  set.insert(jsonConstructor<T>::from_json(*it, args...));
167  }
168  }
169  //}
170  //
171  //namespace Eigen {
172 
174  template <typename Derived>
175  CASM::jsonParser &to_json(const Eigen::MatrixBase<Derived> &value, CASM::jsonParser &json) {
176  json.put_array();
177  for(int i = 0; i < value.rows(); i++) {
178  CASM::jsonParser json_row;
179  json_row.put_array();
180  for(int j = 0; j < value.cols(); j++) {
181  json_row.push_back(value(i, j));
182  }
183  json.push_back(json_row);
184  }
185  return json;
186  }
187 
188 
190  template <typename Derived>
191  CASM::jsonParser &to_json_array(const Eigen::MatrixBase<Derived> &value, CASM::jsonParser &json) {
192  json.put_array();
193  if(value.rows() == 1) {
194  for(int i = 0; i < value.cols(); ++i) {
195  json.push_back(value(0, i));
196  }
197  }
198  else if(value.cols() == 1) {
199  for(int i = 0; i < value.rows(); ++i) {
200  json.push_back(value(i, 0));
201  }
202  }
203  else {
204  throw std::runtime_error("Error in 'to_json_array': Not a vector");
205  }
206  return json;
207  }
208 
212  template <typename Derived>
213  void from_json(Eigen::MatrixBase<Derived> &value, const CASM::jsonParser &json) {
214  if(json.is_array() && !json[0].is_array()) {
215  value.derived().resize(json.size(), 1);
216  for(int i = 0; i < value.rows(); i++) {
217  from_json(value(i, 0), json[i]);
218  }
219  }
220  else {
221  value.derived().resize(json.size(), json[0].size());
222  for(int i = 0; i < value.rows(); i++) {
223  for(int j = 0; j < value.cols(); j++) {
224  from_json(value(i, j), json[i][j]);
225  }
226  }
227  }
228  }
229 
231 }
232 
233 #endif
size_type size() const
Returns array size if *this is a JSON array, object size if *this is a JSON object, 1 otherwise.
Definition: jsonParser.cc:430
void from_json(ClexDescription &desc, const jsonParser &json)
iterator end()
Returns iterator to end of JSON object or JSON array.
Definition: jsonParser.cc:465
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
Main CASM namespace.
Definition: complete.cpp:8
iterator begin()
Returns const_iterator to beginning of JSON object or JSON array.
Definition: jsonParser.cc:440
ProjectSettings & set
Definition: settings.cc:103
CASM::jsonParser & to_json_array(const Eigen::MatrixBase< Derived > &value, CASM::jsonParser &json)
Write Eigen Matrix with 1 row or 1 column to JSON array.
Definition: container.hh:191
Helper struct for constructing objects that need additional data.
Definition: jsonParser.hh:486
jsonParser & put_obj()
Puts new empty JSON object.
Definition: jsonParser.hh:276
jsonParser & push_back(const T &value)
Puts new valued element at end of array of any type T for which 'jsonParser& to_json( const T &value...
Definition: jsonParser.hh:696
bool is_array() const
Check if array type.
Definition: jsonParser.cc:281
jsonParser & put_array()
Puts new empty JSON array.
Definition: jsonParser.hh:285