WriteAsync (Method)

Overview

Type:

Method

Available as of:

V1.5.4.0

Functional Description

This method is used for asynchronous creating and writing of JSON-formatted data. The execution can take several cycles. Prerequisite is that the data has been parsed successfully. Refer to Parse (Method). While writing is in progress, no further methods or properties of the function block can be processed.

The global parameter GPL.Gc_udiJsonMaxNumOfBytesPerCycle specifies the number of bytes processed in one cycle.

The writing is completed if one of the outputs q_xDone or q_xError indicates TRUE. You must cyclically call the method while the output q_xBusy is TRUE.

The diagnostic outputs indicate if the execution has been processed successfully. If there is a detected error refer to the properties Result and ResultMsg for details.

NOTE: If you write a partly parsed JSON string back to the root buffer, the unparsed part of the JSON string is lost.

Interface

Input

Data type

Description

i_anyBufferToWrite

ANY

Buffer allocated in the application.

Output

Data type

Description

q_xBusy

BOOL

If this output is set to TRUE, the method execution is in progress.

q_xDone

BOOL

If this output is set to TRUE, the method execution has been completed successfully.

q_xError

BOOL

If this output is set to TRUE, an error has been detected. For details, refer to q_etResult and q_etResultMsg.

NOTE: For performance reasons the validity of the input parameters of the function blocks is verified only in the first cycle after triggering the method execution. Do not modify these values while the function block is executed. By executing this method, a previously detected error indicated by the corresponding properties and information related to previous writing operation is reset.

Example

The following example indicates how to implement a parse process, a modification of one value out of the parsed JSON-formatted string, and an asynchronous writing:

PROGRAM SR_Main_Async
VAR
    iState : INT;

    xWriteModifiedJsonString : BOOL;
    sCountry : STRING := 'Deutschland';

    sJsonString : STRING[500] := '{"Library": "FileFormatUtility","Namespace": "FFU","Forward Compatible": true,"Supported Formats": ["JSON", "XML", "CSV"],"Company": "Schneider Electric","Address":{"Street": "Schneiderplatz","House Number": 1,"Postal Code": "97828","City": "Marktheidenfeld","Country": "Germany"}}'

    fbJsonUtilities : FFU.FB_JsonUtilities;
    xBusy : BOOL;
    etResult : FFU.ET_Result;
    sResultMsg : STRING;
END_VAR

CASE iState OF
  0:
      IF xWriteModifiedJsonString THEN
          xWriteModifiedJsonString := FALSE;

        //Parse JSON formatted string
        IF NOT (fbJsonUtilities.Parse(i_anyDataToParse := sJsonString, i_sJPath := '')) THEN
          //Error handling for failed Parse process.
    etResult := fbJsonUtilities.Result;
    sResultMsg :=  fbJsonUtilities.ResultMsg
          RETURN;
      END_IF


        //Select element containing requested value
        IF NOT (fbJsonUtilities.Select(i_sJPath := '.Address.Country')) THEN
          //Error handling for failed Parse process.
    etResult := fbJsonUtilities.Result;
    sResultMsg :=  fbJsonUtilities.ResultMsg
          RETURN;
      END_IF

        //Modify value of item
        IF NOT ((fbJsonUtilities.ModifyValueTypeOfSelected(i_anyValue := sCountry)) THEN
          //Error handling for failed Modify process.
    etResult := fbJsonUtilities.Result;
    sResultMsg :=  fbJsonUtilities.ResultMsg
          RETURN;
      END_IF

      iState := 10;
    END_IF


  10:
      //Write modified JSON formatted string
      (fbJsonUtilities.WriteAsync(i_anyBufferToWrite := sJsonString, q_xBusy => xBusy);
    IF NOT xBusy THEN
        IF fbJsonUtilities.Error THEN
              //Error handling for failed Write process.
    etResult := fbJsonUtilities.Result;
    sResultMsg :=  fbJsonUtilities.ResultMsg
        END_IF

        iState :=0;
    END_IF
END_CASE