json.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #ifndef CHECKIN_JSON_H
  2. #define CHECKIN_JSON_H 1
  3. #if ENABLE_JSONC
  4. #include <json-c/json.h>
  5. // fix an older json-c bug
  6. // https://github.com/json-c/json-c/issues/135
  7. #ifdef error_description
  8. #undef error_description
  9. #endif // error_description
  10. #endif // ENABLE_JSONC
  11. #include "jsmn.h"
  12. //https://www.ibm.com/support/knowledgecenter/en/SS9H2Y_7.6.0/com.ibm.dp.doc/json_parserlimits.html
  13. #define JSON_NAME_LEN 256
  14. #define JSON_FULLNAME_LEN 1024
  15. typedef enum {
  16. JSON_OBJECT = 0,
  17. JSON_ARRAY = 1,
  18. JSON_STRING = 2,
  19. JSON_NUMBER = 3,
  20. JSON_BOOLEAN = 4,
  21. JSON_NULL = 5,
  22. } JSON_ENTRY_TYPE;
  23. typedef struct json_entry {
  24. JSON_ENTRY_TYPE type;
  25. char name[JSON_NAME_LEN + 1];
  26. char fullname[JSON_FULLNAME_LEN + 1];
  27. union {
  28. char *string; // type == JSON_STRING
  29. NETDATA_DOUBLE number; // type == JSON_NUMBER
  30. int boolean; // type == JSON_BOOLEAN
  31. size_t items; // type == JSON_ARRAY
  32. } data;
  33. size_t pos; // the position of this item in its parent
  34. char *original_string;
  35. void *callback_data;
  36. int (*callback_function)(struct json_entry *);
  37. } JSON_ENTRY;
  38. // ----------------------------------------------------------------------------
  39. // public functions
  40. #define JSON_OK 0
  41. #define JSON_CANNOT_DOWNLOAD 1
  42. #define JSON_CANNOT_PARSE 2
  43. int json_parse(char *js, void *callback_data, int (*callback_function)(JSON_ENTRY *));
  44. // ----------------------------------------------------------------------------
  45. // private functions
  46. #ifdef ENABLE_JSONC
  47. json_object *json_tokenise(char *js);
  48. size_t json_walk(json_object *t, void *callback_data, int (*callback_function)(struct json_entry *));
  49. #else
  50. jsmntok_t *json_tokenise(char *js, size_t len, size_t *count);
  51. size_t json_walk_tree(char *js, jsmntok_t *t, void *callback_data, int (*callback_function)(struct json_entry *));
  52. #endif
  53. size_t json_walk_object(char *js, jsmntok_t *t, size_t nest, size_t start, JSON_ENTRY *e);
  54. size_t json_walk_array(char *js, jsmntok_t *t, size_t nest, size_t start, JSON_ENTRY *e);
  55. size_t json_walk_string(char *js, jsmntok_t *t, size_t start, JSON_ENTRY *e);
  56. size_t json_walk_primitive(char *js, jsmntok_t *t, size_t start, JSON_ENTRY *e);
  57. int json_callback_print(JSON_ENTRY *e);
  58. static inline void cleanup_json_object_pp(struct json_object **jobj) {
  59. if(*jobj)
  60. json_object_put(*jobj);
  61. }
  62. #define CLEAN_JSON_OBJECT _cleanup_(cleanup_json_object_pp) struct json_object
  63. #endif // CHECKIN_JSON_H