|
| 1 | +/*************************************************************************** |
| 2 | +* Copyright (c) 2016, Johan Mabille, Sylvain Corlay and Wolf Vollprecht * |
| 3 | +* * |
| 4 | +* Distributed under the terms of the BSD 3-Clause License. * |
| 5 | +* * |
| 6 | +* The full license is in the file LICENSE, distributed with this software. * |
| 7 | +****************************************************************************/ |
| 8 | + |
| 9 | +#ifndef XTENSOR_JSON_HPP |
| 10 | +#define XTENSOR_JSON_HPP |
| 11 | + |
| 12 | +#include <cstddef> |
| 13 | +#include <stdexcept> |
| 14 | +#include <utility> |
| 15 | + |
| 16 | +#include <nlohmann/json.hpp> |
| 17 | + |
| 18 | +#include "xstrided_view.hpp" |
| 19 | + |
| 20 | +namespace xt |
| 21 | +{ |
| 22 | + /************************************* |
| 23 | + * to_json and from_json declaration * |
| 24 | + *************************************/ |
| 25 | + |
| 26 | + template <class E> |
| 27 | + enable_xexpression<E> to_json(nlohmann::json&, const E&); |
| 28 | + |
| 29 | + template <class E> |
| 30 | + enable_xcontainer<E> from_json(const nlohmann::json&, E&); |
| 31 | + |
| 32 | + template <class E> |
| 33 | + enable_xview<E> from_json(const nlohmann::json&, E&); |
| 34 | + |
| 35 | + /**************************************** |
| 36 | + * to_json and from_json implementation * |
| 37 | + ****************************************/ |
| 38 | + |
| 39 | + namespace detail |
| 40 | + { |
| 41 | + template <class D> |
| 42 | + void to_json_impl(nlohmann::json& j, const xexpression<D>& e, slice_vector& slices) |
| 43 | + { |
| 44 | + const auto view = dynamic_view(e.derived_cast(), slices); |
| 45 | + if (view.dimension() == 0) |
| 46 | + { |
| 47 | + j = view(); |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + j = nlohmann::json::array(); |
| 52 | + using size_type = typename D::size_type; |
| 53 | + size_type nrows = view.shape()[0]; |
| 54 | + for (size_type i = 0; i != nrows; ++i) |
| 55 | + { |
| 56 | + slices.push_back(i); |
| 57 | + nlohmann::json k; |
| 58 | + to_json_impl(k, e, slices); |
| 59 | + j.push_back(std::move(k)); |
| 60 | + slices.pop_back(); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + template <class D> |
| 66 | + inline void from_json_impl(const nlohmann::json& j, xexpression<D>& e, slice_vector& slices) |
| 67 | + { |
| 68 | + auto view = dynamic_view(e.derived_cast(), slices); |
| 69 | + |
| 70 | + if (view.dimension() == 0) |
| 71 | + { |
| 72 | + view() = j; |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + using size_type = typename D::size_type; |
| 77 | + size_type nrows = view.shape()[0]; |
| 78 | + for (auto i = 0; i != nrows; ++i) |
| 79 | + { |
| 80 | + slices.push_back(i); |
| 81 | + const nlohmann::json& k = j[i]; |
| 82 | + from_json_impl(k, e, slices); |
| 83 | + slices.pop_back(); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + inline unsigned int json_dimension(const nlohmann::json& j) |
| 89 | + { |
| 90 | + if (j.is_array() && j.size()) |
| 91 | + { |
| 92 | + return 1 + json_dimension(j[0]); |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + return 0; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + template <class S> |
| 101 | + inline void json_shape(const nlohmann::json& j, S& s, std::size_t pos = 0) |
| 102 | + { |
| 103 | + if (j.is_array()) |
| 104 | + { |
| 105 | + auto size = j.size(); |
| 106 | + s[pos] = size; |
| 107 | + if (size) |
| 108 | + { |
| 109 | + json_shape(j[0], s, pos + 1); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @brief JSON serialization of an xtensor expression. |
| 117 | + * |
| 118 | + * The to_json method is used by the nlohmann_json package for automatic |
| 119 | + * serialization of user-defined types. The method is picked up by |
| 120 | + * argument-dependent lookup. |
| 121 | + * |
| 122 | + * @param j a JSON object |
| 123 | + * @param e a const \ref xexpression |
| 124 | + */ |
| 125 | + template <class E> |
| 126 | + inline enable_xexpression<E> to_json(nlohmann::json& j, const E& e) |
| 127 | + { |
| 128 | + auto sv = slice_vector(); |
| 129 | + detail::to_json_impl(j, e, sv); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * @brief JSON deserialization of a xtensor expression with a container |
| 134 | + * semantics. |
| 135 | + * |
| 136 | + * The from_json method is used by the nlohmann_json library for automatic |
| 137 | + * serialization of user-defined types. The method is picked up by |
| 138 | + * argument-dependent lookup. |
| 139 | + * |
| 140 | + * @param j a const JSON object |
| 141 | + * @param e an \ref xexpression |
| 142 | + */ |
| 143 | + template <class E> |
| 144 | + inline enable_xcontainer<E> from_json(const nlohmann::json& j, E& e) |
| 145 | + { |
| 146 | + auto dimension = detail::json_dimension(j); |
| 147 | + auto s = xtl::make_sequence<typename E::shape_type>(dimension); |
| 148 | + detail::json_shape(j, s); |
| 149 | + |
| 150 | + // In the case of a container, we resize the container. |
| 151 | + e.resize(s); |
| 152 | + |
| 153 | + auto sv = slice_vector(); |
| 154 | + detail::from_json_impl(j, e, sv); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * @brief JSON deserialization of a xtensor expression with a view |
| 159 | + * semantics. |
| 160 | + * |
| 161 | + * The from_json method is used by the nlohmann_json library for automatic |
| 162 | + * serialization of user-defined types. The method is picked up by |
| 163 | + * argument-dependent lookup. |
| 164 | + * |
| 165 | + * However, for converting a JSON object to a value, nlohmann_json requires |
| 166 | + * the value type to be default constructible, which is typically not the |
| 167 | + * case for expressions with a view semantics. |
| 168 | + * |
| 169 | + * For this usecase, one can call from_json directly. |
| 170 | + * |
| 171 | + * @param j a const JSON object |
| 172 | + * @param e an \ref xexpression |
| 173 | + */ |
| 174 | + template <class E> |
| 175 | + inline enable_xview<E> from_json(const nlohmann::json& j, E& e) |
| 176 | + { |
| 177 | + typename E::shape_type s; |
| 178 | + detail::json_shape(j, s); |
| 179 | + |
| 180 | + // In the case of a view, we check the size of the container. |
| 181 | + if (!std::equal(s.cbegin(), s.cend(), e.shape().cbegin())) |
| 182 | + { |
| 183 | + throw std::runtime_error("Shape mismatch when deserializing JSON to view"); |
| 184 | + } |
| 185 | + |
| 186 | + auto sv = slice_vector(); |
| 187 | + detail::from_json_impl(j, e, sv); |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +#endif |
0 commit comments