diff options
-rw-r--r-- | example.cpp | 5 | ||||
-rw-r--r-- | http_connection.h | 5 | ||||
-rw-r--r-- | http_response.h | 16 | ||||
-rw-r--r-- | json.h | 17 |
4 files changed, 36 insertions, 7 deletions
diff --git a/example.cpp b/example.cpp index 54efccf..16116a8 100644 --- a/example.cpp +++ b/example.cpp @@ -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") diff --git a/http_connection.h b/http_connection.h index 3e1ae62..e2c21ff 100644 --- a/http_connection.h +++ b/http_connection.h @@ -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; { diff --git a/http_response.h b/http_response.h index f0acd9e..b12c8a5 100644 --- a/http_response.h +++ b/http_response.h @@ -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; + } }; } @@ -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() |