Write a JSON/XML Processor
To write data using a JSON/XML driver tag, you must create a processor that defines the data to be written.
Any number of processor modules can be added to an application. The following conditions must be met for each:
- A processor must be declared as part of the PLUGINS class in your application's AppRoot.SRC file.
- It must include a constant IsJSONXMLProcessor flag that identifies itself as a processor.
CONSTANT IsJSONXMLProcessor = TRUE;
- It must be a subroutine that returns the converted data structure made up of dictionaries, structures, arrays, or a combination of these.
- It should have a DrawLabel.
The parameters of a processor are:
Data
The data structure after decoding it from the original JSON or XML payload.
UniqueID
(optional) The UniqueID of the calling JSON/XML Driver.
TimeValuePairs
If timestamped values exist in the data, the converted data structure must put those time and value pairs into a structure made up of Value and Timestamp elements.
The input tag must use this structure's address. For example, an address of "Site1.Temperature" would give a tag access to the value in Site1.Temperature.Value and its timestamp in Site1.Temperature.Timestamp.
Sample time/value structure that could be used by the processor to return time/value pairs of data:
TimeValuePair Structure [ Value; Timestamp; ]
After declaring your custom processor module in your AppRoot.SRC file and importing file changes, use the name that you assigned to the module in the Processor parameter of your JSONXML tag.
Example:
{=========================== CustomJSONProcessor =============================} { Custom JSON data processor for the JSONXMLDriver } {=============================================================================} ( Data; UniqueID; ) [ CONSTANT IsJSONXMLProcessor = TRUE; DrawLabel = "JSONLabel"; NewData; TimeValuePair Struct [ Timestamp; Value; ]; ] Main [ If 1; [ NewData = Dictionary(); NewData["J1"] = TimeValuePair(Data.DataValues.T1, Data.DataValues.V1); NewData["J2"] = TimeValuePair(Data.DataValues.T2, Data.DataValues.V2); NewData["J3"] = TimeValuePair(Data.DataValues.T3, Data.DataValues.V3); NewData["History"] = TimeValuePair(New(3), New(3)); NewData["History"].Timestamp[0] = Data.DataValues.T1; NewData["History"].Timestamp[1] = Data.DataValues.T2; NewData["History"].Timestamp[2] = Data.DataValues.T3; NewData["History"].Value[0] = Data.DataValues.V1; NewData["History"].Value[1] = Data.DataValues.V2; NewData["History"].Value[2] = Data.DataValues.V3; NewData["UniqueID"] = UniqueID; NewData["Timestamp"] = "Not a timestamp"; Return(NewData); ] ]