]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/json.cc
Merge pull request #8223 from PowerDNS/omoerbeek-patch-1
[thirdparty/pdns.git] / pdns / json.cc
CommitLineData
a426cb89 1/*
12471842
PL
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 */
870a0fe4
AT
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
a7a902fb
BH
25#include "json.hh"
26#include "namespaces.hh"
ce403b3d 27#include "misc.hh"
fa8fd4d2 28
5938c49f 29using json11::Json;
283276d3 30
5938c49f
CH
31int 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
43int 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()) {
1148587f
CH
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 }
5938c49f
CH
54 } else {
55 // TODO: check if value really isn't present
56 return default_value;
57 }
58}
59
81399d19
AT
60double 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()) {
1148587f
CH
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 }
81399d19
AT
71 } else {
72 throw JsonException("Key '" + string(key) + "' not an Integer or not present");
73 }
74}
75
76double 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
5938c49f
CH
89string 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
f6cafa5b 99bool boolFromJson(const Json container, const std::string& key)
5938c49f
CH
100{
101 auto val = container[key];
102 if (val.is_bool()) {
103 return val.bool_value();
5938c49f 104 }
9895aa4b 105 throw JsonException("Key '" + string(key) + "' not present or not a Bool");
5938c49f
CH
106}
107
f6cafa5b 108bool boolFromJson(const Json container, const std::string& key, const bool default_value)
5938c49f
CH
109{
110 auto val = container[key];
111 if (val.is_bool()) {
112 return val.bool_value();
d1587ceb 113 }
9895aa4b 114 return default_value;
d1587ceb 115}