mqttjson.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // mqttjson.h :
  2. //
  3. #if !defined(AGD_MQTTJSON_H__DAA1F894_3266_4778_855A_77CFA4A5156A__INCLUDED_)
  4. #define AGD_MQTTJSON_H__DAA1F894_3266_4778_855A_77CFA4A5156A__INCLUDED_
  5. #include <jansson.h>
  6. #ifdef __cplusplus
  7. /////////////////////////////////////////////////////////////////////////////
  8. // mqttjson.h - Declarations:
  9. class CJson_t
  10. {
  11. public:
  12. CJson_t(void) : m_pjt(NULL), m_bFree(false)
  13. {
  14. }
  15. CJson_t(json_t *pjt, bool bFree = true) : m_pjt(pjt), m_bFree(bFree)
  16. {
  17. }
  18. ~CJson_t(void)
  19. {
  20. if(m_pjt && m_bFree)
  21. json_decref(m_pjt);
  22. }
  23. CJson_t& operator = (json_t *pjt)
  24. {
  25. m_pjt = pjt;
  26. return *this;
  27. }
  28. operator json_t*(void) {
  29. return m_pjt;}
  30. operator const json_t*(void) const {
  31. return m_pjt;}
  32. bool Attach(json_t *pjt, bool bFree) {
  33. if(!pjt)
  34. return false;
  35. m_pjt = pjt;
  36. m_bFree = bFree;
  37. return true;}
  38. bool GetValue(const char *key, CJson_t &val)
  39. {
  40. if(IsValid() && key && *key)
  41. {
  42. json_t *pjt = json_object_get(m_pjt, key);
  43. if(!pjt)
  44. return false;
  45. return val.Attach(pjt, false);
  46. }
  47. return false;
  48. }
  49. bool IsValid(void) const {
  50. return !!m_pjt;}
  51. bool IsArray(void) {
  52. return json_is_array(m_pjt);}
  53. bool IsObject(void) {
  54. return json_is_object(m_pjt);}
  55. int Type(void) const {
  56. return IsValid() ? json_typeof(m_pjt) : -1;}
  57. private:
  58. json_t *m_pjt;
  59. bool m_bFree;
  60. };
  61. /////////////////////////////////////////////////////////////////////////////
  62. #endif // __cplusplus
  63. #endif // !defined(AGD_MQTTJSON_H__DAA1F894_3266_4778_855A_77CFA4A5156A__INCLUDED_)