SNode.C
Loading...
Searching...
No Matches
JsonMiddleware.cpp
Go to the documentation of this file.
1
/*
2
* SNode.C - a slim toolkit for network communication
3
* Copyright (C) Volker Christian <me@vchrist.at>
4
* 2020, 2021, 2022, 2023, 2024, 2025
5
* Json Middleware 2020, 2021 Marlene Mayr, Anna Moser, Matteo Prock, Eric Thalhammer
6
* Github <MarleneMayr><moseranna><MatteoMatteoMatteo><peregrin-tuk>
7
*
8
* This program is free software: you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation, either version 3 of the License, or
11
* (at your option) any later version.
12
*
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
17
*
18
* You should have received a copy of the GNU General Public License
19
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20
*/
21
22
#
include
"express/middleware/JsonMiddleware.h"
23
24
#
ifndef
DOXYGEN_SHOULD_SKIP_THIS
25
26
#
include
<
nlohmann
/
json
.
hpp
>
27
#
include
<
string
>
28
#
include
<
vector
>
29
30
// IWYU pragma: no_include <nlohmann/detail/exceptions.hpp>
31
// IWYU pragma: no_include <nlohmann/json_fwd.hpp>
32
33
#
endif
/* DOXYGEN_SHOULD_SKIP_THIS */
34
35
namespace
express::
middleware
{
36
37
JsonMiddleware
::
JsonMiddleware
() {
38
use([]
MIDDLEWARE
(req, res, next) {
39
try
{
40
// parse body string with json library
41
// store it as type json from nlohmann library
42
// nlohmann::json json = nlohmann::json::parse(req.body, req.body + req.contentLength);
43
44
// req->body.push_back(0);
45
const
nlohmann::json json = nlohmann::json::parse(std::string(req->body.begin(), req->body.end()));
46
47
// set all the json data as attributes in the request object
48
req->setAttribute<nlohmann::json>(json);
49
}
catch
([[maybe_unused]]
const
nlohmann::detail::parse_error& error) {
50
// silently fail if body is not json
51
}
52
53
next();
54
});
55
}
56
57
class
JsonMiddleware
&
JsonMiddleware
::
instance
() {
58
// Keep the created json middleware alive
59
static
std::shared_ptr<
class
JsonMiddleware
> jsonMiddleware =
nullptr
;
60
61
if
(jsonMiddleware ==
nullptr
) {
62
jsonMiddleware = std::shared_ptr<JsonMiddleware>(
new
JsonMiddleware());
63
}
64
65
return
*jsonMiddleware;
66
}
67
68
// "Constructor" of JsonMiddleware
69
class
JsonMiddleware
& JsonMiddleware() {
70
return
JsonMiddleware
::
instance
(
)
;
71
}
72
73
}
// namespace express::middleware
MIDDLEWARE
#define MIDDLEWARE(req, res, next)
Definition
Router.h:40
express::middleware::JsonMiddleware
Definition
JsonMiddleware.h:33
express::middleware::JsonMiddleware::JsonMiddleware
JsonMiddleware()
Definition
JsonMiddleware.cpp:37
express::middleware::JsonMiddleware::instance
static class JsonMiddleware & instance()
Definition
JsonMiddleware.cpp:57
express::middleware
Definition
StaticMiddleware.h:35
express
middleware
JsonMiddleware.cpp
Generated on Mon Feb 10 2025 20:21:02 for SNode.C by
1.11.0