summaryrefslogtreecommitdiff
path: root/src/json.h
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2011-03-14 00:57:05 +0900
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2011-03-14 00:57:05 +0900
commitbf01bb84b6b96fe2cba529f588e29a9038c0daf6 (patch)
tree929c8ad8e4ec3eebdae8ffd046f7990b9f6dccae /src/json.h
parentfeb4e2e53a368baa64dffd5e2182204548fe86d5 (diff)
Added JSONP support. Callback query parameter is "jsoncallback".
Diffstat (limited to 'src/json.h')
-rw-r--r--src/json.h62
1 files changed, 41 insertions, 21 deletions
diff --git a/src/json.h b/src/json.h
index 186f3a4c..97d00e94 100644
--- a/src/json.h
+++ b/src/json.h
@@ -58,9 +58,9 @@ OutputStream& encode(OutputStream& out, const ValueBase* vlb)
{
const std::string& s = string.s();
std::string t = jsonEscape(s);
- out_ << '"';
+ out_ << "\"";
out_.write(t.data(), t.size());
- out_ << '"';
+ out_ << "\"";
}
virtual void visit(const Integer& integer)
@@ -80,40 +80,40 @@ OutputStream& encode(OutputStream& out, const ValueBase* vlb)
virtual void visit(const List& list)
{
- out_ << '[';
+ out_ << "[";
List::ValueType::const_iterator i = list.begin();
if(!list.empty()) {
(*i)->accept(*this);
+ ++i;
+ for(List::ValueType::const_iterator eoi = list.end(); i != eoi; ++i){
+ out_ << ",";
+ (*i)->accept(*this);
+ }
}
- ++i;
- for(List::ValueType::const_iterator eoi = list.end(); i != eoi; ++i){
- out_ << ',';
- (*i)->accept(*this);
- }
- out_ << ']';
+ out_ << "]";
}
virtual void visit(const Dict& dict)
{
- out_ << '{';
+ out_ << "{";
Dict::ValueType::const_iterator i = dict.begin();
if(!dict.empty()) {
std::string key = jsonEscape((*i).first);
- out_ << '"';
- out_.write(key.data(), key.size());
- out_ << "\":";
- (*i).second->accept(*this);
- }
- ++i;
- for(Dict::ValueType::const_iterator eoi = dict.end(); i != eoi; ++i){
- out_ << ',';
- std::string key = jsonEscape((*i).first);
- out_ << '"';
+ out_ << "\"";
out_.write(key.data(), key.size());
out_ << "\":";
(*i).second->accept(*this);
+ ++i;
+ for(Dict::ValueType::const_iterator eoi = dict.end(); i != eoi; ++i){
+ out_ << ",";
+ std::string key = jsonEscape((*i).first);
+ out_ << "\"";
+ out_.write(key.data(), key.size());
+ out_ << "\":";
+ (*i).second->accept(*this);
+ }
}
- out_ << '}';
+ out_ << "}";
}
private:
OutputStream& out_;
@@ -133,6 +133,26 @@ OutputStream& encode(OutputStream& out, const SharedHandle<ValueBase>& vlb)
std::string encode(const ValueBase* json);
std::string encode(const SharedHandle<ValueBase>& json);
+struct JsonGetParam {
+ std::string request;
+ std::string callback;
+ JsonGetParam(const std::string& request, const std::string& callback);
+};
+
+// Decodes JSON-RPC request from GET query parameter query. query must
+// starts with "?". This function identifies method name, id,
+// parameters and jsonp callback. For method name, it searches
+// "method" query parameter. For id, it searches "id" query
+// parameter. The id is always treated as string. For parameters, it
+// searches "params" query parameter. The params is Base64 encoded
+// JSON string normally associated "params" key in POST request. For
+// jsonp callback, it searches "jsoncallback". For example, calling
+// remote method, sum([1,2,3]) with id=300 looks like this:
+// ?method=sum&id=300&params=WzEsMiwzXQ%3D%3D
+//
+// If both method and id are missing, params is treated as batch call.
+JsonGetParam decodeGetParams(const std::string& query);
+
} // namespace json
} // namespace aria2