JSON Parser
Using json_init_obj initialize the json by giving json obj as an argument.
Parse the json input data in byte by byte.
Function |
Description |
---|---|
rs_json_init_obj |
Function to initialize the JSON object |
rs_json_process_input |
Function to parse the JSON input data in byte by byte |
rs_json_output_to_string |
Function to generates the JSON string from the structure data according to the input string length |
rs_json_reset |
Function to reset the JSON object |
Using these ‘rs’ calls set the input required for the json file, call the function ‘rs_json_process_input’ it will parse the data in byte by byte and store in to structure.
Example Demo
Please refer below section,
JSON Parser Header Details
Documentation from the relevant header as follows:
Defines
-
RS_JSON_DATA_TYPE_CHAR
JSON Value character Data type.
-
RS_JSON_DATA_TYPE_USHORT
JSON Value unsigned short integer Data type.
-
RS_JSON_DATA_TYPE_SHORT
JSON Value signed short integer Data type.
-
RS_JSON_DATA_TYPE_UINT
JSON Value unsigned integer Data type.
-
RS_JSON_DATA_TYPE_INT
JSON Value signed integer Data type.
-
RS_JSON_DATA_TYPE_STRING
JSON Value string Data type.
-
RS_JSON_DATA_TYPE_ARRAY
JSON Value array Data type.
-
RS_JSON_DATA_BUFF_SIZE
Size of the JSON key and value data buffer.
-
RS_JSON_MAX_ARRAY_DEPTH
Maximum array depth in JSON message.
-
RS_JSON_MAX_ELEMENTS_IN_INITIAL_STRUCT
The initial structure of the JSON message contains the maximum elements.
-
STRUCTARRAY(type, element, subtype, count)
JSON structure variables details.
-
MEMBEROFFSET(type, element)
Offset of the variable in the structure.
Typedefs
-
typedef struct tag_rs_json_data_objects_t rs_json_objects_t
Structure to store the array details in the JSON message.
-
typedef struct tag_rs_json_strings_t rs_json_strings_t
Structure to store the string value in the JSON message.
-
typedef struct tag_rs_json_int_t rs_json_int_t
Structure to store the integer value in the JSON message.
-
typedef struct tag_rs_json_uint_t rs_json_uint_t
Structure to store the unsigned integer value in the JSON message.
-
typedef struct tag_rs_json_short_t rs_json_short_t
Structure to store the short integer value in the JSON message.
-
typedef struct tag_rs_json_ushort_t rs_json_ushort_t
Structure to store the short unsigned integer value in the JSON message.
-
typedef struct tag_rs_json_real_t rs_json_real_t
Structure to store the real value in the JSON message.
-
typedef struct tag_rs_json_array_t rs_json_array_t
Structure to hold the information about array in the JSON message.
-
typedef struct tag_rs_json_attr_t rs_json_attr_t
Structure to hold the attribute of the individual objects in the JSON message.
-
typedef struct tag_rs_json_arr_attr_info_t rs_json_arr_attr_info_t
Structure to hold the starting address of the array object.
-
typedef struct tag_rs_json_obj_t rs_json_obj_t
Structure contains the complete JSON object details.
Functions
-
rs_ret_val_t rs_json_init_obj(rs_json_obj_t *ptr_json_obj_inst, rs_json_attr_t *ptr_attrs, void *ptr_obj_storage, uint32_t u32_max_obj_size)
Function to initialize the JSON object.
This function initializes the JSON object for further JSON parsing operation
- Parameters:
ptr_json_obj_inst – [in] - Pointer to JSON object instance
ptr_attrs – [in] - Pointer to the starting address of JSON attribute
ptr_obj_storage – [in] - Pointer to the user defined storage
u32_max_obj_size – [in] - Maximum size of the user object
- Returns:
0 on success or error code on failure
-
rs_ret_val_t rs_json_process_input(rs_json_obj_t *ptr_json_obj, uint8_t *ptr_json_msg_buf, uint32_t u32_json_msg_len)
Function to parse the JSON input data.
This function stores the JSON value in the appropriate variable after parsing the JSON input data.
- Parameters:
ptr_json_obj – [in] - Pointer to the JSON object
ptr_json_msg_buf – [in] - Pointer to JSON message buffer
u32_json_msg_len – [in] - JSON message length
- Returns:
0 on success or error code on failure
-
rs_ret_val_t rs_json_output_to_string(rs_json_obj_t *ptr_json_obj, uint8_t *ptr_json_out_buf, uint32_t u32_max_str_len)
Function to output JSON string for the given object.
This function outputs JSON formatted string for the object
- Parameters:
ptr_json_obj – [in] - Pointer to the JSON object
ptr_json_out_buf – [in] - Pointer to JSON output buffer
u32_max_str_len – [in] - Maximum JSON string length
- Returns:
0 on success or error code on failure
-
rs_ret_val_t rs_json_reset(rs_json_obj_t *ptr_json_obj)
Function to reset the JSON object variables.
This function reset the JSON object variables to zero, if the JSON data was invalid
- Parameters:
ptr_json_obj – [in] - Pointer to JSON object
- Returns:
0 on success or error code on failure
-
struct tag_rs_json_data_objects_t
- #include <rs_json.h>
Structure to store the array details in the JSON message.
Public Members
-
struct rs_json_attr_t *ptr_subtype
Pointer to sub array.
-
uint8_t *ptr_base
Pointer to base.
-
uint32_t stride
Variable of stride.
-
struct rs_json_attr_t *ptr_subtype
-
struct tag_rs_json_strings_t
- #include <rs_json.h>
Structure to store the string value in the JSON message.
-
struct tag_rs_json_int_t
- #include <rs_json.h>
Structure to store the integer value in the JSON message.
Public Members
-
int32_t *ptr_data
Pointer data to signed integer.
-
int32_t *ptr_data
-
struct tag_rs_json_uint_t
- #include <rs_json.h>
Structure to store the unsigned integer value in the JSON message.
Public Members
-
uint32_t *ptr_data
Pointer data to unsigned integer.
-
uint32_t *ptr_data
-
struct tag_rs_json_short_t
- #include <rs_json.h>
Structure to store the short integer value in the JSON message.
Public Members
-
int16_t *ptr_data
Pointer data to signed short integer.
-
int16_t *ptr_data
-
struct tag_rs_json_ushort_t
- #include <rs_json.h>
Structure to store the short unsigned integer value in the JSON message.
Public Members
-
uint16_t *ptr_data
Pointer data to unsigned short integer.
-
uint16_t *ptr_data
-
struct tag_rs_json_real_t
- #include <rs_json.h>
Structure to store the real value in the JSON message.
Public Members
-
uint64_t *ptr_data
Pointer data to real.
-
uint64_t *ptr_data
-
struct tag_rs_json_array_t
- #include <rs_json.h>
Structure to hold the information about array in the JSON message.
Public Members
-
uint8_t element_type
Variable of Element data type.
-
int32_t offset
Variable of offset value.
-
rs_json_objects_t objects
Variable of objects structure.
-
rs_json_strings_t strings
Variable of strings structure.
-
rs_json_int_t integer
Variable of signed integer structure.
-
rs_json_uint_t uinteger
Variable of unsigned integer structure.
-
rs_json_short_t shortint
Variable of signed short integer structure.
-
rs_json_ushort_t ushortint
Variable of unsigned short integer structure.
-
rs_json_real_t real
Variable of real structure.
-
union tag_rs_json_array_t arr
-
uint32_t obj_count_offset
Variable to store the offset of array object count.
-
uint32_t maxlen
Variable of maximum object count in each array.
-
uint8_t element_type
-
struct tag_rs_json_attr_t
- #include <rs_json.h>
Structure to hold the attribute of the individual objects in the JSON message.
Public Members
-
char *ptr_attribute
Pointer to store JSON key attribute.
-
uint8_t data_type
Variable of JSON key attribute data type.
-
int32_t *ptr_integer
Pointer to signed integer.
-
uint32_t *ptr_uinteger
Pointer to unsigned integer.
-
int16_t *ptr_shortint
Pointer to signed short integer.
-
uint16_t *ptr_ushortint
Pointer to unsigned short integer.
-
uint64_t *ptr_real
Pointer to double.
-
uint8_t *ptr_string
Pointer to string.
-
uint8_t *ptr_character
Pointer to character.
-
struct rs_json_attr_t *ptr_attrs
Pointer variable of JSON attribute structure.
-
rs_json_array_t array
Variable of JSON array structure.
-
int32_t offset
Variable of offset value.
-
union tag_rs_json_attr_t addr
-
int32_t integer
Variable of signed integer.
-
uint32_t uinteger
Variable of unsigned integer.
-
int16_t shortint
Variable of signed short integer.
-
uint16_t ushortint
Variable of unsigned short integer.
-
uint64_t real
Variable of double.
-
uint8_t character
Variable of character.
-
uint8_t *ptr_check
Pointer to check.
-
union tag_rs_json_attr_t dflt
-
int32_t len
Variable of string length for JSON string attribute.
-
char *ptr_attribute
-
struct tag_rs_json_arr_attr_info_t
- #include <rs_json.h>
Structure to hold the starting address of the array object.
Public Members
-
void *ptr_current_json_attr
Void pointer to store starting address of the array object.
-
void *ptr_current_json_attr
-
struct tag_rs_json_obj_t
- #include <rs_json.h>
Structure contains the complete JSON object details.
Public Members
-
void *ptr_obj
Starting address of the user structure.
-
uint32_t max_object_size
Variable of object size - future use.
-
rs_json_attr_t *ptr_json_attr
Starting address of the user structure used for parse the JSON message.
-
uint8_t data_buff[RS_JSON_DATA_BUFF_SIZE]
Buffer to store the JSON Key and Value.
-
int32_t arr_offset[RS_JSON_MAX_ARRAY_DEPTH]
Array contains the individual array offset from starting address.
-
uint32_t count[RS_JSON_MAX_ARRAY_DEPTH]
Holding the array count variables offset.
-
int32_t next_obj_offset[RS_JSON_MAX_ARRAY_DEPTH]
Holding the offset of the next object in the array.
-
uint32_t max_len[RS_JSON_MAX_ARRAY_DEPTH]
Holding the maximum length of each array in the JSON message.
-
uint32_t flag
Holding JSON Message status flag.
-
uint32_t data_index
Holding JSON Message index.
-
uint32_t offset_value
Holding the Offset of the JSON value to be written.
-
uint8_t data_type
Holding Data type of the JSON key data.
-
uint8_t array_count
Holding array count.
-
uint8_t array_depth
Holding array depth.
-
void *ptr_curr_base[RS_JSON_MAX_ARRAY_DEPTH]
Holding starting address of the JSON input data to string output data.
-
uint32_t u32_curr_depth
Holding current array depth.
-
uint32_t u32_num_bytes
Holding the number of bytes copied into the user output buffer.
-
uint8_t u8_attr_index
Holding the index of the attributes in the JSON object.
-
rs_json_arr_attr_info_t json_arr_attr_info[RS_JSON_MAX_ARRAY_DEPTH]
Holding the starting address of the array in JSON.
-
void *ptr_obj