Utility class for working with JSON content in HTTP requests.
More...
#include <JsonRequestHelper.h>
|
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 bool | hasField (const HttpRequest &req, const std::string &key) |
| Check if a JSON field exists.
|
|
static std::string | getString (const HttpRequest &req, const std::string &key) |
| Get a string value from the request body.
|
|
static int | getInt (const HttpRequest &req, const std::string &key, int def=0) |
| Get an integer value from the request body.
|
|
static double | getDouble (const HttpRequest &req, const std::string &key, double def=0.0) |
| Get a double 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 | getFullJson (const HttpRequest &req) |
| Parse and return the full JSON body from the request.
|
|
static json | getArray (const HttpRequest &req, const std::string &key) |
| Get a JSON array from the request body.
|
|
static json | getObject (const HttpRequest &req, const std::string &key) |
| Get a JSON object from the request body.
|
|
Definition at line 23 of file JsonRequestHelper.h.
◆ getArray()
json JsonRequestHelper::getArray |
( |
const HttpRequest & |
req, |
|
|
const std::string & |
key |
|
) |
| |
|
static |
Get a JSON array from the request body.
- Parameters
-
req | HttpRequest with JSON body. |
key | Dot-separated path to the array. |
- Returns
- The array if found and valid, or an empty array.
- Parameters
-
req | HttpRequest with JSON body. |
key | Dot-separated path to the array. |
- Returns
- The array if found and valid, or an empty array.
Definition at line 90 of file JsonRequestHelper.cpp.
91 {
93 return val.is_array() ? val : json::array();
94 }
static json getJsonValue(const HttpRequest &req, const std::string &path)
Extract a nested JSON value using a dot-separated path (e.g., "user.name").
References getJsonValue().
◆ getBool()
bool JsonRequestHelper::getBool |
( |
const HttpRequest & |
req, |
|
|
const std::string & |
key, |
|
|
bool |
def = false |
|
) |
| |
|
static |
Get a boolean value from the request body.
- Parameters
-
req | HttpRequest containing JSON body. |
key | Dot-separated path to the boolean field. |
def | Default value if key is missing or not boolean. |
- Returns
- The boolean value or default.
- Parameters
-
req | HttpRequest containing JSON body. |
key | Dot-separated path to the boolean field. |
def | Default value if key is missing or not boolean. |
- Returns
- The boolean value or default.
Definition at line 83 of file JsonRequestHelper.cpp.
84 {
86 return val.is_boolean() ? val.get<bool>() : def;
87 }
References getJsonValue().
◆ getDouble()
double JsonRequestHelper::getDouble |
( |
const HttpRequest & |
req, |
|
|
const std::string & |
key, |
|
|
double |
def = 0.0 |
|
) |
| |
|
static |
Get a double value from the request body.
- Parameters
-
req | HttpRequest containing JSON body. |
key | Dot-separated path to the double field. |
def | Default value if key is missing or not numeric. |
- Returns
- The double value or default.
- Parameters
-
req | HttpRequest containing JSON body. |
key | Dot-separated path to the double field. |
def | Default value if key is missing or not numeric. |
- Returns
- The double value or default.
Definition at line 76 of file JsonRequestHelper.cpp.
77 {
79 return val.is_number() ? val.get<double>() : def;
80 }
References getJsonValue().
◆ getFullJson()
Parse and return the full JSON body from the request.
- Parameters
-
req | The HTTP request object. |
- Returns
- Parsed JSON object or empty
{}
if parsing fails.
- Parameters
-
req | The HTTP request object. |
- Returns
- Parsed JSON object or empty
{}
if parsing fails.
Definition at line 28 of file JsonRequestHelper.cpp.
29 {
30 if (!req.
isJson())
return json::object();
31 return json::parse(req.
getBody(),
nullptr,
false);
32 }
const std::string & getBody() const
Get the request body (copy).
bool isJson() const
Check if content-type is application/json.
References HttpRequest::getBody(), and HttpRequest::isJson().
Referenced by getJsonValue().
◆ getInt()
int JsonRequestHelper::getInt |
( |
const HttpRequest & |
req, |
|
|
const std::string & |
key, |
|
|
int |
def = 0 |
|
) |
| |
|
static |
Get an integer value from the request body.
- Parameters
-
req | HttpRequest containing JSON body. |
key | Dot-separated path to the integer field. |
def | Default value if key is missing or not an integer. |
- Returns
- The integer value or default.
- Parameters
-
req | HttpRequest containing JSON body. |
key | Dot-separated path to the integer field. |
def | Default value if key is missing or not an integer. |
- Returns
- The integer value or default.
Definition at line 69 of file JsonRequestHelper.cpp.
70 {
72 return val.is_number_integer() ? val.get<int>() : def;
73 }
References getJsonValue().
◆ getJsonValue()
json JsonRequestHelper::getJsonValue |
( |
const HttpRequest & |
req, |
|
|
const std::string & |
path |
|
) |
| |
|
static |
Extract a nested JSON value using a dot-separated path (e.g., "user.name").
- Parameters
-
req | The incoming request object. |
path | Dot-separated path to the value. |
- Returns
- Extracted JSON value or null.
- Parameters
-
req | The incoming request object. |
path | Dot-separated path to the value. |
- Returns
- Extracted JSON value or null.
Definition at line 35 of file JsonRequestHelper.cpp.
36 {
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 }
static json getFullJson(const HttpRequest &req)
Parse and return the full JSON body from the request.
References getFullJson().
Referenced by getArray(), getBool(), getDouble(), getInt(), getObject(), getString(), and hasField().
◆ getObject()
json JsonRequestHelper::getObject |
( |
const HttpRequest & |
req, |
|
|
const std::string & |
key |
|
) |
| |
|
static |
Get a JSON object from the request body.
- Parameters
-
req | HttpRequest with JSON body. |
key | Dot-separated path to the object. |
- Returns
- The object if found and valid, or an empty object.
- Parameters
-
req | HttpRequest with JSON body. |
key | Dot-separated path to the object. |
- Returns
- The object if found and valid, or an empty object.
Definition at line 97 of file JsonRequestHelper.cpp.
98 {
100 return val.is_object() ? val : json::object();
101 }
References getJsonValue().
◆ getString()
std::string JsonRequestHelper::getString |
( |
const HttpRequest & |
req, |
|
|
const std::string & |
key |
|
) |
| |
|
static |
Get a string value from the request body.
- Parameters
-
req | HttpRequest containing JSON body. |
key | Dot-separated path to the string field. |
- Returns
- The string value or empty string if not found.
- Parameters
-
req | HttpRequest containing JSON body. |
key | Dot-separated path to the string field. |
- Returns
- The string value or empty string if not found.
Definition at line 60 of file JsonRequestHelper.cpp.
61 {
63 if (val.is_string()) return val.get<std::string>();
64 if (!val.is_null()) return val.dump();
65 return "";
66 }
References getJsonValue().
◆ hasField()
bool JsonRequestHelper::hasField |
( |
const HttpRequest & |
req, |
|
|
const std::string & |
key |
|
) |
| |
|
static |
Check if a JSON field exists.
- Parameters
-
req | The incoming request. |
key | Dot-separated key path. |
- Returns
- true if the field exists and is not null.
- Parameters
-
req | The incoming request. |
key | Dot-separated key path. |
- Returns
- true if the field exists and is not null.
Definition at line 54 of file JsonRequestHelper.cpp.
References getJsonValue().
◆ parseJsonBody()
- Parameters
-
- Returns
- Parsed JSON object, or
{}
on failure.
The documentation for this class was generated from the following files: