JSONObject.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "JSONObject.h"
  2. #include <stdlib.h>
  3. bell::JSONValue::JSONValue(cJSON *body, std::string key)
  4. {
  5. this->body = body;
  6. this->key = key;
  7. }
  8. void bell::JSONValue::operator=(const std::string val)
  9. {
  10. this->operator=(val.c_str());
  11. }
  12. void bell::JSONValue::operator=(const char *val)
  13. {
  14. cJSON_AddStringToObject(this->body, this->key.c_str(), val);
  15. }
  16. void bell::JSONValue::operator=(int val)
  17. {
  18. cJSON_AddNumberToObject(this->body, this->key.c_str(), val);
  19. }
  20. bell::JSONObject::JSONObject()
  21. {
  22. this->body = cJSON_CreateObject();
  23. }
  24. bell::JSONObject::~JSONObject()
  25. {
  26. cJSON_Delete(this->body);
  27. }
  28. bell::JSONValue bell::JSONObject::operator[](std::string index)
  29. {
  30. return bell::JSONValue(this->body, index);
  31. }
  32. std::string bell::JSONObject::toString()
  33. {
  34. char *body = cJSON_Print(this->body);
  35. std::string retVal = std::string(body);
  36. free(body);
  37. return retVal;
  38. }
  39. std::vector<uint8_t> bell::JSONObject::toVector() {
  40. char *body = cJSON_Print(this->body);
  41. std::vector<uint8_t> res(body, body + strlen(body));
  42. free(body);
  43. return res;
  44. }