source: S-port/trunk/Core/Inc/JSON.h@ 1

Last change on this file since 1 was 1, checked in by AlexLir, 3 years ago
File size: 6.9 KB
Line 
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
12enum json_node_type{
13 json_object_type = 1,
14 json_array_type
15};
16
17struct json_write_node_stack{
18 enum json_node_type nodeType;
19 int elementNo;
20};
21
22struct 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
34typedef unsigned int json_size_t;
35
36typedef 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
54typedef void *(*json_malloc_t)(json_size_t);
55typedef void (*json_free_t)(void*);
56
57typedef struct json_config {
58 json_malloc_t json_malloc;
59 json_free_t json_free;
60} json_config_t;
61
62typedef enum json_search_in_type_t {
63 json_root,
64 json_obj
65} json_search_in_type_t;
66
67typedef 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
78typedef struct {
79 char *json_data; /**< JSON string. */
80 json_size_t json_data_length;
81}json_response_t;
82
83typedef 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
90typedef 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
98typedef json_token_type_t json_value_type_t;
99
100typedef 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
107typedef struct json_value json_value_t;
108
109typedef struct json_string {
110 char *data; /**< String bytes. */
111 json_size_t size; /**< Allocated bytes count. */
112} json_string_t;
113
114typedef 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. */
120typedef 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. */
126typedef 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. */
132typedef 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
141struct json_value {
142 json_value_type_t type; /**< JSON value type. */
143 json_value_data_t data; /**< Value data. */
144};
145
146typedef 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
154void json_write_open(char *buffer, unsigned int buflen, enum json_node_type rootType, int isPretty);
155int json_write_close(void);
156void json_write_obj_string( char *key, char *value );
157void json_write_obj_int( char *key, int value );
158void json_write_obj_double( char *key, double value );
159void json_write_obj_bool( char *key, int oneOrZero );
160void json_write_obj_null( char *key );
161void json_write_obj_object(char *key);
162void json_write_obj_array( char *key );
163void json_write_arr_string( char *value );
164void json_write_arr_int( int value );
165void json_write_arr_double( double value );
166void json_write_arr_bool( int oneOrZero );
167void json_write_arr_null(void);
168void json_write_arr_object(void);
169void json_write_arr_array(void);
170int json_write_end(void);
171
172
173json_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);
174json_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);
176json_status_t json_init_parser(json_parser_t *parser, const json_config_t *config);
177json_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 */
Note: See TracBrowser for help on using the repository browser.