json.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #ifndef JSON__JSON_H__INCLUDED
  2. #define JSON__JSON_H__INCLUDED
  3. #include <stddef.h>
  4. #include <stdio.h>
  5. #include <stdarg.h>
  6. #include <stdint.h>
  7. //#define JSON_DEBUG
  8. #ifdef JSON_DEBUG
  9. #define dbg_printf(args...) printf(args)
  10. #else
  11. #define dbg_printf(args...) {}
  12. //static void nothin(char* x, ...) {};
  13. //efine dbg_printf nothin
  14. #endif
  15. // the parser currently does not handle comments.
  16. #define JSON_DISCARD_COMMENTS 1
  17. #ifndef JSON_NO_TYPEDEFS
  18. #define JSON_TD(x) x
  19. #define JSON_TYPEDEF typedef
  20. #else
  21. #define JSON_TD(x)
  22. #endif
  23. struct json_obj;
  24. struct json_array;
  25. JSON_TYPEDEF enum json_type {
  26. JSON_TYPE_UNDEFINED = 0,
  27. JSON_TYPE_NULL,
  28. JSON_TYPE_INT,
  29. JSON_TYPE_DOUBLE,
  30. JSON_TYPE_STRING,
  31. JSON_TYPE_BOOL,
  32. JSON_TYPE_OBJ,
  33. JSON_TYPE_ARRAY,
  34. JSON_TYPE_COMMENT_SINGLE,
  35. JSON_TYPE_COMMENT_MULTI,
  36. // only used for unpacking to structs
  37. JSON_TYPE_FLOAT,
  38. JSON_TYPE_INT8,
  39. JSON_TYPE_INT16,
  40. JSON_TYPE_INT32,
  41. JSON_TYPE_INT64,
  42. JSON_TYPE_UINT8,
  43. JSON_TYPE_UINT16,
  44. JSON_TYPE_UINT32,
  45. JSON_TYPE_UINT64,
  46. JSON_TYPE_MAXVALUE
  47. } JSON_TD(json_type_e);
  48. #define JSON__type_enum_tail JSON_TYPE_MAXVALUE
  49. JSON_TYPEDEF enum json_error {
  50. JSON_ERROR_NONE = 0,
  51. JSON_ERROR_OOM,
  52. JSON_LEX_ERROR_NULL_IN_STRING,
  53. JSON_LEX_ERROR_NULL_BYTE,
  54. JSON_LEX_ERROR_INVALID_STRING,
  55. JSON_LEX_ERROR_UNEXPECTED_END_OF_INPUT,
  56. JSON_LEX_ERROR_INVALID_CHAR,
  57. JSON_PARSER_ERROR_CORRUPT_STACK,
  58. JSON_PARSER_ERROR_STACK_EXHAUSTED,
  59. JSON_PARSER_ERROR_UNEXPECTED_EOI,
  60. JSON_PARSER_ERROR_UNEXPECTED_TOKEN,
  61. JSON_PARSER_ERROR_BRACE_MISMATCH,
  62. JSON_PARSER_ERROR_BRACKET_MISMATCH,
  63. JSON_ERROR_MAXVALUE
  64. } JSON_TD(json_error_e);
  65. struct json_obj_field;
  66. struct json_link;
  67. JSON_TYPEDEF struct json_value {
  68. enum json_type type;
  69. int base;
  70. size_t len;
  71. union {
  72. int64_t n;
  73. uint64_t u;
  74. double d;
  75. char* s;
  76. struct {
  77. size_t alloc_size;
  78. struct json_obj_field* buckets;
  79. } obj;
  80. struct {
  81. struct json_link* head, *tail;
  82. } arr;
  83. };
  84. } JSON_TD(json_value_t);
  85. JSON_TYPEDEF struct json_link {
  86. struct json_link* next, *prev;
  87. struct json_value* v;
  88. } JSON_TD(json_link_t);
  89. JSON_TYPEDEF struct json_file {
  90. struct json_value* root;
  91. void* lex_info; // don't poke around in here...
  92. enum json_error error;
  93. char* error_str;
  94. long error_line_num;
  95. long error_char_num;
  96. } JSON_TD(json_file_t);
  97. JSON_TYPEDEF struct json_string_buffer {
  98. char* buf;
  99. size_t length;
  100. size_t alloc;
  101. int line_len;
  102. } JSON_TD(json_string_buffer_t);
  103. JSON_TYPEDEF struct json_output_format {
  104. char indentChar;
  105. char indentAmt;
  106. char trailingComma;
  107. // char commaBeforeElem;
  108. char objColonSpace;
  109. char noQuoteKeys;
  110. char useSingleQuotes;
  111. // char escapeNonLatin;
  112. // char breakLongStrings;
  113. int minArraySzExpand;
  114. int minObjSzExpand;
  115. int maxLineLength; // only wraps after the comma on array/obj elements
  116. char* floatFormat;
  117. } JSON_TD(json_output_format_t);
  118. JSON_TYPEDEF struct json_write_context {
  119. int depth;
  120. struct json_string_buffer* sb;
  121. struct json_output_format fmt;
  122. } JSON_TD(json_write_context_t);
  123. /*
  124. Type coercion rules:
  125. undefined/null -> int = 0
  126. numbers, as you would expect, according to C type conversion rules
  127. obj/array -> number/string = error
  128. string/comment -> string = string
  129. number -> string = sprintf, accoding to some nice rules.
  130. string -> number = strtod/i
  131. Strings are always dup'd. You must free them yourself.
  132. JSON_TYPE_INT is assumed to be C int
  133. Numbers over 2^63 are not properly supported yet. they will be truncated to 0
  134. */
  135. int json_as_type(struct json_value* v, enum json_type t, void* out);
  136. int64_t json_as_int(struct json_value* v);
  137. double json_as_double(struct json_value* v);
  138. char* json_as_strdup(struct json_value* v);
  139. float json_as_float(struct json_value* v);
  140. /*
  141. #define JSON_UNPACK(t, f, type) #f, (t), (void*)(&((t)->f)) - (void*)(t), type
  142. int json_obj_unpack_struct(int count, struct json_value* obj, ...);
  143. int json_obj_unpack_string_array(struct json_value* obj, char*** out, size_t* len);
  144. */
  145. int json_array_push_tail(struct json_value* arr, struct json_value* val);
  146. int json_array_push_head(struct json_value* arr, struct json_value* val);
  147. struct json_value* json_array_pop_tail(struct json_value* arr);
  148. struct json_value* json_array_pop_head(struct json_value* arr);
  149. // USUALLY UNNECESSARY. manually calculate the array length.
  150. size_t json_array_calc_length(struct json_value* arr);
  151. int json_obj_get_key(struct json_value* obj, char* key, struct json_value** val);
  152. int json_obj_set_key(struct json_value* obj, char* key, struct json_value* val);
  153. int json_obj_set_key_nodup(struct json_value* obj, char* key, struct json_value* val); // takes ownership of key's memory
  154. // will probably be changed or removed later
  155. // coerces and strdup's the result
  156. // returns null if the key does not exist
  157. char* json_obj_key_as_string(struct json_value* obj, char* key);
  158. // returns a newly allocated string, or NULL if it's not a string
  159. char* json_obj_get_strdup(struct json_value* obj, char* key);
  160. // returns pointer to the internal string, or null if it's not a string
  161. char* json_obj_get_str(struct json_value* obj, char* key);
  162. // returns a double or the default value if it's not an integer
  163. double json_obj_get_double(struct json_value* obj, char* key, double def);
  164. // returns an integer or the default value if it's not an integer
  165. int64_t json_obj_get_int(struct json_value* obj, char* key, int64_t def);
  166. // returns the json_value strut for a key, or null if it doesn't exist
  167. struct json_value* json_obj_get_val(struct json_value* obj, char* key);
  168. // iteration. no order. results undefined if modified while iterating
  169. // returns 0 when there is none left
  170. // set iter to NULL to start
  171. int json_obj_next(struct json_value* val, void** iter, char** key, struct json_value** value);
  172. struct json_value* json_deep_copy(struct json_value* v);
  173. void json_merge(struct json_value* into, struct json_value* from);
  174. struct json_file* json_load_path(char* path);
  175. struct json_file* json_read_file(FILE* f);
  176. struct json_file* json_parse_string(char* source, size_t len);
  177. // recursive.
  178. void json_free(struct json_value* v);
  179. void json_file_free(struct json_file* jsf);
  180. struct json_value* json_new_str(char* s);
  181. struct json_value* json_new_strn(char* s, size_t len);
  182. struct json_value* json_new_double(double d);
  183. struct json_value* json_new_int(int64_t n);
  184. struct json_value* json_new_array(void);
  185. struct json_value* json_new_object(size_t initial_alloc_size);
  186. struct json_value* json_new_null(void);
  187. struct json_value* json_new_undefined(void);
  188. struct json_value* json_new_true(void);
  189. struct json_value* json_new_false(void);
  190. char* json_get_type_str(enum json_type t);
  191. char* json_get_err_str(enum json_error e);
  192. void json_dump_value(struct json_value* root, int cur_depth, int max_depth);
  193. struct json_string_buffer* json_string_buffer_create(size_t initSize);
  194. void json_string_buffer_free(struct json_string_buffer* sb);
  195. void json_stringify(struct json_write_context* ctx, struct json_value* v);
  196. /*
  197. Loop macro magic
  198. https://www.chiark.greenend.org.uk/~sgtatham/mp/
  199. json_value_t* obj;
  200. JSON_OBJ_EACH(&obj, key, val) {
  201. printf("loop: %s, %s", key, val->s);
  202. }
  203. effective source:
  204. #define JSON_OBJ_EACH(obj, keyname, valname)
  205. if(0)
  206. finished: ;
  207. else
  208. for(char* keyname;;) // internal declarations, multiple loops to avoid comma op funny business
  209. for(valtype valname;;)
  210. for(void* iter = NULL ;;)
  211. if(json_obj_next(obj, iter, &keyname, &valname))
  212. goto main_loop;
  213. else
  214. while(1)
  215. if(1) {
  216. // when the user uses break
  217. goto finished;
  218. }
  219. else
  220. while(1)
  221. if(!json_obj_next(obj, iter, &keyname, &valname)) {
  222. // normal termination
  223. goto finished;
  224. }
  225. else
  226. main_loop:
  227. // { user block; not in macro }
  228. */
  229. #define JSONHASH__PASTEINNER(a, b) a ## b
  230. #define JSONHASH__PASTE(a, b) JSONHASH__PASTEINNER(a, b)
  231. #define JSONHASH__ITER(key, val) JSONHASH__PASTE(jsonhashtable_iter_ ## key ## __ ## val ## __, __LINE__)
  232. #define JSONHASH__FINISHED(key, val) JSONHASH__PASTE(jsonhashtable_finished__ ## key ## __ ## val ## __, __LINE__)
  233. #define JSONHASH__MAINLOOP(key, val) JSONHASH__PASTE(jsonhashtable_main_loop__ ## key ## __ ## val ## __, __LINE__)
  234. #define JSON_OBJ_EACH(obj, keyname, valname) \
  235. if(0) \
  236. JSONHASH__FINISHED(keyname, val): ; \
  237. else \
  238. for(char* keyname ;;) \
  239. for(struct json_value* valname ;;) \
  240. for(void* JSONHASH__ITER(keyname, val) = NULL ;;) \
  241. if(json_obj_next(obj, & (JSONHASH__ITER(keyname, val)), &keyname, &valname)) \
  242. goto JSONHASH__MAINLOOP(keyname, val); \
  243. else \
  244. while(1) \
  245. if(1) { \
  246. goto JSONHASH__FINISHED(keyname, val); \
  247. } \
  248. else \
  249. while(1) \
  250. if(!json_obj_next(obj, & (JSONHASH__ITER(keyname, val)), &keyname, &valname)) { \
  251. goto JSONHASH__FINISHED(keyname, val); \
  252. } \
  253. else \
  254. JSONHASH__MAINLOOP(keyname, val) :
  255. // { user block; not in macro }
  256. #endif // JSON__JSON_H__INCLUDED