hat-util  0.6.16
Utility library
json.h
Go to the documentation of this file.
1 #ifndef HAT_JSON_H
2 #define HAT_JSON_H
3 
4 #include <stdbool.h>
5 #include <stdint.h>
6 #include "allocator.h"
7 #include "buff.h"
8 
9 #ifndef HAT_JSON_MAX_DEPTH
10 #define HAT_JSON_MAX_DEPTH 1024
11 #endif
12 
13 #define HAT_JSON_SUCCESS 0
14 #define HAT_JSON_ERROR (-1)
15 
16 #define HAT_JSON_TOKEN_NULL 0
17 #define HAT_JSON_TOKEN_BOOL 1
18 #define HAT_JSON_TOKEN_INT 2
19 #define HAT_JSON_TOKEN_REAL 3
20 #define HAT_JSON_TOKEN_STR 4
21 #define HAT_JSON_TOKEN_ARR 5
22 #define HAT_JSON_TOKEN_ARR_END 6
23 #define HAT_JSON_TOKEN_OBJ 7
24 #define HAT_JSON_TOKEN_OBJ_KEY 8
25 #define HAT_JSON_TOKEN_OBJ_END 9
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 typedef int hat_json_error_t;
32 
33 typedef int hat_json_token_t;
34 
35 typedef union {
36  bool bool_value;
37  int64_t int_value;
38  double real_value;
39  struct {
40  char *data;
41  size_t len;
42  } str_value;
44 
45 // in case of HAT_JSON_TOKEN_ARR, HAT_JSON_TOKEN_OBJ and
46 // HAT_JSON_TOKEN_OBJ_KEY, return value is used as ctx for child tokens
47 typedef void *(*hat_json_parser_cb_t)(hat_json_token_t *token,
48  hat_json_value_t *value, void *ctx);
49 
50 typedef hat_json_error_t (*hat_json_writer_cb_t)(char *data, size_t len,
51  void *ctx);
52 
54 
56 
57 
59  hat_json_parser_cb_t cb, void *ctx);
63 
65  hat_json_writer_cb_t cb, void *ctx);
70 void hat_json_writer_write_str(hat_json_writer_t *w, char *value, size_t len);
75 
76 #ifdef __cplusplus
77 }
78 #endif
79 
80 #endif
Memory allocator.
Data buffer.
void hat_json_writer_write_end(hat_json_writer_t *w)
void hat_json_writer_write_str(hat_json_writer_t *w, char *value, size_t len)
hat_json_parser_t * hat_json_parser_create(hat_allocator_t *a, hat_json_parser_cb_t cb, void *ctx)
void *(* hat_json_parser_cb_t)(hat_json_token_t *token, hat_json_value_t *value, void *ctx)
Definition: json.h:47
void hat_json_parser_destroy(hat_json_parser_t *p)
int hat_json_error_t
Definition: json.h:31
void hat_json_writer_destroy(hat_json_writer_t *w)
void hat_json_writer_write_arr(hat_json_writer_t *w)
void hat_json_writer_write_real(hat_json_writer_t *w, double value)
void hat_json_writer_write_int(hat_json_writer_t *w, int64_t value)
struct hat_json_parser_t hat_json_parser_t
Definition: json.h:53
hat_json_error_t hat_json_parser_parse(hat_json_parser_t *p, hat_buff_t *buff)
void hat_json_writer_write_bool(hat_json_writer_t *w, bool value)
void hat_json_writer_write_obj(hat_json_writer_t *w)
hat_json_writer_t * hat_json_writer_create(hat_allocator_t *a, hat_json_writer_cb_t cb, void *ctx)
int hat_json_token_t
Definition: json.h:33
bool hat_json_parser_empty(hat_json_parser_t *p)
void hat_json_writer_write_null(hat_json_writer_t *w)
hat_json_error_t(* hat_json_writer_cb_t)(char *data, size_t len, void *ctx)
Definition: json.h:50
struct hat_json_writer_t hat_json_writer_t
Definition: json.h:55
Allocator base struct.
Definition: allocator.h:36
Data buffer.
Definition: buff.h:28
Definition: json.h:35
double real_value
Definition: json.h:38
char * data
Definition: json.h:40
size_t len
Definition: json.h:41
bool bool_value
Definition: json.h:36
int64_t int_value
Definition: json.h:37