Home

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

Version 1.0.0.15
Changes:
1. refactoring of JSON_TO_STRUCT
1. added MatchAllLevels method to make the code more understandable
2. moved some variables, changed some names
2. added JSON_TO_STRUCT.ClearAll method which resets the value of all JSONVARS to null
3. added STRUCT_TO_JSON.ClearString method which clears the output string
4. added JSON_TO_STRUCT_VISU - mostly for testing, but I guess you could use it anywhere
5. added STRUCT_TO_JSON_VISU
6. deleted copyright and copying files. I don’t care about licensing. This library is released under the terms of "it's on the internet so people can do whatever the heck they want with it". Use it, modify it, copy it, put it in your own projects, contribute to it, credit me. Or not. Whatever you like. But it's free, so don't cry to me if it breaks something.
https://www.dropbox.com/s/9w2p2yrgncalixp/PRO_JSON%201.0.0.15.library?dl=0

Version 1.0.0.14
fixes a bug with composing arrays, where the last } was missing sometimes
https://www.dropbox.com/s/6mjgipbbkqnamde/PRO_JSON%201.0.0.14.library?dl=0

I realize I'm bypassing the structure of the Forge platform by simply posting links to the .library files. I don't have time to make this site work with Schneider Machine Expert, which is what I primarily use for development. If you want to contribute, comment below or send me an email.

This as an open source library to compose and parse JSON objects. For more information 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)
);