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