Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
JsonRequestHelper Class Reference

Utility class for working with JSON content in HTTP requests. More...

#include <JsonRequestHelper.h>

+ Collaboration diagram for JsonRequestHelper:

Static Public Member Functions

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.
 

Static Private Member Functions

static json parseJsonBody (const HttpRequest &req)
 Parse the raw body into a JSON object.
 

Detailed Description

Definition at line 23 of file JsonRequestHelper.h.

Member Function Documentation

◆ getArray()

json JsonRequestHelper::getArray ( const HttpRequest req,
const std::string &  key 
)
static

Get a JSON array from the request body.

Parameters
reqHttpRequest with JSON body.
keyDot-separated path to the array.
Returns
The array if found and valid, or an empty array.
Parameters
reqHttpRequest with JSON body.
keyDot-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 {
92 auto val = getJsonValue(req, key);
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().

+ Here is the call graph for this function:

◆ getBool()

bool JsonRequestHelper::getBool ( const HttpRequest req,
const std::string &  key,
bool  def = false 
)
static

Get a boolean value from the request body.

Parameters
reqHttpRequest containing JSON body.
keyDot-separated path to the boolean field.
defDefault value if key is missing or not boolean.
Returns
The boolean value or default.
Parameters
reqHttpRequest containing JSON body.
keyDot-separated path to the boolean field.
defDefault value if key is missing or not boolean.
Returns
The boolean value or default.

Definition at line 83 of file JsonRequestHelper.cpp.

84 {
85 auto val = getJsonValue(req, key);
86 return val.is_boolean() ? val.get<bool>() : def;
87 }

References getJsonValue().

+ Here is the call graph for this function:

◆ 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
reqHttpRequest containing JSON body.
keyDot-separated path to the double field.
defDefault value if key is missing or not numeric.
Returns
The double value or default.
Parameters
reqHttpRequest containing JSON body.
keyDot-separated path to the double field.
defDefault value if key is missing or not numeric.
Returns
The double value or default.

Definition at line 76 of file JsonRequestHelper.cpp.

77 {
78 auto val = getJsonValue(req, key);
79 return val.is_number() ? val.get<double>() : def;
80 }

References getJsonValue().

+ Here is the call graph for this function:

◆ getFullJson()

json JsonRequestHelper::getFullJson ( const HttpRequest req)
static

Parse and return the full JSON body from the request.

Parameters
reqThe HTTP request object.
Returns
Parsed JSON object or empty {} if parsing fails.
Parameters
reqThe 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().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getInt()

int JsonRequestHelper::getInt ( const HttpRequest req,
const std::string &  key,
int  def = 0 
)
static

Get an integer value from the request body.

Parameters
reqHttpRequest containing JSON body.
keyDot-separated path to the integer field.
defDefault value if key is missing or not an integer.
Returns
The integer value or default.
Parameters
reqHttpRequest containing JSON body.
keyDot-separated path to the integer field.
defDefault value if key is missing or not an integer.
Returns
The integer value or default.

Definition at line 69 of file JsonRequestHelper.cpp.

70 {
71 auto val = getJsonValue(req, key);
72 return val.is_number_integer() ? val.get<int>() : def;
73 }

References getJsonValue().

+ Here is the call graph for this function:

◆ 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
reqThe incoming request object.
pathDot-separated path to the value.
Returns
Extracted JSON value or null.
Parameters
reqThe incoming request object.
pathDot-separated path to the value.
Returns
Extracted JSON value or null.

Definition at line 35 of file JsonRequestHelper.cpp.

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 }
nlohmann::json json
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().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getObject()

json JsonRequestHelper::getObject ( const HttpRequest req,
const std::string &  key 
)
static

Get a JSON object from the request body.

Parameters
reqHttpRequest with JSON body.
keyDot-separated path to the object.
Returns
The object if found and valid, or an empty object.
Parameters
reqHttpRequest with JSON body.
keyDot-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 {
99 auto val = getJsonValue(req, key);
100 return val.is_object() ? val : json::object();
101 }

References getJsonValue().

+ Here is the call graph for this function:

◆ getString()

std::string JsonRequestHelper::getString ( const HttpRequest req,
const std::string &  key 
)
static

Get a string value from the request body.

Parameters
reqHttpRequest containing JSON body.
keyDot-separated path to the string field.
Returns
The string value or empty string if not found.
Parameters
reqHttpRequest containing JSON body.
keyDot-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 {
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 }

References getJsonValue().

+ Here is the call graph for this function:

◆ hasField()

bool JsonRequestHelper::hasField ( const HttpRequest req,
const std::string &  key 
)
static

Check if a JSON field exists.

Parameters
reqThe incoming request.
keyDot-separated key path.
Returns
true if the field exists and is not null.
Parameters
reqThe incoming request.
keyDot-separated key path.
Returns
true if the field exists and is not null.

Definition at line 54 of file JsonRequestHelper.cpp.

55 {
56 return !getJsonValue(req, key).is_null();
57 }

References getJsonValue().

+ Here is the call graph for this function:

◆ parseJsonBody()

static json JsonRequestHelper::parseJsonBody ( const HttpRequest req)
staticprivate
Parameters
reqHttpRequest containing JSON body.
Returns
Parsed JSON object, or {} on failure.

The documentation for this class was generated from the following files: