#ifndef __JSON_H #define __JSON_H #include "stm32f4xx_hal.h" #include "main.h" #include "ip_addr.h" #define JSON_WRITE_STACK_DEPTH 32 #define JSON_WRITE_COMPACT 0 #define JSON_WRITE_PRETTY 1 enum json_node_type{ json_object_type = 1, json_array_type }; struct json_write_node_stack{ enum json_node_type nodeType; int elementNo; }; struct json_write_control{ char *buffer; unsigned int buflen; char *bufp; char tmpbuf[32]; int error; int callNo; struct json_write_node_stack nodeStack[JSON_WRITE_STACK_DEPTH]; int stackpos; int isPretty; }; typedef unsigned int json_size_t; typedef enum json_status { json_unknown = 0x00, /**< Unknown status */ json_success = 0x01, /**< Last operation finished sucessfully. */ json_invalid_arguments = 0x02, /**< Invalid arguments were passed to the function. */ json_no_memory = 0x03, /**< Not enough tokens were provided. */ json_invalid_input = 0x04, /**< Invalid character in JSON string. */ json_error_part = 0x05, /**< The string is not a full JSON packet. More bytes expected. */ json_unknown_type = 0x06, /**< Unknown token type. */ json_not_found = 0x07, /**< Something was not found. */ json_buf_full, json_not_array, json_not_object, json_stack_full, json_stack_empty, json_nest_err } json_status_t; typedef void *(*json_malloc_t)(json_size_t); typedef void (*json_free_t)(void*); typedef struct json_config { json_malloc_t json_malloc; json_free_t json_free; } json_config_t; typedef enum json_search_in_type_t { json_root, json_obj } json_search_in_type_t; typedef enum json_token_type { json_type_undefined = 0x00, /**< Undefined token type. */ json_type_null = 0x01, /**< Null token type. */ json_type_boolean = 0x02, /**< Boolean token type. */ json_type_integer = 0x03, /**< Integer token type. */ json_type_double = 0x04, /**< Double token type. */ json_type_string = 0x05, /**< String token type. */ json_type_array = 0x06, /**< Array token type. */ json_type_object = 0x07, /**< Object token type. */ } json_token_type_t; typedef struct { char *json_data; /**< JSON string. */ json_size_t json_data_length; }json_response_t; typedef struct json_token { json_token_type_t type; /**< Token type. */ int start; /**< Token start position. */ int end; /**< Token end position. */ json_size_t size; /**< Token children count. */ }json_token_t; typedef struct json_tokens_data { const char *json_data; /**< JSON string. */ json_token_t *tokens; /**< String parsing result in tokens. */ json_size_t tokens_count; /**< Tokens count. */ json_size_t current_token; /**< Index of current token. */ ip_addr_t *remote_addr; } json_tokens_data_t; typedef json_token_type_t json_value_type_t; typedef struct json_parser { json_size_t pos; json_size_t next_token; int superior_token; const json_config_t *config; } json_parser_t; typedef struct json_value json_value_t; typedef struct json_string { char *data; /**< String bytes. */ json_size_t size; /**< Allocated bytes count. */ } json_string_t; typedef struct json_object_map { json_string_t key; /**< Object key. */ json_value_t *value; /**< Oject value. */ } json_object_map_t; /** JSON array structure. */ typedef struct json_array { json_value_t **items; /**< JSON items in array. */ json_size_t count; /**< Items count in array. */ } json_array_t; /** JSON object structure. */ typedef struct json_object { json_object_map_t **items; /**< JSON items in object. */ json_size_t count; /**< Items count in object. */ } json_object_t; /** JFES value data union. */ typedef union json_value_data { int bool_val; /**< Boolean JSON value. */ int int_val; /**< Integer JSON value. */ double double_val; /**< Double JSON value. */ json_string_t string_val; /**< String JSON value. */ json_array_t *array_val; /**< Array JSON value. */ json_object_t *object_val; /**< Object JSON value. */ } json_value_data_t; struct json_value { json_value_type_t type; /**< JSON value type. */ json_value_data_t data; /**< Value data. */ }; typedef struct { json_value_type_t type; int bool_val; int int_val; double double_val; json_string_t string_val; }valType_t; void json_write_open(char *buffer, unsigned int buflen, enum json_node_type rootType, int isPretty); int json_write_close(void); void json_write_obj_string( char *key, char *value ); void json_write_obj_int( char *key, int value ); void json_write_obj_double( char *key, double value ); void json_write_obj_bool( char *key, int oneOrZero ); void json_write_obj_null( char *key ); void json_write_obj_object(char *key); void json_write_obj_array( char *key ); void json_write_arr_string( char *value ); void json_write_arr_int( int value ); void json_write_arr_double( double value ); void json_write_arr_bool( int oneOrZero ); void json_write_arr_null(void); void json_write_arr_object(void); void json_write_arr_array(void); int json_write_end(void); 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); 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); //json_status_t json_get_object(json_tokens_data_t *tokens_data, const char *Key, json_tokens_data_t *retVal); json_status_t json_init_parser(json_parser_t *parser, const json_config_t *config); 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); #endif /* __JSON_H */