1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- // mqttjson.h :
- //
- #if !defined(AGD_MQTTJSON_H__DAA1F894_3266_4778_855A_77CFA4A5156A__INCLUDED_)
- #define AGD_MQTTJSON_H__DAA1F894_3266_4778_855A_77CFA4A5156A__INCLUDED_
- #include <jansson.h>
- #ifdef __cplusplus
- /////////////////////////////////////////////////////////////////////////////
- // mqttjson.h - Declarations:
- class CJson_t
- {
- public:
- CJson_t(void) : m_pjt(NULL), m_bFree(false)
- {
- }
- CJson_t(json_t *pjt, bool bFree = true) : m_pjt(pjt), m_bFree(bFree)
- {
- }
- ~CJson_t(void)
- {
- if(m_pjt && m_bFree)
- json_decref(m_pjt);
- }
- CJson_t& operator = (json_t *pjt)
- {
- m_pjt = pjt;
- return *this;
- }
- operator json_t*(void) {
- return m_pjt;}
- operator const json_t*(void) const {
- return m_pjt;}
-
- bool Attach(json_t *pjt, bool bFree) {
- if(!pjt)
- return false;
- m_pjt = pjt;
- m_bFree = bFree;
- return true;}
-
- bool GetValue(const char *key, CJson_t &val)
- {
- if(IsValid() && key && *key)
- {
- json_t *pjt = json_object_get(m_pjt, key);
- if(!pjt)
- return false;
- return val.Attach(pjt, false);
- }
- return false;
- }
- bool IsValid(void) const {
- return !!m_pjt;}
- bool IsArray(void) {
- return json_is_array(m_pjt);}
- bool IsObject(void) {
- return json_is_object(m_pjt);}
- int Type(void) const {
- return IsValid() ? json_typeof(m_pjt) : -1;}
- private:
- json_t *m_pjt;
- bool m_bFree;
- };
- /////////////////////////////////////////////////////////////////////////////
- #endif // __cplusplus
- #endif // !defined(AGD_MQTTJSON_H__DAA1F894_3266_4778_855A_77CFA4A5156A__INCLUDED_)
|