]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/json.cc
rec: Don't account chained queries more than once
[thirdparty/pdns.git] / pdns / json.cc
1 /*
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 */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include "json.hh"
26 #include "namespaces.hh"
27 #include "misc.hh"
28
29 using json11::Json;
30
31 int intFromJson(const Json container, const std::string& key)
32 {
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());
38 } else {
39 throw JsonException("Key '" + string(key) + "' not an Integer or not present");
40 }
41 }
42
43 int intFromJson(const Json container, const std::string& key, const int default_value)
44 {
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());
50 } else {
51 // TODO: check if value really isn't present
52 return default_value;
53 }
54 }
55
56 double doubleFromJson(const Json container, const std::string& key)
57 {
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());
63 } else {
64 throw JsonException("Key '" + string(key) + "' not an Integer or not present");
65 }
66 }
67
68 double doubleFromJson(const Json container, const std::string& key, const double default_value)
69 {
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());
75 } else {
76 // TODO: check if value really isn't present
77 return default_value;
78 }
79 }
80
81 string stringFromJson(const Json container, const std::string &key)
82 {
83 const Json val = container[key];
84 if (val.is_string()) {
85 return val.string_value();
86 } else {
87 throw JsonException("Key '" + string(key) + "' not present or not a String");
88 }
89 }
90
91 bool boolFromJson(const Json container, const std::string& key)
92 {
93 auto val = container[key];
94 if (val.is_bool()) {
95 return val.bool_value();
96 }
97 throw JsonException("Key '" + string(key) + "' not present or not a Bool");
98 }
99
100 bool boolFromJson(const Json container, const std::string& key, const bool default_value)
101 {
102 auto val = container[key];
103 if (val.is_bool()) {
104 return val.bool_value();
105 }
106 return default_value;
107 }