json.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. #ifndef _JSON_H
  2. #define _JSON_H
  3. #include <stdbool.h>
  4. /**
  5. * Structure: print_type_t
  6. * Typedef: print_type_t
  7. * Author: Joachim M. Giæver
  8. *
  9. * Sections:
  10. * JSON_SAVE_FILE,
  11. * JSON_PRINT,
  12. * JSON_PPRINT,
  13. * JSON_TYPE
  14. *
  15. * Description:
  16. * Representation of JSON after processing.
  17. **/
  18. typedef enum {
  19. JSON_SAVE_FILE = 0x32f,
  20. JSON_PRINT = JSON_SAVE_FILE << 1,
  21. JSON_PPRINT = JSON_PRINT << 1,
  22. JSON_TYPE
  23. } print_type_t;
  24. /**
  25. * Enum: json_type_t
  26. * Typedef: json_type_t
  27. * Author: Joachim M. Giæver
  28. *
  29. * Sections:
  30. * - describes itself
  31. *
  32. * Description:
  33. * Defines entries in JSON
  34. **/
  35. typedef enum {
  36. __JSON__ = 0x1,
  37. JSON_OBJ = __JSON__ << 1,
  38. JSON_MEM = JSON_OBJ << 1,
  39. JSON_ELEM = JSON_MEM << 1,
  40. JSON_PAIR = JSON_ELEM << 1,
  41. JSON_ARR = JSON_PAIR << 1,
  42. JSON_VAL = JSON_ARR << 1,
  43. JSON_CHAR = JSON_VAL << 1,
  44. JSON_INT = JSON_CHAR << 1,
  45. JSON_FLOAT = JSON_INT << 1,
  46. JSON_BOOL = JSON_FLOAT << 1,
  47. JSON_NULL = JSON_BOOL << 1,
  48. JSON_TYPE_NOTSET
  49. } json_types_t;
  50. /**
  51. * Structure: json_type
  52. * Typedef: json_type_t
  53. * Author: Joachim M. Giæver
  54. *
  55. * Sections:
  56. * json_types_t type: Type of an JSON-entry.
  57. *
  58. * Description:
  59. * Holds the type of an JSON-entry.
  60. **/
  61. typedef struct json_type json_type_t;
  62. /**
  63. * JSON-entry types
  64. **/
  65. /**
  66. * Structure:
  67. * Typedef:
  68. * Author: Joachim M. Giæver
  69. *
  70. * Sections:
  71. * json_types_t type: Entry type (MUST BE FIRST)
  72. * json_mem_t *members: JSON-meners entries.
  73. * print_type_t ptype: Print type, eg Pretty print.
  74. * int code: Status code.
  75. *
  76. * Description:
  77. * Defines an JSON-object.
  78. **/
  79. typedef struct json_obj json_obj_t;
  80. /**
  81. * Structure: json_mem
  82. * Typedef: json_mem_t
  83. * Author: Joachim M. Giæver
  84. *
  85. * Sections:
  86. * json_types_t type: Entry type (MUST BE FIRST)
  87. * void *entry: Containing entry. Either pair or new member
  88. * void *next: Simblings; other members or pairs in parent.
  89. *
  90. * Description:
  91. * Defines an JSON-member entry
  92. **/
  93. typedef struct json_mem json_mem_t;
  94. /**
  95. * Structure: json_elem
  96. * Typedef: json_elem_t
  97. * Author: Joachim M. Giæver
  98. *
  99. * Sections:
  100. * json_types_t type: Entry type (MUST BE FIRST).
  101. * void *entry: Containing entry. Either value or new element.
  102. * void *next: Simblings; other elements in parent.
  103. *
  104. * Description:
  105. * Defines an JSON-element entry.
  106. **/
  107. typedef struct json_elem json_elem_t;
  108. /**
  109. * Structure: json_pair
  110. * Typedef: json_pair_t
  111. * Author: Joachim M. Giæver
  112. *
  113. * Sections:
  114. * json_types_t type: Entry type (MUST BE FIRST).
  115. * char *str: String specifier.
  116. * json_val_t *value; Containing entry, only value.
  117. * void *next: Simblings; other members or pairs in parent.
  118. *
  119. * Description:
  120. *
  121. **/
  122. typedef struct json_pair json_pair_t;
  123. /**
  124. * Structure: json_arr
  125. * Typedef: json_arr_t
  126. * Author: Joachim M. Giæver
  127. *
  128. * Sections:
  129. * json_types_t type: Entry type (MUST BE FIRST).
  130. * void *entry: Containing entry, only elements.
  131. *
  132. * Description:
  133. * Defines a JSON-array entry.
  134. **/
  135. typedef struct json_arr json_arr_t;
  136. /**
  137. * Structure: json_val
  138. * Typedef: json_val_t
  139. * Author: Joachim M. Giæver
  140. *
  141. * Sections:
  142. * json_types_t type: Entry type (MUST BE FIRST).
  143. * json_types_t sub_type: Type of the sub entry, eg JSON_INT, _CHAR etc
  144. * void *value: The actual entry
  145. *
  146. * Description:
  147. *
  148. **/
  149. typedef struct json_val json_val_t;
  150. typedef void json_printf_t (void *, ...);
  151. /**
  152. * Result codes; OK = 0 =< resultcode > 0 = Failure
  153. **/
  154. #define JSON_INS_ELEM_ARR 11
  155. #define JSON_INS_VAL_ELEM 9
  156. #define JSON_INS_VAL_VAL 8
  157. #define JSON_INS_TYPE_VAL 7
  158. #define JSON_INS_VAL_PAIR 6
  159. #define JSON_INS_PAIR_MEM 5
  160. #define JSON_INS_MEM_OBJ 3
  161. #define JSON_INS_CORRECTLY 2
  162. #define JSON_OK 1
  163. #define JSON_NO_REPORT 0
  164. #define JSON_UNKNOWN_PTYPE -1
  165. #define JSON_UNINITALIZED_OJB -2
  166. #define JSON_NOMEM -3
  167. #define JSON_INVALID_ROOT_ENT -4
  168. #define JSON_INVALID_OBJ_ENT -5
  169. #define JSON_INVALID_MEM_ENT -6
  170. #define JSON_INVALID_PAIR_ENT -7
  171. #define JSON_INVALID_ELE_MENT -8
  172. #define JSON_INVALID_VAL_TYPE -9
  173. #define JSON_INVALID_VAL_ENT -10
  174. #define JSON_INVALID_ELEM_ENT -11
  175. #define JSON_INVALID_ARR_ENT -12
  176. #define JSON_MIXED_VAL_AND_TYPE -13
  177. #define JSON_INVALID_STRUCTURE -100
  178. /**
  179. * Function: json_create
  180. * Author: Joachim M. Giæver
  181. *
  182. * Parameters:
  183. * - char *fname: The filename; on saving
  184. and possible parsing - in the future.
  185. * - print_type_t ptype: Store/print type
  186. *
  187. * Description:
  188. * Creates the main root of the JSON-structure.
  189. *
  190. * Returns: json_obj_t *, the mother root
  191. **/
  192. json_obj_t *json_create( char *fname, print_type_t ptype );
  193. /**
  194. * Function: json_destroy
  195. * Author: Joachim M. Giæver
  196. *
  197. * Parameters:
  198. * - json_obj_t *jobj: JSON-object to destroy
  199. *
  200. * Description:
  201. * Destroys any kind of JSON-objects
  202. *
  203. * Returns: void
  204. **/
  205. void json_destroy( json_obj_t *jobj );
  206. /**
  207. * Function: json_create_obj
  208. * Author: Joachim M. Giæver
  209. *
  210. * Parameters:
  211. * - json_obj_t *jobj: JSON-object which is the main-root
  212. * - json_val_t *jval: JSON-value to insert to
  213. *
  214. * Description:
  215. * Creates a new JSON-object to insert into the
  216. * JSON-structure (only into JSON-value.).
  217. *
  218. * Works recursivly on children.
  219. *
  220. * Returns: json_obj_t *, JSON-object
  221. **/
  222. json_obj_t *json_create_obj( json_obj_t *jobj, json_val_t *jval );
  223. /**
  224. * Function: json_create_mem
  225. * Author: Joachim M. Giæver
  226. *
  227. * Parameters:
  228. * - json_obj_t *jobj: JSON-object to insert to
  229. *
  230. * Description:
  231. * Creates a new JSON-member and inserts it into the
  232. * JSON-object. May be any kind of JSON-object, both
  233. * root or children.
  234. *
  235. * Returns: json_mem_t *, JSON-member
  236. **/
  237. json_mem_t *json_create_mem( json_obj_t *jobj );
  238. /**
  239. * Function: json_destroy_mem
  240. * Author: Joachim M. Giæver
  241. *
  242. * Parameters:
  243. * - json_mem_t *jmem: JSON-member to destroy
  244. *
  245. * Description:
  246. * Destroy a JSON-member, works recursively on both simblings
  247. * and children.
  248. *
  249. * Returns: void
  250. **/
  251. void json_destroy_mem( json_mem_t *jmem );
  252. /**
  253. * Function: json_create_pair
  254. * Author: Joachim M. Giæver
  255. *
  256. * Parameters:
  257. * - json_obj_t *jobj: JSON-object which is the main-root
  258. * - json_mem_t *jmem: JSON-member to insert to
  259. *
  260. * Description:
  261. * Creates a new JSON-pair and inserts it into a JSON-member.
  262. *
  263. * Returns: json_pair_t *, JSON-pair
  264. **/
  265. json_pair_t *json_create_pair( json_obj_t *jobj, json_mem_t *jmem, char *str );
  266. /**
  267. * Function: json_destroy_pair
  268. * Author: Joachim M. Giæver
  269. *
  270. * Parameters:
  271. * - json_pair_t *jpair: JSON-pair to destroy
  272. *
  273. * Description:
  274. * Destroys a JSON-pair. Works recursively on both the simblings
  275. * and children.
  276. *
  277. * Returns: void
  278. **/
  279. void json_destroy_pair( json_pair_t *jpair );
  280. /**
  281. * Function: json_create_arr
  282. * Author: Joachim M. Giæver
  283. *
  284. * Parameters:
  285. * - json_obj_t *jobj: JSON-object which is the main-root
  286. * - json_val_t *jval: JSON-value to insert to
  287. *
  288. * Description:
  289. * Creates a new JSON-array and inserts it into JSON-value.
  290. *
  291. * Returns: json_arr_t *, JSON-array
  292. **/
  293. json_arr_t *json_create_arr( json_obj_t *jobj, json_val_t *jval );
  294. /**
  295. * Function: json_destroy_arr
  296. * Author: Joachim M. Giæver
  297. *
  298. * Parameters:
  299. * - json_arr_t *jarr: JSON-array to destroy
  300. *
  301. * Description:
  302. * Destroys a JSON-array. Works recursively on the children.
  303. *
  304. * Returns: void
  305. **/
  306. void json_destroy_arr( json_arr_t *jarr );
  307. /**
  308. * Function: json_create_elem
  309. * Author: Joachim M. Giæver
  310. *
  311. * Parameters:
  312. * - json_obj_t *jobj: JSON-object which is the main-root
  313. * - json_arr_t *jarr: JSON-array to insert into
  314. *
  315. * Description:
  316. * Creates a JSON-element and inserts it int an JSON-array.
  317. *
  318. * Returns: json_elem_t *, JSON-element
  319. **/
  320. json_elem_t *json_create_elem( json_obj_t *jobj, json_arr_t *jarr );
  321. /**
  322. * Function: json_destroy_elem
  323. * Author: Joachim M. Giæver
  324. *
  325. * Parameters:
  326. * - json_elem_t *jelem: JSON-element to destroy
  327. *
  328. * Description:
  329. * Destroys a JSON-element. Works recursively on both simblings
  330. * and children.
  331. *
  332. * Returns: void
  333. **/
  334. void json_destroy_elem( json_elem_t *jelem );
  335. /**
  336. * Function: json_insert_value
  337. * Author: Joachim M. Giæver
  338. *
  339. * Parameters:
  340. * - json_obj_t *jobj: JSON-object which is the main-root
  341. * - void *ins_obj: Object or element to insert into
  342. * - json_types_t vtype: Type of the held element in value
  343. * - void *entry: Entry to store in value
  344. *
  345. * Description:
  346. * Creates a new JSON-value and inserts it into a JSON-element or
  347. * a JSON-pair. The value must also be specified. JSON-object and
  348. * JSON-array can be insert upon creation by setting entry as NULL.
  349. *
  350. * Ex:
  351. *
  352. * // Insert this value into a JSON-pair, will contain an JSON-array
  353. * json_val_t *jvalue = json_insert_value( jobj, jpair, JSON_ARR, NULL );
  354. *
  355. * // Create the JSON-array and insert it into the above value
  356. * json_arr_t *jarr = json_create_arr( jobj, jval );
  357. *
  358. * Returns: json_val_t *, JSON-value
  359. **/
  360. json_val_t *json_insert_value( json_obj_t *jobj, void *ins_obj, json_types_t vtype, void *entry );
  361. /**
  362. * Function: json_destroy_value
  363. * Author: Joachim M. Giæver
  364. *
  365. * Parameters:
  366. * - json_val_t *jval: JSON-value to destroy
  367. *
  368. * Description:
  369. * Destroys a JSON-value. Works recursively on children.
  370. *
  371. * Returns: void
  372. **/
  373. void json_destroy_value( json_val_t *jval );
  374. /**
  375. * Function: json_has_err
  376. * Author: Joachim M. Giæver
  377. *
  378. * Parameters:
  379. * - json_obj_t *jobj: JSON-object which is the main-root
  380. *
  381. * Description:
  382. * Returns whether or not there was an error present on last event.
  383. *
  384. * Returns: int 0 on non errors 1 on errors
  385. **/
  386. int json_has_err( json_obj_t *jobj );
  387. /**
  388. * Function: json_last_err
  389. * Author: Joachim M. Giæver
  390. *
  391. * Parameters:
  392. * - json_obj_t *jobj: JSON-object which is the main-root
  393. *
  394. * Description:
  395. * Returns the last error code present.
  396. *
  397. * Returns: int, >= 0 on non errors <= 0 on errors
  398. **/
  399. int json_last_err( json_obj_t *jobj );
  400. /**
  401. * Function: json_last_errmsg
  402. * Author: Joachim M. Giæver
  403. *
  404. * Parameters:
  405. * - json_obj_t *jobj: JSON-object which is the main-root
  406. *
  407. * Description:
  408. * Returns the error message related to the error code
  409. * stored in the JSON-object (remember to check for errors; json_has_err).
  410. *
  411. * Returns: char *, error message
  412. **/
  413. const char *json_last_errmsg( json_obj_t *jobj );
  414. /**
  415. * Function:
  416. * Author: Joachim M. Giæver
  417. *
  418. * Parameters:
  419. * - json_obj_t *jobj: JSON-object which is the main-root
  420. *
  421. * Description:
  422. * Recursively prints the JSON-object.
  423. *
  424. * Returns: void
  425. **/
  426. void json_print( json_obj_t *jobj );
  427. #endif