|
MLIB
|
JSON objects support. More...
#include <jbridge.h>
Classes | |
| class | entry |
| JSON data dictionary entry. More... | |
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 |
| Data dictionary structure. | |
| typedef dictionary::iterator | dict_ptr |
| Dictionary iterator. | |
| typedef dictionary::const_iterator | dict_cptr |
| Dictionary iterator. | |
Public Member Functions | |
| jbridge (const char *path) | |
| Creates a jbridge object for the given path. | |
| void | attach_to (server &server) |
| Attach jbridge 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 |
| Parse a JSON-encoded body of a POST message. | |
| 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) |
| Add an object to data dictionary. | |
| 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) |
| Generate 410 response if a variable was not found in JSON dictionary. | |
| 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 |
| Search for a variable name in JSON dictionary. | |
JSON objects support.
This class provides JSON serialization/deserialization of variables.
It gives an easy access mechanism to program variables for the HTTP server.
A jbridge 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 jbridge.
Assume the HTTP server hs answers requests sent to http://localhost:8080 and the jbridge 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 an object to data dictionary.
| name | object's name |
|
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.