]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/json.cc
Merge pull request #3161 from rgacogne/openssl-rsa
[thirdparty/pdns.git] / pdns / json.cc
1 /*
2 Copyright (C) 2002 - 2015 PowerDNS.COM BV
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License version 2
6 as published by the Free Software Foundation
7
8 Additionally, the license of this program contains a special
9 exception which allows to distribute the program in binary form when
10 it is linked against OpenSSL.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
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 string stringFromJson(const Json container, const std::string &key)
57 {
58 const Json val = container[key];
59 if (val.is_string()) {
60 return val.string_value();
61 } else {
62 throw JsonException("Key '" + string(key) + "' not present or not a String");
63 }
64 }
65
66 bool boolFromJson(const Json container, const std::string& key)
67 {
68 auto val = container[key];
69 if (val.is_bool()) {
70 return val.bool_value();
71 } else {
72 throw JsonException("Key '" + string(key) + "' not present or not a Bool");
73 }
74 }
75
76 bool boolFromJson(const Json container, const std::string& key, const bool default_value)
77 {
78 auto val = container[key];
79 if (val.is_bool()) {
80 return val.bool_value();
81 } else {
82 return default_value;
83 }
84 }