]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/json.cc
rec: ensure correct service user on debian
[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 try {
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");
53 }
54 } else {
55 // TODO: check if value really isn't present
56 return default_value;
57 }
58 }
59
60 double doubleFromJson(const Json container, const std::string& key)
61 {
62 auto val = container[key];
63 if (val.is_number()) {
64 return val.number_value();
65 } else if (val.is_string()) {
66 try {
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");
70 }
71 } else {
72 throw JsonException("Key '" + string(key) + "' not an Integer or not present");
73 }
74 }
75
76 double doubleFromJson(const Json container, const std::string& key, const double default_value)
77 {
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());
83 } else {
84 // TODO: check if value really isn't present
85 return default_value;
86 }
87 }
88
89 string stringFromJson(const Json container, const std::string &key)
90 {
91 const Json val = container[key];
92 if (val.is_string()) {
93 return val.string_value();
94 } else {
95 throw JsonException("Key '" + string(key) + "' not present or not a String");
96 }
97 }
98
99 bool boolFromJson(const Json container, const std::string& key)
100 {
101 auto val = container[key];
102 if (val.is_bool()) {
103 return val.bool_value();
104 }
105 throw JsonException("Key '" + string(key) + "' not present or not a Bool");
106 }
107
108 bool boolFromJson(const Json container, const std::string& key, const bool default_value)
109 {
110 auto val = container[key];
111 if (val.is_bool()) {
112 return val.bool_value();
113 }
114 return default_value;
115 }