[1] | 1 | #ifndef __JSON_H
|
---|
| 2 | #define __JSON_H
|
---|
| 3 |
|
---|
| 4 | #include "stm32f4xx_hal.h"
|
---|
| 5 | #include "main.h"
|
---|
| 6 | #include "ip_addr.h"
|
---|
| 7 |
|
---|
| 8 | #define JSON_WRITE_STACK_DEPTH 32
|
---|
| 9 | #define JSON_WRITE_COMPACT 0
|
---|
| 10 | #define JSON_WRITE_PRETTY 1
|
---|
| 11 |
|
---|
| 12 | enum json_node_type{
|
---|
| 13 | json_object_type = 1,
|
---|
| 14 | json_array_type
|
---|
| 15 | };
|
---|
| 16 |
|
---|
| 17 | struct json_write_node_stack{
|
---|
| 18 | enum json_node_type nodeType;
|
---|
| 19 | int elementNo;
|
---|
| 20 | };
|
---|
| 21 |
|
---|
| 22 | struct json_write_control{
|
---|
| 23 | char *buffer;
|
---|
| 24 | unsigned int buflen;
|
---|
| 25 | char *bufp;
|
---|
| 26 | char tmpbuf[32];
|
---|
| 27 | int error;
|
---|
| 28 | int callNo;
|
---|
| 29 | struct json_write_node_stack nodeStack[JSON_WRITE_STACK_DEPTH];
|
---|
| 30 | int stackpos;
|
---|
| 31 | int isPretty;
|
---|
| 32 | };
|
---|
| 33 |
|
---|
| 34 | typedef unsigned int json_size_t;
|
---|
| 35 |
|
---|
| 36 | typedef enum json_status {
|
---|
| 37 | json_unknown = 0x00, /**< Unknown status */
|
---|
| 38 | json_success = 0x01, /**< Last operation finished sucessfully. */
|
---|
| 39 | json_invalid_arguments = 0x02, /**< Invalid arguments were passed to the function. */
|
---|
| 40 | json_no_memory = 0x03, /**< Not enough tokens were provided. */
|
---|
| 41 | json_invalid_input = 0x04, /**< Invalid character in JSON string. */
|
---|
| 42 | json_error_part = 0x05, /**< The string is not a full JSON packet. More bytes expected. */
|
---|
| 43 | json_unknown_type = 0x06, /**< Unknown token type. */
|
---|
| 44 | json_not_found = 0x07, /**< Something was not found. */
|
---|
| 45 |
|
---|
| 46 | json_buf_full,
|
---|
| 47 | json_not_array,
|
---|
| 48 | json_not_object,
|
---|
| 49 | json_stack_full,
|
---|
| 50 | json_stack_empty,
|
---|
| 51 | json_nest_err
|
---|
| 52 | } json_status_t;
|
---|
| 53 |
|
---|
| 54 | typedef void *(*json_malloc_t)(json_size_t);
|
---|
| 55 | typedef void (*json_free_t)(void*);
|
---|
| 56 |
|
---|
| 57 | typedef struct json_config {
|
---|
| 58 | json_malloc_t json_malloc;
|
---|
| 59 | json_free_t json_free;
|
---|
| 60 | } json_config_t;
|
---|
| 61 |
|
---|
| 62 | typedef enum json_search_in_type_t {
|
---|
| 63 | json_root,
|
---|
| 64 | json_obj
|
---|
| 65 | } json_search_in_type_t;
|
---|
| 66 |
|
---|
| 67 | typedef enum json_token_type {
|
---|
| 68 | json_type_undefined = 0x00, /**< Undefined token type. */
|
---|
| 69 | json_type_null = 0x01, /**< Null token type. */
|
---|
| 70 | json_type_boolean = 0x02, /**< Boolean token type. */
|
---|
| 71 | json_type_integer = 0x03, /**< Integer token type. */
|
---|
| 72 | json_type_double = 0x04, /**< Double token type. */
|
---|
| 73 | json_type_string = 0x05, /**< String token type. */
|
---|
| 74 | json_type_array = 0x06, /**< Array token type. */
|
---|
| 75 | json_type_object = 0x07, /**< Object token type. */
|
---|
| 76 | } json_token_type_t;
|
---|
| 77 |
|
---|
| 78 | typedef struct {
|
---|
| 79 | char *json_data; /**< JSON string. */
|
---|
| 80 | json_size_t json_data_length;
|
---|
| 81 | }json_response_t;
|
---|
| 82 |
|
---|
| 83 | typedef struct json_token {
|
---|
| 84 | json_token_type_t type; /**< Token type. */
|
---|
| 85 | int start; /**< Token start position. */
|
---|
| 86 | int end; /**< Token end position. */
|
---|
| 87 | json_size_t size; /**< Token children count. */
|
---|
| 88 | }json_token_t;
|
---|
| 89 |
|
---|
| 90 | typedef struct json_tokens_data {
|
---|
| 91 | const char *json_data; /**< JSON string. */
|
---|
| 92 | json_token_t *tokens; /**< String parsing result in tokens. */
|
---|
| 93 | json_size_t tokens_count; /**< Tokens count. */
|
---|
| 94 | json_size_t current_token; /**< Index of current token. */
|
---|
| 95 | ip_addr_t *remote_addr;
|
---|
| 96 | } json_tokens_data_t;
|
---|
| 97 |
|
---|
| 98 | typedef json_token_type_t json_value_type_t;
|
---|
| 99 |
|
---|
| 100 | typedef struct json_parser {
|
---|
| 101 | json_size_t pos;
|
---|
| 102 | json_size_t next_token;
|
---|
| 103 | int superior_token;
|
---|
| 104 | const json_config_t *config;
|
---|
| 105 | } json_parser_t;
|
---|
| 106 |
|
---|
| 107 | typedef struct json_value json_value_t;
|
---|
| 108 |
|
---|
| 109 | typedef struct json_string {
|
---|
| 110 | char *data; /**< String bytes. */
|
---|
| 111 | json_size_t size; /**< Allocated bytes count. */
|
---|
| 112 | } json_string_t;
|
---|
| 113 |
|
---|
| 114 | typedef struct json_object_map {
|
---|
| 115 | json_string_t key; /**< Object key. */
|
---|
| 116 | json_value_t *value; /**< Oject value. */
|
---|
| 117 | } json_object_map_t;
|
---|
| 118 |
|
---|
| 119 | /** JSON array structure. */
|
---|
| 120 | typedef struct json_array {
|
---|
| 121 | json_value_t **items; /**< JSON items in array. */
|
---|
| 122 | json_size_t count; /**< Items count in array. */
|
---|
| 123 | } json_array_t;
|
---|
| 124 |
|
---|
| 125 | /** JSON object structure. */
|
---|
| 126 | typedef struct json_object {
|
---|
| 127 | json_object_map_t **items; /**< JSON items in object. */
|
---|
| 128 | json_size_t count; /**< Items count in object. */
|
---|
| 129 | } json_object_t;
|
---|
| 130 |
|
---|
| 131 | /** JFES value data union. */
|
---|
| 132 | typedef union json_value_data {
|
---|
| 133 | int bool_val; /**< Boolean JSON value. */
|
---|
| 134 | int int_val; /**< Integer JSON value. */
|
---|
| 135 | double double_val; /**< Double JSON value. */
|
---|
| 136 | json_string_t string_val; /**< String JSON value. */
|
---|
| 137 | json_array_t *array_val; /**< Array JSON value. */
|
---|
| 138 | json_object_t *object_val; /**< Object JSON value. */
|
---|
| 139 | } json_value_data_t;
|
---|
| 140 |
|
---|
| 141 | struct json_value {
|
---|
| 142 | json_value_type_t type; /**< JSON value type. */
|
---|
| 143 | json_value_data_t data; /**< Value data. */
|
---|
| 144 | };
|
---|
| 145 |
|
---|
| 146 | typedef struct {
|
---|
| 147 | json_value_type_t type;
|
---|
| 148 | int bool_val;
|
---|
| 149 | int int_val;
|
---|
| 150 | double double_val;
|
---|
| 151 | json_string_t string_val;
|
---|
| 152 | }valType_t;
|
---|
| 153 |
|
---|
| 154 | void json_write_open(char *buffer, unsigned int buflen, enum json_node_type rootType, int isPretty);
|
---|
| 155 | int json_write_close(void);
|
---|
| 156 | void json_write_obj_string( char *key, char *value );
|
---|
| 157 | void json_write_obj_int( char *key, int value );
|
---|
| 158 | void json_write_obj_double( char *key, double value );
|
---|
| 159 | void json_write_obj_bool( char *key, int oneOrZero );
|
---|
| 160 | void json_write_obj_null( char *key );
|
---|
| 161 | void json_write_obj_object(char *key);
|
---|
| 162 | void json_write_obj_array( char *key );
|
---|
| 163 | void json_write_arr_string( char *value );
|
---|
| 164 | void json_write_arr_int( int value );
|
---|
| 165 | void json_write_arr_double( double value );
|
---|
| 166 | void json_write_arr_bool( int oneOrZero );
|
---|
| 167 | void json_write_arr_null(void);
|
---|
| 168 | void json_write_arr_object(void);
|
---|
| 169 | void json_write_arr_array(void);
|
---|
| 170 | int json_write_end(void);
|
---|
| 171 |
|
---|
| 172 |
|
---|
| 173 | json_status_t json_get_value_array(json_tokens_data_t *tokens_data, void *value, uint8_t index, json_token_type_t type, json_search_in_type_t in);
|
---|
| 174 | json_status_t json_get_value_object(json_tokens_data_t *tokens_data, const char *Key, void *value, json_token_type_t type, json_search_in_type_t in);
|
---|
| 175 | //json_status_t json_get_object(json_tokens_data_t *tokens_data, const char *Key, json_tokens_data_t *retVal);
|
---|
| 176 | json_status_t json_init_parser(json_parser_t *parser, const json_config_t *config);
|
---|
| 177 | json_status_t json_parse_tokens(json_parser_t *parser, const char *json, json_size_t length, json_token_t *tokens, json_size_t *max_tokens_count);
|
---|
| 178 |
|
---|
| 179 | #endif /* __JSON_H */
|
---|