cJSON_util.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <rtthread.h>
  6. #include "cJSON.h"
  7. #include "cJSON_util.h"
  8. void cJSON_free(void *ptr)
  9. {
  10. rt_free(ptr);
  11. }
  12. const char * cJSON_item_get_string(cJSON *object, const char *item_name)
  13. {
  14. cJSON *item;
  15. item = cJSON_GetObjectItem(object, item_name);
  16. if(!item)
  17. return 0;
  18. if( (item->type != cJSON_String) && (item->type != cJSON_Array) )
  19. return 0;
  20. if(item->type == cJSON_Array)
  21. return item->child->valuestring; // TODO
  22. return item->valuestring;
  23. }
  24. int cJSON_item_get_number(cJSON *object, const char *item_name, int * result)
  25. {
  26. cJSON *item;
  27. item = cJSON_GetObjectItem(object, item_name);
  28. if(!item)
  29. return -1;
  30. if(item->type != cJSON_Number)
  31. return -1;
  32. if(result)
  33. *result = item->valueint;
  34. return 0;
  35. }
  36. void cJSON_AddInteger2StringToObject(cJSON *object, const char *name, int i)
  37. {
  38. char str_buf[10+2];
  39. sprintf(str_buf, "%d", i);
  40. cJSON_AddStringToObject(object, name, str_buf);
  41. }