MLIB
Loading...
Searching...
No Matches
mlib::JSONBridge Class Reference

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< entrydictionary
 
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_connectionclient ()
 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
 
entryadd_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
 

Detailed Description

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:

JSONBridge jb("var");
JSON objects support.
Definition jbridge.h:26

Then:

jb.attach_to(hs);

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.

Member Enumeration Documentation

◆ jb_type

Dictionary entry types.

Enumerator
JT_SHORT 

short int

JT_USHORT 

unsigned short int

JT_INT 

int

JT_UINT 

unsigned int

JT_LONG 

long

JT_ULONG 

unsigned long

JT_FLT 

float

JT_DBL 

double

JT_PSTR 

char*

JT_CSTR 

char[]

JT_STR 

std::string

JT_BOOL 

bool

JT_OBJECT 

composite object

JT_POSTFUN 

POST function call.

Member Function Documentation

◆ find()

bool mlib::JSONBridge::find ( const std::string &  name,
dict_cptr &  found,
size_t *  pidx = 0 
) const
protected

Search a variable in JSON dictionary.

The variable name can be a construct <name>_<index> for an indexed variable.


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