]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/json.cc
2 Copyright (C) 2002 - 2015 PowerDNS.COM BV
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
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.
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.
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
23 #include "namespaces.hh"
25 #include <boost/foreach.hpp>
26 #include "rapidjson/document.h"
27 #include "rapidjson/stringbuffer.h"
28 #include "rapidjson/writer.h"
30 using namespace rapidjson
;
32 int intFromJson(const Value
& container
, const char* key
)
34 if (!container
.IsObject()) {
35 throw JsonException("Container was not an object.");
37 const Value
& val
= container
[key
];
40 } else if (val
.IsString()) {
41 return atoi(val
.GetString());
43 throw JsonException("Key '" + string(key
) + "' not an Integer or not present");
47 int intFromJson(const Value
& container
, const char* key
, const int default_value
)
49 if (!container
.IsObject()) {
50 throw JsonException("Container was not an object.");
52 const Value
& val
= container
[key
];
55 } else if (val
.IsString()) {
56 return atoi(val
.GetString());
58 // TODO: check if value really isn't present
63 string
stringFromJson(const Value
& container
, const char* key
)
65 if (!container
.IsObject()) {
66 throw JsonException("Container was not an object.");
68 const Value
& val
= container
[key
];
70 return val
.GetString();
72 throw JsonException("Key '" + string(key
) + "' not present or not a String");
76 string
stringFromJson(const Value
& container
, const char* key
, const string
& default_value
)
78 if (!container
.IsObject()) {
79 throw JsonException("Container was not an object.");
81 const Value
& val
= container
[key
];
83 return val
.GetString();
85 // TODO: check if value really isn't present
90 bool boolFromJson(const rapidjson::Value
& container
, const char* key
)
92 if (!container
.IsObject()) {
93 throw JsonException("Container was not an object.");
95 const Value
& val
= container
[key
];
99 throw JsonException("Key '" + string(key
) + "' not present or not a Bool");
103 bool boolFromJson(const rapidjson::Value
& container
, const char* key
, const bool default_value
)
105 if (!container
.IsObject()) {
106 throw JsonException("Container was not an object.");
108 const Value
& val
= container
[key
];
110 return val
.GetBool();
112 return default_value
;
116 string
makeStringFromDocument(const Document
& doc
)
119 Writer
<StringBuffer
> w(output
);
121 return string(output
.GetString(), output
.Size());
124 string
returnJsonObject(const map
<string
, string
>& items
)
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());
132 return makeStringFromDocument(doc
);
135 string
returnJsonError(const string
& error
)
139 Value
jerror(error
.c_str(), doc
.GetAllocator()); // copy
140 doc
.AddMember("error", jerror
, doc
.GetAllocator());
141 return makeStringFromDocument(doc
);
144 /* success response */
145 string
returnJsonMessage(const string
& message
)
150 jmessage
.SetString(message
.c_str());
151 doc
.AddMember("result", jmessage
, doc
.GetAllocator());
152 return makeStringFromDocument(doc
);