JSON Parser Demo

Overview

There is an example implementation of JSON available in the apps folder. In this implementation, JSON input data is parsed byte by byte and stored in a user structure.

Example Usecase

Example JSON data:

{
    "interface_list": [
        {
            "interface_id": 1,
            "port_id": 0,
            "num_device": 4,
            "protocol": 0,
            "baud_rate": 9600,
            "data_bits": 8,
            "parity": 0,
            "stop_bits": 1,
            "interface_type": 0,
            "flow_ctrl": 0,
            "device_list": [
                {
                    "device_name": "FM1",
                    "label": "Zdfc",
                    "profile": "FlowMeter",
                    "interface": 1,
                    "data_list": [
                        {
                            "slave_id": 154,
                            "starting_reg_addr": 3000,
                            "data_type": 6,
                            "req_type": 4,
                            "data": "absolute_massflow",
                            "unit": "kg/s"
                        }
                    ]
                }
            ]
        }
    ],
    "cloud_list": [
        {
            "cloud_id": "precisol",
            "url": "http://cloud.precisol-automation.com/api/v1/",
            "start_time": "2024-03-01:05:00:00",
            "interval_type": 0,
            "interval": 1,
            "enable_signal": 0
        }
    ]
}

rs_json_init_obj - This function initializes the JSON object for further JSON parsing operation.

rs_json_process_input - This function reads the JSON input message byte-by-byte and parses the key and value data from it. If a mismatch between the key data in the JSON input message and the key data configured in the JSON object structure is found, the entire JSON data will be marked as corrupted, and the user will need to reset the JSON object once before processing the next JSON message.

Example Parsed JSON Data:

Interface_Count:  1

************Client Interface Details******************
****Client Interface Details [0]****

Interface_ID [0] : 1
Port_ID [0] : 0
Num_Devices [0] : 4
Protocol [0] : 0
Baud_Rate [0] : 9600
Data_Bits [0] : 8
Parity [0] : 0
Stop_Bits [0] : 1
Interface_type [0] : 0
Flow_Ctrl [0] : 0

Device_Count:  1

************Client Device Details******************
****Client Device Details [0]****
        Device_Name[0] :FM1
        Label[0] :Zdfc
        Profile[0] :FlowMeter
        Interface[0] :1

Data_Count[0]:  1

************Client Request Details******************
****Client Request Details[0]****
                Slave_ID[0] :154
                Starting_Reg_Addr[0] :3000
                Data_Type[0] :6
                Req_Type[0] :4
                Data[0] :absolute_massflow
                Unit[0] :kg/s


Cloud_Count:  1

****Cloud Configuration Details****
****Cloud Configuration Details [0]****

Cloud_ID [0] : precisol
URL [0] : http//cloud.precisol-automation.2024-03-01050000
Start_time [0] : 2024-03-01050000
Interval_Type [0] : 0
Interval [0] : 1
Enable_Signal [0] : 0

JSON data parsed successfully!...

rs_json_reset - This function resets the JSON object to zero. If rs_json_process_input returns RS_ERR_JSON_MSG_CORRUPTED(-33) error, then it is required to reset the JSON object once before processing the next JSON message.

rs_json_output_to_string - This function generates the JSON string from the structure data according to the input string length.

Sample String Data output:

JSON to String output data : {"interface_list":[{"interface_id": 1,"port_id": 0,"num_device": 4,"protocol": 0,"baud_rate": 9600,

JSON Module Driver

Refer to the JSON Parser page for an example about how the RAPIDSEA uses the json file.