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.

  • This is a streaming JSON parser with the primary use case of low memory devices where even a medium sized JSON string can not

be processed.

json calls

Function

Description

rs_json_init_obj

Function to initialize the JSON object

rs_json_process_input_string

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_get_element_addr

Function to extract pointer to an array element

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:

JSON Parser Module.

This file contains the APIs for using JSON Parser feature

Author

Embien RAPIDSEA Team

Copyright

Embien Technologies India Pvt. Ltd.

Defines

RS_JSON_DATA_TYPE_INVALID

JSON Value Numeric Data type.

RS_JSON_DATA_TYPE_INT8U
RS_JSON_DATA_TYPE_INT8S
RS_JSON_DATA_TYPE_INT16U
RS_JSON_DATA_TYPE_INT16S
RS_JSON_DATA_TYPE_INT32U
RS_JSON_DATA_TYPE_INT32S
RS_JSON_DATA_TYPE_INT64U
RS_JSON_DATA_TYPE_INT64S
RS_JSON_DATA_TYPE_FLOAT
RS_JSON_DATA_TYPE_DOUBLE
RS_JSON_DATA_TYPE_STRING
RS_JSON_DATA_TYPE_BOOLEAN
RS_JSON_DATA_TYPE_OBJECT
RS_JSON_ARRAY_TYPE_STATIC
RS_JSON_ARRAY_TYPE_DYNAMIC
RS_JSON_DATA_TYPE_MASK
RS_JSON_ARRAY_TYPE_MASK
RS_JSON_DEBUG_MSGS
RS_JSON_MAX_KEY_VALUE_SIZE

Size of the JSON key and value data buffer.

RS_JSON_MAX_ARRAY_DEPTH

Maximum array depth in JSON message that can be handled.

JSON_OBJ_BASIC_DATA_TYPE_OFFSET(type, element)

Offset of the basic data variable in the structure.

JSON_OBJ_STRING_DATA_TYPE_OFFSET(type, element, max_len)
JSON_OBJ_OBJECT_DATA_TYPE_OFFSET(type, element, attr)
JSON_OBJ_STATIC_ARRAY_TYPE_INFO(type, element, count_var, max_count, element_size)
JSON_OBJ_DYNAMIC_ARRAY_TYPE_INFO(type, element, count_var, max_count, element_size)
JSON_OBJ_OFFSET_TO_ARRAY_OFFSET(type, element, count_var, target_element)
JSON_OBJ_ATTRIBUTE(x)
JSON_STRUCT_OFFSET(type, element, subtype, obj_count, struct1)

Typedefs

typedef struct tag_rs_json_attr_t rs_json_attr_t

Structure to hold the attribute of the individual elements in the JSON message.

typedef struct tag_rs_json_obj_t rs_json_config_t

Structure contains the JSON object details and is used to parse.

Functions

rs_ret_val_t rs_json_init_obj(rs_json_config_t *ptr_json_obj_inst, rs_json_attr_t *ptr_attrs, void *ptr_load_addr, uint32_t u32_buf_size, uint32_t u32_obj_size)
rs_ret_val_t rs_json_process_input_string(rs_json_config_t *ptr_json_obj, uint8_t *ptr_json_data, uint32_t u32_json_msg_len)
rs_ret_val_t rs_json_output_to_string(rs_json_config_t *ptr_json_obj, void *ptr_json_src_buf, char *ptr_json_out_buf, uint32_t u32_max_str_len)
void *rs_json_get_element_addr(rs_json_config_t *ptr_json_obj_inst, uint32_t u32_array_offset, uint32_t u32_element_index)
struct tag_rs_json_attr_t
#include <rs_json.h>

Structure to hold the attribute of the individual elements in the JSON message.

Public Members

char *ptr_attr_name

Name of the JSON key attribute.

uint8_t data_type

Data type by which the JSON value has to be handled.

int32_t obj_count_var_offset

Offset to the array count variable relative to the array element. Mostly negative.

uint32_t target_element_size

Target element size. Used to calculate array element offset.

int32_t len

Variable of string length for JSON string attribute.

uint32_t offset

Byte offset from base pointer from where the value is stored.

struct tag_rs_json_attr_t *ptr_attr

Pointer to store corresponding attribute info.

struct tag_rs_json_obj_t
#include <rs_json.h>

Structure contains the JSON object details and is used to parse.

Public Members

uint8_t *ptr_cur_obj_addr

Starting address of the current object being worked.

uint8_t *ptr_obj_base_addr

Starting address of the based memory to store the entire JSON object.

uint32_t buffer_size

Size of the buffer given to store objects and related info.

uint8_t process_state

Holds the current state of message parsing.

uint8_t process_sub_state

Holds the sub state of message parsing.

uint8_t data_buff[RS_JSON_MAX_KEY_VALUE_SIZE]

Buffer to store the JSON Key and Value.

uint8_t data_index

Holds the index to the buffer.

uint8_t attibute_level

Holds the current level of attribute being parsed by the object mapper. 0 index holds the user given value

uint8_t return_process_state

To pass the state to return to.

uint8_t return_process_sub_state

To pass the substate to return to.

rs_json_attr_t *ptr_json_root_attr

Pointer to the base attribute.

struct tag_rs_json_obj_t::tag_rs_json_parse_data json_parse_data_t[RS_JSON_MAX_ARRAY_DEPTH]
uint32_t used_buffer_size

Buffer size consumed so far.

char *ptr_out_addr

Next pointer to output the JSON string.

uint32_t object_alloc_offset

Offset to the next object allocation area.

uint32_t array_alloc_offset

Offset to the last object allocation area.

uint32_t move_start_offset

Offset(excluding) below which other offset are to be moved.

uint32_t move_size

Number of bytes to move.

uint8_t move_attibute_level

Holds the current level of attribute being parsed by the object mapper. 0 index holds the user given value

struct tag_rs_json_obj_t::tag_rs_json_move_parse_data json_move_parse_data_t[RS_JSON_MAX_ARRAY_DEPTH]
struct tag_rs_json_parse_data
#include <rs_json.h>

Structure to hold information of attributes as and when the depth of the message increases.

Public Members

rs_json_attr_t *ptr_json_attr

Pointer to store starting address of the attribute.

uint8_t *ptr_object_store_addr

Holding the address of the variable to store to.

uint32_t *ptr_obj_count_addr

To store the count of the object/array being read.

uint16_t element_size

Hold the base element size of each array element.

uint8_t return_process_state

Holds the return state to gone once this level is done.

uint8_t return_process_sub_state

Holds the return substate to gone once this level is done.

uint8_t data_type

Holds data type of the JSON value data being processed.

uint8_t push_type

Holds the type of push.

char reason[50]

Holds the reason of this push. For debug.

struct tag_rs_json_move_parse_data
#include <rs_json.h>

Structure to hold information of attributes as and when the depth of the message increases.

Public Members

rs_json_attr_t *ptr_json_attr

Pointer to store starting address of the attribute.

uint8_t *ptr_object_store_addr

Holding the address of the variable to store to.