Debian Codegraph @

doc/ref/corelib/reflect/Eigen-Matrix-example.md

raw
1 ### Eigen::Matrix examples
2
3 This example shows how to specialize [json_conv_traits](json_conv_traits.md) for an [Eigen matrix class](https://eigen.tuxfamily.org/dox-devel/group__TutorialMatrixClass.html).
4 It defines separate `json_conv_traits` class templates for the dynamic and fixed sized row/column cases.
5
6 ```cpp
7 #include <jsoncons/json.hpp>
8 #include <Eigen/Dense>
9 #include <iostream>
10 #include <cassert>
11
12 namespace jsoncons {
13 namespace reflect {
14
15 // fixed sized row/columns
16
17 template <typename Json, typename Scalar, std::size_t RowsAtCompileTime, std::size_t ColsAtCompileTime>
18 struct json_conv_traits<Json, Eigen::Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime>>
19 {
20 using allocator_type = typename Json::allocator_type;
21 using matrix_type = Eigen::Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime>;
22 using result_type = conversion_result<matrix_type>;
23
24 static bool is(const Json& jval) noexcept
25 {
26 if (!jval.is_array() || jval.size() != RowsAtCompileTime)
27 return false;
28
29 for (std::size_t i = 0; i < jval.size(); ++i)
30 {
31 const Json& row = jval[i];
32 if (row.size() != ColsAtCompileTime)
33 {
34 return false;
35 }
36 }
37
38 return true;
39 }
40
41 template <typename Alloc, typename TempAlloc>
42 static result_type try_as(const allocator_set<Alloc, TempAlloc>& aset, const Json& jval)
43 {
44 if (!jval.is_array())
45 {
46 return result_type{jsoncons::unexpect, conv_errc::not_array};
47 }
48 if (jval.size() != RowsAtCompileTime)
49 {
50 return result_type{jsoncons::unexpect, conv_errc::conversion_failed, "Expected " +
51 std::to_string(RowsAtCompileTime) + " rows, found " + std::to_string(jval.size())};
52 }
53
54 matrix_type m(RowsAtCompileTime, ColsAtCompileTime);
55
56 for (std::size_t i = 0; i < jval.size(); ++i)
57 {
58 const Json& row = jval[i];
59 if (row.size() != ColsAtCompileTime)
60 {
61 return result_type{jsoncons::unexpect, conv_errc::conversion_failed, "Expected " +
62 std::to_string(ColsAtCompileTime) + " columns, found " + std::to_string(row.size())};
63 }
64 for (std::size_t j = 0; j < row.size(); ++j)
65 {
66 m(i, j) = row[j].as<Scalar>();
67 }
68 }
69
70 return result_type{std::move(m)};
71 }
72
73 template <typename Alloc, typename TempAlloc>
74 static Json to_json(const allocator_set<Alloc, TempAlloc>& aset, const matrix_type& m)
75 {
76 auto jval = jsoncons::make_obj_using_allocator<Json>(aset.get_allocator(), jsoncons::json_array_arg);
77 for (Eigen::Index i = 0; i < m.rows(); ++i)
78 {
79 auto row = jsoncons::make_obj_using_allocator<Json>(aset.get_allocator(), jsoncons::json_array_arg);
80 for (Eigen::Index j = 0; j < m.cols(); ++j)
81 {
82 row.push_back(m(i, j));
83 }
84 jval.push_back(std::move(row));
85 }
86
87 return jval;
88 }
89 };
90
91 // dynamic sized row/columns
92
93 template <typename Json, typename Scalar>
94 struct json_conv_traits<Json, Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>>
95 {
96 using allocator_type = typename Json::allocator_type;
97 using matrix_type = Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>;
98 using result_type = conversion_result<matrix_type>;
99
100 static bool is(const Json& jval) noexcept
101 {
102 if (!jval.is_array())
103 return false;
104
105 std::size_t cols = jval[0].size();
106
107 for (std::size_t i = 0; i < jval.size(); ++i)
108 {
109 const Json& row = jval[i];
110 if (row.size() != cols)
111 {
112 return false;
113 }
114 }
115
116 return true;
117 }
118
119 template <typename Alloc, typename TempAlloc>
120 static result_type try_as(const allocator_set<Alloc, TempAlloc>&, const Json& jval)
121 {
122 if (!jval.is_array())
123 {
124 return result_type{jsoncons::unexpect, conv_errc::not_array};
125 }
126 if (jval.empty() || jval[0].size() == 0)
127 {
128 return result_type{jsoncons::unexpect, conv_errc::conversion_failed, "Invalid matrix"};
129 }
130
131 std::size_t cols = jval[0].size();
132 matrix_type m(jval.size(), cols);
133
134 for (std::size_t i = 0; i < jval.size(); ++i)
135 {
136 const Json& row = jval[i];
137 if (row.size() != cols)
138 {
139 return result_type{jsoncons::unexpect, conv_errc::conversion_failed};
140 }
141 for (std::size_t j = 0; j < row.size(); ++j)
142 {
143 m(i, j) = row[j].template as<Scalar>();
144 }
145 }
146
147 return result_type{std::move(m)};
148 }
149
150 template <typename Alloc, typename TempAlloc>
151 static Json to_json(const allocator_set<Alloc, TempAlloc>& aset, const matrix_type& m)
152 {
153 auto jval = jsoncons::make_obj_using_allocator<Json>(aset.get_allocator(), jsoncons::json_array_arg);
154 for (Eigen::Index i = 0; i < m.rows(); ++i)
155 {
156 auto row = jsoncons::make_obj_using_allocator<Json>(aset.get_allocator(), jsoncons::json_array_arg);
157 for (Eigen::Index j = 0; j < m.cols(); ++j)
158 {
159 row.push_back(m(i, j));
160 }
161 jval.push_back(std::move(row));
162 }
163
164 return jval;
165 }
166 };
167
168 } // namespace reflect
169 } // namespace jsoncons
170 ```
171
172 #### Fixed size matrix example
173
174 ```cpp
175 using matrix_type = Eigen::Matrix<double, 3, 4>;
176
177 // Don't use auto here! (Random returns proxy)
178 matrix_type m1 = matrix_type::Random(3, 4);
179 std::cout << "(1) " << '\n' << m1 << "\n\n";
180
181 std::string buffer;
182 auto options = jsoncons::json_options{}.array_array_line_splits(jsoncons::line_split_kind::same_line);
183
184 auto wresult = jsoncons::try_encode_json_pretty(m1, buffer, options);
185 assert(wresult);
186
187 std::cout << "(2) " << '\n' << buffer << "\n\n";
188
189 auto rresult = jsoncons::try_decode_json<matrix_type>(buffer);
190 assert(rresult);
191 auto& m2(*rresult);
192 assert(m1 == m2);
193
194 // This should fail
195 auto r3by3 = jsoncons::try_decode_json<Eigen::Matrix<double, 3, 3>>(buffer);
196 if (!r3by3)
197 {
198 std::cout << "(3) " << r3by3.error().message() << "\n\n";
199 }
200
201 auto jresult = jsoncons::try_decode_json<jsoncons::json>(buffer);
202 assert(jresult);
203 auto j1(*jresult);
204
205 auto mresult = j1.try_as<matrix_type>();
206 assert(mresult);
207 auto& m3(*mresult);
208 assert(m1 == m3);
209
210 jsoncons::json j2{m1};
211 assert(j1 == j2);
212 ```
213 Output:
214 ```
215 (1)
216 -0.997497 0.617481 -0.299417 0.49321
217 0.127171 0.170019 0.791925 -0.651784
218 -0.613392 -0.0402539 0.64568 0.717887
219
220 (2)
221 [
222 [-0.9974974822229682, 0.6174810022278512, -0.2994170964690085, 0.4932096316415906],
223 [0.12717062898648024, 0.1700186162907804, 0.7919248023926511, -0.651783806878872],
224 [-0.6133915219580676, -0.04025391399884026, 0.6456801049836727, 0.717886898403882]
225 ]
226
227 (3) Expected 3 columns, found 4: Unable to convert into the provided type at line 1 and column 2
228 ```
229
230 #### Dynamic matrix example
231
232 ```cpp
233 using matrix_type = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>;
234
235 // Don't use auto here! (Random returns proxy)
236 matrix_type m1 = matrix_type::Random(3, 4);
237 std::cout << "(1) " << '\n' << m1 << "\n\n";
238
239 std::string buffer;
240 auto options = jsoncons::json_options{}.array_array_line_splits(jsoncons::line_split_kind::same_line);
241
242 auto wresult = jsoncons::try_encode_json_pretty(m1, buffer, options);
243 assert(wresult);
244
245 std::cout << "(2) " << '\n' << buffer << "\n\n";
246
247 auto rresult = jsoncons::try_decode_json<matrix_type>(buffer);
248 assert(rresult);
249 auto& m2(*rresult);
250
251 assert(m1 == m2);
252
253 auto jresult = jsoncons::try_decode_json<jsoncons::json>(buffer);
254 assert(jresult);
255 auto j1(*jresult);
256
257 auto mresult = j1.try_as<matrix_type>();
258 assert(mresult);
259 auto& m3(*mresult);
260 assert(m1 == m3);
261
262 jsoncons::json j2{m1};
263 assert(j1 == j2);
264 ```
265 Output:
266 ```
267 (1)
268 -0.997497 0.617481 -0.299417 0.49321
269 0.127171 0.170019 0.791925 -0.651784
270 -0.613392 -0.0402539 0.64568 0.717887
271
272 (2)
273 [
274 [-0.9974974822229682, 0.6174810022278512, -0.2994170964690085, 0.4932096316415906],
275 [0.12717062898648024, 0.1700186162907804, 0.7919248023926511, -0.651783806878872],
276 [-0.6133915219580676, -0.04025391399884026, 0.6456801049836727, 0.717886898403882]
277 ]
278 ```
279
280 ### See also
281
282 [allocator_set](../allocator_set.md)
283 [decode_json, try_decode_json](../decode_json.md)
284 [encode_json, encode_json_pretty, try_encode_json, try_encode_json_pretty](../encode_json.md)
285 [basic_json](../basic_json.md)
286