]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/json.cc
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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.
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.
26 #include "namespaces.hh"
31 static inline int intFromJsonInternal(const Json
& container
, const std::string
& key
, const bool have_default
, const int default_value
)
33 const auto& val
= container
[key
];
34 if (val
.is_number()) {
35 return val
.int_value();
38 if (val
.is_string()) {
40 return std::stoi(val
.string_value());
41 } catch (std::out_of_range
&) {
42 throw JsonException("Key '" + string(key
) + "' is out of range");
49 throw JsonException("Key '" + string(key
) + "' not an Integer or not present");
52 int intFromJson(const Json
& container
, const std::string
& key
)
54 return intFromJsonInternal(container
, key
, false, 0);
57 int intFromJson(const Json
& container
, const std::string
& key
, const int default_value
)
59 return intFromJsonInternal(container
, key
, true, default_value
);
62 static inline unsigned int uintFromJsonInternal(const Json
& container
, const std::string
& key
, const bool have_default
, const unsigned int default_value
)
64 int intval
= intFromJsonInternal(container
, key
, have_default
, static_cast<int>(default_value
));
68 throw JsonException("Key '" + string(key
) + "' is not a positive Integer");
71 unsigned int uintFromJson(const Json
& container
, const std::string
& key
)
73 return uintFromJsonInternal(container
, key
, false, 0);
76 unsigned int uintFromJson(const Json
& container
, const std::string
& key
, const unsigned int default_value
)
78 return uintFromJsonInternal(container
, key
, true, default_value
);
81 static inline double doubleFromJsonInternal(const Json
& container
, const std::string
& key
, const bool have_default
, const double default_value
)
83 const auto& val
= container
[key
];
84 if (val
.is_number()) {
85 return val
.number_value();
88 if (val
.is_string()) {
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");
99 throw JsonException("Key '" + string(key
) + "' not an Integer or not present");
102 double doubleFromJson(const Json
& container
, const std::string
& key
)
104 return doubleFromJsonInternal(container
, key
, false, 0);
107 double doubleFromJson(const Json
& container
, const std::string
& key
, const double default_value
)
109 return doubleFromJsonInternal(container
, key
, true, default_value
);
112 string
stringFromJson(const Json
& container
, const std::string
&key
)
114 const auto& val
= container
[key
];
115 if (val
.is_string()) {
116 return val
.string_value();
118 throw JsonException("Key '" + string(key
) + "' not present or not a String");
121 static inline bool boolFromJsonInternal(const Json
& container
, const std::string
& key
, const bool have_default
, const bool default_value
)
123 const auto& val
= container
[key
];
125 return val
.bool_value();
128 return default_value
;
130 throw JsonException("Key '" + string(key
) + "' not present or not a Bool");
133 bool boolFromJson(const Json
& container
, const std::string
& key
)
135 return boolFromJsonInternal(container
, key
, false, false);
138 bool boolFromJson(const Json
& container
, const std::string
& key
, const bool default_value
)
140 return boolFromJsonInternal(container
, key
, true, default_value
);