]>
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 int intFromJson(const Json container
, const std::string
& key
)
33 auto val
= container
[key
];
34 if (val
.is_number()) {
35 return val
.int_value();
36 } else if (val
.is_string()) {
37 return std::stoi(val
.string_value());
39 throw JsonException("Key '" + string(key
) + "' not an Integer or not present");
43 int intFromJson(const Json container
, const std::string
& key
, const int default_value
)
45 auto val
= container
[key
];
46 if (val
.is_number()) {
47 return val
.int_value();
48 } else if (val
.is_string()) {
50 return std::stoi(val
.string_value());
51 } catch (std::out_of_range
&) {
52 throw JsonException("Value for key '" + string(key
) + "' is out of range");
55 // TODO: check if value really isn't present
60 double doubleFromJson(const Json container
, const std::string
& key
)
62 auto val
= container
[key
];
63 if (val
.is_number()) {
64 return val
.number_value();
65 } else if (val
.is_string()) {
67 return std::stod(val
.string_value());
68 } catch (std::out_of_range
&) {
69 throw JsonException("Value for key '" + string(key
) + "' is out of range");
72 throw JsonException("Key '" + string(key
) + "' not an Integer or not present");
76 double doubleFromJson(const Json container
, const std::string
& key
, const double default_value
)
78 auto val
= container
[key
];
79 if (val
.is_number()) {
80 return val
.number_value();
81 } else if (val
.is_string()) {
82 return std::stod(val
.string_value());
84 // TODO: check if value really isn't present
89 string
stringFromJson(const Json container
, const std::string
&key
)
91 const Json val
= container
[key
];
92 if (val
.is_string()) {
93 return val
.string_value();
95 throw JsonException("Key '" + string(key
) + "' not present or not a String");
99 bool boolFromJson(const Json container
, const std::string
& key
)
101 auto val
= container
[key
];
103 return val
.bool_value();
105 throw JsonException("Key '" + string(key
) + "' not present or not a Bool");
108 bool boolFromJson(const Json container
, const std::string
& key
, const bool default_value
)
110 auto val
= container
[key
];
112 return val
.bool_value();
114 return default_value
;