json.h 2.0 KB

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