cJSON_util.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <rtthread.h>
  5. #include "cJSON.h"
  6. #include "cJSON_util.h"
  7. void cJSON_free(void *ptr)
  8. {
  9. rt_free(ptr);
  10. }
  11. const char * cJSON_item_get_string(cJSON *object, const char *item_name)
  12. {
  13. cJSON *item;
  14. const char * string;
  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. const char * string;
  28. item = cJSON_GetObjectItem(object, item_name);
  29. if(!item)
  30. return -1;
  31. if(item->type != cJSON_Number)
  32. return -1;
  33. if(result)
  34. *result = item->valueint;
  35. return 0;
  36. }
  37. void cJSON_AddInteger2StringToObject(cJSON *object, const char *name, int i)
  38. {
  39. char str_buf[10+2];
  40. sprintf(str_buf, "%d", i);
  41. cJSON_AddStringToObject(object, name, str_buf);
  42. }