10#ifndef XTENSOR_JSON_HPP 
   11#define XTENSOR_JSON_HPP 
   17#include <nlohmann/json.hpp> 
   19#include "../core/xtensor_config.hpp" 
   20#include "../views/xstrided_view.hpp" 
   28    template <
template <
typename U, 
typename V, 
typename... Args> 
class M, class E>
 
   29    enable_xexpression<E> 
to_json(nlohmann::basic_json<M>&, const E&);
 
   31    template <template <typename U, typename V, typename... Args> class M, class E>
 
   32    enable_xcontainer_semantics<E> 
from_json(const nlohmann::basic_json<M>&, E&);
 
   35    template <template <typename U, typename V, typename... Args> class M, class E>
 
   36    enable_xview_semantics<E> 
from_json(const nlohmann::basic_json<M>&, E&);
 
   46        template <
template <
typename U, 
typename V, 
typename... Args> 
class M, class D>
 
   50            if (
view.dimension() == 0)
 
   56                j = nlohmann::basic_json<M>::array();
 
   57                using size_type = 
typename D::size_type;
 
   58                size_type nrows = 
view.shape()[0];
 
   59                for (size_type i = 0; i != nrows; ++i)
 
   61                    slices.push_back(
static_cast<std::ptrdiff_t
>(i));
 
   62                    nlohmann::basic_json<M> k;
 
   63                    to_json_impl(k, e, slices);
 
   64                    j.push_back(std::move(k));
 
   70        template <
template <
typename U, 
typename V, 
typename... Args> 
class M, class D>
 
   76            if (
view.dimension() == 0)
 
   78                view() = j.template get<std::remove_reference_t<
decltype(
view())>>();
 
   82                using size_type = 
typename D::size_type;
 
   83                size_type nrows = 
view.shape()[0];
 
   84                for (size_type i = 0; i != nrows; ++i)
 
   86                    slices.push_back(
static_cast<std::ptrdiff_t
>(i));
 
   87                    const nlohmann::basic_json<M>& k = j[i];
 
   88                    from_json_impl(k, e, slices);
 
   94        template <
template <
typename U, 
typename V, 
typename... Args> 
class M>
 
   95        inline unsigned int json_dimension(const nlohmann::basic_json<M>& j)
 
   97            if (j.is_array() && j.size())
 
   99                return 1 + json_dimension(j[0]);
 
  107        template <
template <
typename U, 
typename V, 
typename... Args> 
class M, class S>
 
  108        inline void json_shape(const nlohmann::basic_json<M>& j, S& s, std::size_t pos = 0)
 
  112                auto size = j.size();
 
  116                    json_shape(j[0], s, pos + 1);
 
  132    template <
template <
typename U, 
typename V, 
typename... Args> 
class M, class E>
 
  133    inline enable_xexpression<E> 
to_json(nlohmann::basic_json<M>& j, const E& e)
 
  136        detail::to_json_impl(j, e, sv);
 
 
  155    template <
template <
typename U, 
typename V, 
typename... Args> 
class M, class E>
 
  156    inline enable_xcontainer_semantics<E> 
from_json(const nlohmann::basic_json<M>& j, E& e)
 
  158        auto dimension = detail::json_dimension(j);
 
  159        auto s = xtl::make_sequence<typename E::shape_type>(dimension);
 
  160        detail::json_shape(j, s);
 
  166        detail::from_json_impl(j, e, sv);
 
 
  170    template <
template <
typename U, 
typename V, 
typename... Args> 
class M, class E>
 
  171    inline enable_xview_semantics<E> from_json(const nlohmann::basic_json<M>& j, E& e)
 
  173        typename E::shape_type s;
 
  174        detail::json_shape(j, s);
 
  177        if (!std::equal(s.cbegin(), s.cend(), e.shape().cbegin()))
 
  179            XTENSOR_THROW(std::runtime_error, 
"Shape mismatch when deserializing JSON to view");
 
  183        detail::from_json_impl(j, e, sv);
 
standard mathematical functions for xexpressions
std::vector< xstrided_slice< std::ptrdiff_t > > xstrided_slice_vector
vector of slices used to build a xstrided_view
enable_xcontainer_semantics< E > from_json(const nlohmann::basic_json< M > &, E &)
JSON deserialization of a xtensor expression with a container or a view semantics.
enable_xexpression< E > to_json(nlohmann::basic_json< M > &, const E &)
JSON serialization of an xtensor expression.
auto strided_view(E &&e, S &&shape, X &&stride, std::size_t offset=0, layout_type layout=L) noexcept
Construct a strided view from an xexpression, shape, strides and offset.
auto view(E &&e, S &&... slices)
Constructs and returns a view on the specified xexpression.