response can return json object

This commit is contained in:
ipkn 2014-04-17 10:06:41 -04:00
parent d908a2657e
commit 86433135f3
4 changed files with 36 additions and 7 deletions

View File

@ -1,4 +1,5 @@
#include "flask.h"
#include "json.h"
#include <sstream>
@ -14,7 +15,9 @@ int main()
FLASK_ROUTE(app, "/json")
([]{
return "{\"message\":\"Hello, World!\"}";
flask::json::wvalue x;
x["message"] = "Hello, World!";
return x;
});
FLASK_ROUTE(app, "/about")

View File

@ -60,6 +60,11 @@ namespace flask
buffers_.clear();
buffers_.reserve(4*(res.headers.size()+4)+3);
if (res.body.empty() && res.json_value.t == json::type::Object)
{
res.body = json::encode(res.json_value);
}
if (!statusCodes.count(res.code))
res.code = 500;
{

View File

@ -1,17 +1,33 @@
#pragma once
#include <string>
#include <unordered_map>
#include "json.h"
namespace flask
{
struct response
{
std::string body;
json::wvalue json_value;
int code{200};
std::unordered_map<std::string, std::string> headers;
response() {}
explicit response(int code) : code(code) {}
response(std::string body) : body(std::move(body)) {}
response(json::wvalue&& json_value) : json_value(std::move(json_value)) {}
response(const json::wvalue& json_value) : body(json::encode(json_value)) {}
response(int code, std::string body) : body(std::move(body)), code(code) {}
response(response&& r)
{
*this = std::move(r);
}
response& operator = (response&& r)
{
body = std::move(r.body);
json_value = std::move(r.json_value);
code = r.code;
headers = std::move(r.headers);
return *this;
}
};
}

17
json.h
View File

@ -220,13 +220,18 @@ namespace flask
wvalue() {}
wvalue(wvalue&& r)
:
t(r.t),
d(r.d),
s{std::move(r.s)},
l{std::move(r.l)},
o{std::move(r.o)}
{
*this = std::move(r);
}
wvalue& operator = (wvalue&& r)
{
t = r.t;
d = r.d;
s = std::move(r.s);
l = std::move(r.l);
o = std::move(r.o);
return *this;
}
void clear()