Feat C++ API
A feature engineering automation tool
serialization.h
Go to the documentation of this file.
1 /* FEAT
2 copyright 2017 William La Cava
3 license: GNU/GPL v3
4 */
5 #ifndef SERIALIZATION_H
6 #define SERIALIZATION_H
7 
8 #include <Eigen/Dense>
9 #include <Eigen/Core>
10 
11 #include "../init.h"
12 using nlohmann::json;
13 
14 namespace Eigen
15 {
16 
17  /*
18  * Serialization for Eigen dynamic types
19  */
20  template <typename T>
21  void to_json(json& j, const MatrixBase<T>& matrix)
22  {
23  for (int row = 0; row < matrix.rows(); ++row)
24  {
25  json column = json::array();
26  for (int col = 0; col < matrix.cols(); ++col)
27  {
28  column.push_back(matrix(row, col));
29  }
30  j.push_back(column);
31  }
32  }
33 
34  template <typename T>
35  void from_json(const json& j, Matrix<T, Dynamic, Dynamic>& matrix)
36  {
37  if (j.size() == 0) return;
38  matrix.resize(j.size(), j.at(0).size());
39  for (std::size_t row = 0; row < j.size(); ++row)
40  {
41  const auto& jrow = j.at(row);
42  for (std::size_t col = 0; col < jrow.size(); ++col)
43  {
44  const auto& value = jrow.at(col);
45  value.get_to(matrix(row, col));
46  }
47 
48  }
49  }
50 
51  template <typename T>
52  void from_json(const json& j, Matrix<T, Dynamic, 1>& V)
53  {
54  V.resize(j.size());
55  for (int i = 0 ; i < j.size(); ++i)
56  {
57  j.at(i).get_to(V(i));
58  }
59 
60  }
61 }
62 
63 
64 #endif
void to_json(json &j, const MatrixBase< T > &matrix)
Definition: serialization.h:21
void from_json(const json &j, Matrix< T, Dynamic, Dynamic > &matrix)
Definition: serialization.h:35
int i
Definition: params.cc:552