MLIB
|
JSON objects support. More...
#include <jbridge.h>
Data Structures | |
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 |
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 (httpd &server) |
Attach JSONBridge object to a HTTP server. | |
void | lock () |
Enter the critical section associated with this context. | |
void | unlock () |
Leave the critical section associated with this context. | |
const std::string & | path () const |
Return the context path. | |
void | set_action (post_action pfn) |
http_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, post_fun pfn) |
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 function 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
If 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 it's content as a JSON object.
|
protected |
Search a variable in JSON dictionary.
The variable name can be a construct <name>_<index>
for an indexed variable.