Ginkgo
Generated from branch based on main. Ginkgo version 1.11.0
A numerical linear algebra library targeting many-core architectures
Toggle main menu visibility
Loading...
Searching...
No Matches
json_config.hpp
1
// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2
//
3
// SPDX-License-Identifier: BSD-3-Clause
4
5
#ifndef GKO_PUBLIC_EXTENSIONS_CONFIG_JSON_CONFIG_HPP_
6
#define GKO_PUBLIC_EXTENSIONS_CONFIG_JSON_CONFIG_HPP_
7
8
9
#include <fstream>
10
#include <stdexcept>
11
#include <string>
12
13
#include <nlohmann/json.hpp>
14
15
#include <ginkgo/core/config/property_tree.hpp>
16
17
18
namespace
gko
{
19
namespace
ext {
20
namespace
config {
21
22
27
inline
gko::config::pnode parse_json(
const
nlohmann::json& input)
28
{
29
const
auto
& dom = input;
30
31
auto
parse_array = [](
const
auto
& arr) {
32
gko::config::pnode::array_type nodes;
33
for
(
auto
it : arr) {
34
nodes.emplace_back(parse_json(it));
35
}
36
return
gko::config::pnode{nodes};
37
};
38
auto
parse_map = [](
const
auto
& map) {
39
gko::config::pnode::map_type nodes;
40
for
(
auto
& el : map.items()) {
41
nodes.emplace(el.key(), parse_json(el.value()));
42
}
43
return
gko::config::pnode{nodes};
44
};
45
auto
parse_data = [](
const
auto
& data) {
46
if
(data.is_number_integer()) {
47
return
gko::config::pnode{data.template get<std::int64_t>()};
48
}
49
if
(data.is_boolean()) {
50
return
gko::config::pnode{data.template get<bool>()};
51
}
52
if
(data.is_number_float()) {
53
return
gko::config::pnode{data.template get<double>()};
54
}
55
if
(data.is_string()) {
56
return
gko::config::pnode{
57
std::string(data.template get<std::string>())};
58
}
59
throw
std::runtime_error(
60
"property_tree can not handle the node with content: "
+
61
data.dump());
62
};
63
64
if
(dom.is_array()) {
65
return
parse_array(dom);
66
}
67
if
(dom.is_object()) {
68
return
parse_map(dom);
69
}
70
return
parse_data(dom);
71
}
72
73
77
inline
gko::config::pnode parse_json_file(std::string filename)
78
{
79
std::ifstream fstream(filename);
80
return
parse_json(nlohmann::json::parse(fstream));
81
}
82
86
inline
gko::config::pnode parse_json_string(std::string json)
87
{
88
return
parse_json(nlohmann::json::parse(json));
89
}
90
91
}
// namespace config
92
}
// namespace ext
93
}
// namespace gko
94
95
96
#endif
// GKO_PUBLIC_EXTENSIONS_CONFIG_JSON_CONFIG_HPP_
gko
The Ginkgo namespace.
Definition
abstract_factory.hpp:20
ginkgo
extensions
config
json_config.hpp
Generated by
1.18.0