version 3.11-dev
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
json.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3//
4// SPDX-FileCopyrightText: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
13#include <dune/common/fvector.hh>
14#include <dune/common/exceptions.hh>
15
16#include <dumux/io/json/json.hpp>
17
18namespace Dumux::Json {
19using JsonTree = nlohmann::json;
20} // end namespace Dumux::Json
21
22namespace nlohmann {
23
24template <typename ctype, int k>
25struct adl_serializer<Dune::FieldVector<ctype, k>>
26{
27 static void to_json(json& j, const Dune::FieldVector<ctype, k>& fv)
28 {
29 j = json::array();
30 for (int i = 0; i < k; ++i) {
31 j.push_back(fv[i]);
32 }
33 }
34
35 static void from_json(const json& j, Dune::FieldVector<ctype, k>& fv)
36 {
37 if (!j.is_array())
38 DUNE_THROW(Dune::IOError, "json: Cannot convert to FieldVector, not an array");
39
40 if (j.size() != k)
41 DUNE_THROW(Dune::IOError, "json: Cannot convert to FieldVector of size " << k << " from array of size " << j.size());
42
43 for (int i = 0; i < k; ++i)
44 fv[i] = j[i].template get<ctype>();
45 }
46};
47
48} // end namespace nlohmann
Definition: json.hh:18
nlohmann::json JsonTree
Definition: json.hh:19
Definition: common/pdesolver.hh:24
Definition: json.hh:22
static void to_json(json &j, const Dune::FieldVector< ctype, k > &fv)
Definition: json.hh:27
static void from_json(const json &j, Dune::FieldVector< ctype, k > &fv)
Definition: json.hh:35