Home

codesys.com tvm
There is a newer version of this page. You can find it here.

Version 1.0.0.9 of the .library file:
https://www.dropbox.com/s/0qn2cb2fyv9lqrv/PRO_JSON%201.0.0.9.library?dl=0

This as an open source library to compose and parse JSON objects. For more informations about json, have a look at json.org. It is a popular language used as part of many other protocols, like REST.

Usage

The usage is simple. You can describe a JSON file with a special structure format, which then can be stored or loaded from or to a file. Actually instead of a file, you are also able to use an HTTP Client library or s.th. similar.

TYPE EXAMPLEJSONSTRUCT :
STRUCT
    var1:       JSONVAR;
    var2:       JSONVAR;
    var3:       JSONVAR;
    obj:        EXAMPLEJSONSTRUCT_1;
    arrayobj:   ARRAY[1..3] OF JSONVAR;
END_STRUCT
END_TYPE
PROGRAM ExampleJSON_PRG
VAR
    LocalJSONObj:       EXAMPLEJSONSTRUCT;
    ComposeJSON:        STRUCT_TO_JSON;

    JSONString:         STRING(1000);

    NewJSONObj:         EXAMPLEJSONSTRUCT;  
    ParseJSON:          JSON_TO_STRUCT;
END_VAR

LocalJSONObj.var1.Boolean:= TRUE;
LocalJSONObj.var2.Number:= 34.8756;
LocalJSONObj.var3.CharString:= 'teststring';
LocalJSONObj.obj.var4.Number:= 22;
LocalJSONObj.obj.var5.AsString:= 'FALSE';   //JSONVAR will guess at type, in this case, boolean
LocalJSONObj.obj.var6.CharString:= 'qwer';
LocalJSONObj.obj.var7[1].Number:= 123.234;
LocalJSONObj.obj.var7[2].AsString:= '55.46';    //JSONVAR will guess at type, in this case, a number
LocalJSONObj.obj.var7[3].Number:= 985;
LocalJSONObj.arrayobj[1].Number:= 1;
LocalJSONObj.arrayobj[2].Number:= 2;
LocalJSONObj.arrayobj[3].Number:= 3;

//create a JSON string from local object
ComposeJSON(
    JSONString:= ADR(JSONString), 
    JSONStringSize:= SIZEOF(JSONString),
    JSONVars:= ADR(LocalJSONObj),
    NumberOfVars:= SIZEOF(LocalJSONObj) / SIZEOF(JSONVAR)
);

//fill another local object with values from the created JSON string
ParseJSON(
    Execute:= ComposeJSON.Done,
    JSONString:= ADR(JSONString), 
    JSONStringSize:= SIZEOF(JSONString),
    JSONVars:= ADR(NewJSONObj),
    NumberOfVars:= SIZEOF(NewJSONObj) / SIZEOF(JSONVAR)
);