]>
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()) {
49 return std::stoi(val
.string_value());
51 // TODO: check if value really isn't present
56 double doubleFromJson(const Json container
, const std::string
& key
)
58 auto val
= container
[key
];
59 if (val
.is_number()) {
60 return val
.number_value();
61 } else if (val
.is_string()) {
62 return std::stod(val
.string_value());
64 throw JsonException("Key '" + string(key
) + "' not an Integer or not present");
68 double doubleFromJson(const Json container
, const std::string
& key
, const double default_value
)
70 auto val
= container
[key
];
71 if (val
.is_number()) {
72 return val
.number_value();
73 } else if (val
.is_string()) {
74 return std::stod(val
.string_value());
76 // TODO: check if value really isn't present
81 string
stringFromJson(const Json container
, const std::string
&key
)
83 const Json val
= container
[key
];
84 if (val
.is_string()) {
85 return val
.string_value();
87 throw JsonException("Key '" + string(key
) + "' not present or not a String");
91 bool boolFromJson(const Json container
, const std::string
& key
)
93 auto val
= container
[key
];
95 return val
.bool_value();
97 throw JsonException("Key '" + string(key
) + "' not present or not a Bool");
100 bool boolFromJson(const Json container
, const std::string
& key
, const bool default_value
)
102 auto val
= container
[key
];
104 return val
.bool_value();
106 return default_value
;