MLIB
|
JSON objects support. More...
#include <jbridge.h>
Data Structures | |
class | entry |
Public Types | |
enum | jb_type { JT_UNKNOWN , JT_SHORT , JT_USHORT , JT_INT , JT_UINT , JT_LONG , JT_ULONG , JT_FLT , JT_DBL , JT_PSTR , JT_CSTR , JT_STR , JT_BOOL , JT_OBJECT , JT_POSTFUN } |
Dictionary entry types. More... | |
typedef std::list< entry > | dictionary |
typedef dictionary::iterator | dict_ptr |
typedef dictionary::const_iterator | dict_cptr |
Public Member Functions | |
JSONBridge (const char *path) | |
Creates a JSONBridge object for the given path. | |
void | attach_to (server &server) |
Attach JSONBridge object to a HTTP server. | |
void | lock () |
Enter the critical section associated with this bridge. | |
void | unlock () |
Leave the critical section associated with this bridge. | |
const std::string & | path () const |
Return the root path where bridge is attached. | |
void | redirect_to (const std::string &uri) |
Set default redirection target for POST requests. | |
void | set_post_action (uri_handler pfn) |
Set the function invoked after successful processing of a POST request. | |
connection & | client () |
Return currently connected client (if any) | |
bool | parse_urlencoded () const |
Parse the URL-encoded body of a POST request assigning new values to all variables. | |
bool | parse_jsonencoded () const |
template<typename T, size_t C> | |
void | add_var (T(&var)[C], const std::string &name) |
add array | |
template<typename T> | |
std::enable_if_t< std::is_same_v< T, std::string >||std::is_same_v< T, short >||std::is_same_v< T, unsigned short >||std::is_same_v< T, int >||std::is_same_v< T, unsigned int >||std::is_same_v< T, long >||std::is_same_v< T, unsigned long >||std::is_same_v< T, float >||std::is_same_v< T, double >||std::is_same_v< T, bool > > | add_var (T &var, const std::string &name) |
add variable of one of basic types | |
entry & | add_object (const std::string &name) |
void | add_postfun (const std::string &name, uri_handler pfn) |
Add a handler function invoked in response to a POST request with an additional parameter. | |
Protected Member Functions | |
erc | jsonify (json::node &n, dict_cptr entry) |
Serializes a variable to JSON format. | |
void | not_found (const char *varname) |
bool | find (const std::string &name, dict_cptr &found, size_t *idx=0) const |
Search a variable in JSON dictionary. | |
bool | deep_find (const std::string &name, dict_cptr &found, size_t *idx=0) const |
JSON objects support.
JSONBridge class provides an easy access mechanism to program variables for HTTP server.
A JSONBridge object is attached to a HTTP server using the attach_to() function. Behind the scene, this function registers a handler for the path associated with the JSONBridge.
Assume the HTTP server hs
answers requests sent to http://localhost:8080
and the JSONBridge object was created as:
Then:
will register a handler for requests to http://localhost:8080/var
When this handler is invoked with a GET request for http://localhost:8080/var?data, it will search in the JSON dictionary a variable called 'data' and return its content as a JSON object.
|
inline |
Add a handler function invoked in response to a POST request with an additional parameter.
qparam | additional query parameter |
pfn | handler function |
If the handler function does not return HTTP_OK, the post_action
function is not invoked and client is not redirected to ‘redirect_uri’
|
protected |
Search a variable in JSON dictionary.
The variable name can be a construct <name>_<index>
for an indexed variable.
|
inline |
Set the function invoked after successful processing of a POST request.
This function is invoked before redirecting the client to the redirect_uri
. If the function does not return HTTP_OK, the client is not redirected.