mirror of
https://github.com/CrowCpp/Crow.git
synced 2024-06-07 21:10:44 +00:00
response can return json object
This commit is contained in:
parent
d908a2657e
commit
86433135f3
@ -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")
|
||||
|
@ -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;
|
||||
{
|
||||
|
@ -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
17
json.h
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user