]>
Commit | Line | Data |
---|---|---|
a426cb89 | 1 | /* |
12471842 PL |
2 | * This file is part of PowerDNS or dnsdist. |
3 | * Copyright -- PowerDNS.COM B.V. and its contributors | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of version 2 of the GNU General Public License as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * In addition, for the avoidance of any doubt, permission is granted to | |
10 | * link this program with OpenSSL and to (re)distribute the binaries | |
11 | * produced as the result of such linking. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
21 | */ | |
870a0fe4 AT |
22 | #ifdef HAVE_CONFIG_H |
23 | #include "config.h" | |
24 | #endif | |
a7a902fb BH |
25 | #include "json.hh" |
26 | #include "namespaces.hh" | |
ce403b3d | 27 | #include "misc.hh" |
fa8fd4d2 | 28 | |
5938c49f | 29 | using json11::Json; |
283276d3 | 30 | |
cf17e6b8 | 31 | static inline int intFromJsonInternal(const Json& container, const std::string& key, const bool have_default, const int default_value) |
5938c49f | 32 | { |
224085cc | 33 | const auto& val = container[key]; |
5938c49f CH |
34 | if (val.is_number()) { |
35 | return val.int_value(); | |
224085cc RP |
36 | } |
37 | ||
38 | if (val.is_string()) { | |
cf17e6b8 CH |
39 | try { |
40 | return std::stoi(val.string_value()); | |
41 | } catch (std::out_of_range&) { | |
42 | throw JsonException("Key '" + string(key) + "' is out of range"); | |
43 | } | |
5938c49f | 44 | } |
224085cc RP |
45 | |
46 | if (have_default) { | |
47 | return default_value; | |
48 | } | |
49 | throw JsonException("Key '" + string(key) + "' not an Integer or not present"); | |
5938c49f CH |
50 | } |
51 | ||
cf17e6b8 CH |
52 | int intFromJson(const Json& container, const std::string& key) |
53 | { | |
54 | return intFromJsonInternal(container, key, false, 0); | |
55 | } | |
56 | ||
4de01aaa | 57 | int intFromJson(const Json& container, const std::string& key, const int default_value) |
5938c49f | 58 | { |
cf17e6b8 CH |
59 | return intFromJsonInternal(container, key, true, default_value); |
60 | } | |
61 | ||
62 | static inline unsigned int uintFromJsonInternal(const Json& container, const std::string& key, const bool have_default, const unsigned int default_value) | |
63 | { | |
64 | int intval = intFromJsonInternal(container, key, have_default, static_cast<int>(default_value)); | |
65 | if (intval >= 0) { | |
66 | return intval; | |
5938c49f | 67 | } |
cf17e6b8 | 68 | throw JsonException("Key '" + string(key) + "' is not a positive Integer"); |
5938c49f CH |
69 | } |
70 | ||
cf17e6b8 CH |
71 | unsigned int uintFromJson(const Json& container, const std::string& key) |
72 | { | |
73 | return uintFromJsonInternal(container, key, false, 0); | |
74 | } | |
75 | ||
76 | unsigned int uintFromJson(const Json& container, const std::string& key, const unsigned int default_value) | |
77 | { | |
78 | return uintFromJsonInternal(container, key, true, default_value); | |
79 | } | |
80 | ||
81 | static inline double doubleFromJsonInternal(const Json& container, const std::string& key, const bool have_default, const double default_value) | |
81399d19 | 82 | { |
224085cc | 83 | const auto& val = container[key]; |
81399d19 AT |
84 | if (val.is_number()) { |
85 | return val.number_value(); | |
224085cc RP |
86 | } |
87 | ||
88 | if (val.is_string()) { | |
1148587f CH |
89 | try { |
90 | return std::stod(val.string_value()); | |
91 | } catch (std::out_of_range&) { | |
92 | throw JsonException("Value for key '" + string(key) + "' is out of range"); | |
93 | } | |
81399d19 | 94 | } |
224085cc RP |
95 | |
96 | if (have_default) { | |
97 | return default_value; | |
98 | } | |
99 | throw JsonException("Key '" + string(key) + "' not an Integer or not present"); | |
81399d19 AT |
100 | } |
101 | ||
cf17e6b8 CH |
102 | double doubleFromJson(const Json& container, const std::string& key) |
103 | { | |
104 | return doubleFromJsonInternal(container, key, false, 0); | |
105 | } | |
106 | ||
4de01aaa | 107 | double doubleFromJson(const Json& container, const std::string& key, const double default_value) |
81399d19 | 108 | { |
cf17e6b8 | 109 | return doubleFromJsonInternal(container, key, true, default_value); |
81399d19 AT |
110 | } |
111 | ||
4de01aaa | 112 | string stringFromJson(const Json& container, const std::string &key) |
5938c49f | 113 | { |
224085cc | 114 | const auto& val = container[key]; |
5938c49f CH |
115 | if (val.is_string()) { |
116 | return val.string_value(); | |
5938c49f | 117 | } |
224085cc | 118 | throw JsonException("Key '" + string(key) + "' not present or not a String"); |
5938c49f CH |
119 | } |
120 | ||
cf17e6b8 | 121 | static inline bool boolFromJsonInternal(const Json& container, const std::string& key, const bool have_default, const bool default_value) |
5938c49f | 122 | { |
224085cc | 123 | const auto& val = container[key]; |
5938c49f CH |
124 | if (val.is_bool()) { |
125 | return val.bool_value(); | |
5938c49f | 126 | } |
cf17e6b8 CH |
127 | if (have_default) { |
128 | return default_value; | |
129 | } | |
9895aa4b | 130 | throw JsonException("Key '" + string(key) + "' not present or not a Bool"); |
5938c49f CH |
131 | } |
132 | ||
cf17e6b8 CH |
133 | bool boolFromJson(const Json& container, const std::string& key) |
134 | { | |
135 | return boolFromJsonInternal(container, key, false, false); | |
136 | } | |
137 | ||
4de01aaa | 138 | bool boolFromJson(const Json& container, const std::string& key, const bool default_value) |
5938c49f | 139 | { |
cf17e6b8 | 140 | return boolFromJsonInternal(container, key, true, default_value); |
d1587ceb | 141 | } |