]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/json.cc
API: add cryptokeys test
[thirdparty/pdns.git] / pdns / json.cc
CommitLineData
a426cb89
CH
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
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
75ff5b8b
BH
29#include "rapidjson/document.h"
30#include "rapidjson/stringbuffer.h"
31#include "rapidjson/writer.h"
283276d3 32
6ec5e728 33using namespace rapidjson;
283276d3 34
6ec5e728 35int intFromJson(const Value& container, const char* key)
283276d3 36{
2b7c1986
CH
37 if (!container.IsObject()) {
38 throw JsonException("Container was not an object.");
39 }
6ec5e728
CH
40 const Value& val = container[key];
41 if (val.IsInt()) {
42 return val.GetInt();
43 } else if (val.IsString()) {
335da0ba 44 return std::stoi(val.GetString());
6ec5e728
CH
45 } else {
46 throw JsonException("Key '" + string(key) + "' not an Integer or not present");
47 }
48}
283276d3 49
6ec5e728
CH
50int intFromJson(const Value& container, const char* key, const int default_value)
51{
2b7c1986
CH
52 if (!container.IsObject()) {
53 throw JsonException("Container was not an object.");
54 }
6ec5e728
CH
55 const Value& val = container[key];
56 if (val.IsInt()) {
57 return val.GetInt();
58 } else if (val.IsString()) {
335da0ba 59 return std::stoi(val.GetString());
6ec5e728
CH
60 } else {
61 // TODO: check if value really isn't present
62 return default_value;
63 }
283276d3
PD
64}
65
6ec5e728
CH
66string stringFromJson(const Value& container, const char* key)
67{
2b7c1986
CH
68 if (!container.IsObject()) {
69 throw JsonException("Container was not an object.");
70 }
6ec5e728
CH
71 const Value& val = container[key];
72 if (val.IsString()) {
73 return val.GetString();
74 } else {
75 throw JsonException("Key '" + string(key) + "' not present or not a String");
76 }
77}
a7a902fb 78
6ec5e728
CH
79string stringFromJson(const Value& container, const char* key, const string& default_value)
80{
2b7c1986
CH
81 if (!container.IsObject()) {
82 throw JsonException("Container was not an object.");
83 }
6ec5e728
CH
84 const Value& val = container[key];
85 if (val.IsString()) {
86 return val.GetString();
87 } else {
88 // TODO: check if value really isn't present
89 return default_value;
90 }
91}
a7a902fb 92
cea26350
CH
93bool boolFromJson(const rapidjson::Value& container, const char* key)
94{
2b7c1986
CH
95 if (!container.IsObject()) {
96 throw JsonException("Container was not an object.");
97 }
cea26350
CH
98 const Value& val = container[key];
99 if (val.IsBool()) {
100 return val.GetBool();
101 } else {
102 throw JsonException("Key '" + string(key) + "' not present or not a Bool");
103 }
104}
105
d1587ceb
CH
106bool boolFromJson(const rapidjson::Value& container, const char* key, const bool default_value)
107{
108 if (!container.IsObject()) {
109 throw JsonException("Container was not an object.");
110 }
111 const Value& val = container[key];
112 if (val.IsBool()) {
113 return val.GetBool();
114 } else {
115 return default_value;
116 }
117}
118
68821d55 119string makeStringFromDocument(const Document& doc)
8537b9f0
BH
120{
121 StringBuffer output;
122 Writer<StringBuffer> w(output);
123 doc.Accept(w);
124 return string(output.GetString(), output.Size());
125}
126
6ec5e728 127string returnJsonObject(const map<string, string>& items)
a7a902fb 128{
75ff5b8b
BH
129 Document doc;
130 doc.SetObject();
131 typedef map<string, string> items_t;
ef7cd021 132 for(const items_t::value_type& val : items) {
75ff5b8b 133 doc.AddMember(val.first.c_str(), val.second.c_str(), doc.GetAllocator());
a7a902fb 134 }
8537b9f0 135 return makeStringFromDocument(doc);
a7a902fb 136}
ce403b3d 137
6ec5e728 138string returnJsonError(const string& error)
73301d73
CH
139{
140 Document doc;
141 doc.SetObject();
142 Value jerror(error.c_str(), doc.GetAllocator()); // copy
143 doc.AddMember("error", jerror, doc.GetAllocator());
144 return makeStringFromDocument(doc);
145}
a426cb89
CH
146
147/* success response */
148string returnJsonMessage(const string& message)
149{
150 Document doc;
151 doc.SetObject();
152 Value jmessage;
153 jmessage.SetString(message.c_str());
154 doc.AddMember("result", jmessage, doc.GetAllocator());
155 return makeStringFromDocument(doc);
156}