Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
JsonRequestHelper.cpp
Go to the documentation of this file.
1
18 #include "framework_config.h" // Must be included before DebugTrace.h to ensure framework_config.h is processed first
19 #include "DebugTrace.h"
21
22 #include "http/JsonRequestHelper.h"
23 #include "http/HttpRequest.h"
24
25 using json = nlohmann::json;
26
29 {
30 if (!req.isJson()) return json::object();
31 return json::parse(req.getBody(), nullptr, false);
32 }
33
35 json JsonRequestHelper::getJsonValue(const HttpRequest& req, const std::string& path)
36 {
37 json current = getFullJson(req);
38 if (!current.is_object()) return nullptr;
39
40 size_t pos = 0, next;
41 while ((next = path.find('.', pos)) != std::string::npos)
42 {
43 std::string part = path.substr(pos, next - pos);
44 if (!current.contains(part) || !current[part].is_object()) return nullptr;
45 current = current[part];
46 pos = next + 1;
47 }
48
49 std::string last = path.substr(pos);
50 return current.contains(last) ? current[last] : nullptr;
51 }
52
54 bool JsonRequestHelper::hasField(const HttpRequest& req, const std::string& key)
55 {
56 return !getJsonValue(req, key).is_null();
57 }
58
60 std::string JsonRequestHelper::getString(const HttpRequest& req, const std::string& key)
61 {
62 auto val = getJsonValue(req, key);
63 if (val.is_string()) return val.get<std::string>();
64 if (!val.is_null()) return val.dump();
65 return "";
66 }
67
69 int JsonRequestHelper::getInt(const HttpRequest& req, const std::string& key, int def)
70 {
71 auto val = getJsonValue(req, key);
72 return val.is_number_integer() ? val.get<int>() : def;
73 }
74
76 double JsonRequestHelper::getDouble(const HttpRequest& req, const std::string& key, double def)
77 {
78 auto val = getJsonValue(req, key);
79 return val.is_number() ? val.get<double>() : def;
80 }
81
83 bool JsonRequestHelper::getBool(const HttpRequest& req, const std::string& key, bool def)
84 {
85 auto val = getJsonValue(req, key);
86 return val.is_boolean() ? val.get<bool>() : def;
87 }
88
90 json JsonRequestHelper::getArray(const HttpRequest& req, const std::string& key)
91 {
92 auto val = getJsonValue(req, key);
93 return val.is_array() ? val : json::array();
94 }
95
97 json JsonRequestHelper::getObject(const HttpRequest& req, const std::string& key)
98 {
99 auto val = getJsonValue(req, key);
100 return val.is_object() ? val : json::object();
101 }
102
Macro-based debug trace system with optional SD file logging.
#define TRACE_INIT(MODULE_NAME)
Declare trace usage in a source file for a given module.
Definition DebugTrace.h:170
Defines the HttpRequest class for handling HTTP requests: headers, method, path, query string,...
nlohmann::json json
nlohmann::json json
Forward declaration for potential routing needs.
Definition HttpRequest.h:32
const std::string & getBody() const
Get the request body (copy).
bool isJson() const
Check if content-type is application/json.
Utility class for working with JSON content in HTTP requests.
static std::string getString(const HttpRequest &req, const std::string &key)
Get a string value from the request body.
static bool getBool(const HttpRequest &req, const std::string &key, bool def=false)
Get a boolean value from the request body.
static json getJsonValue(const HttpRequest &req, const std::string &path)
Extract a nested JSON value using a dot-separated path (e.g., "user.name").
static int getInt(const HttpRequest &req, const std::string &key, int def=0)
Get an integer value from the request body.
static json getArray(const HttpRequest &req, const std::string &key)
Get a JSON array from the request body.
static bool hasField(const HttpRequest &req, const std::string &key)
Check if a JSON field exists.
static json getFullJson(const HttpRequest &req)
Parse and return the full JSON body from the request.
static double getDouble(const HttpRequest &req, const std::string &key, double def=0.0)
Get a double value from the request body.
static json getObject(const HttpRequest &req, const std::string &key)
Get a JSON object from the request body.
Delegates to user or system configuration.