Codesys SP21 Download here
Codesys SP16 Download here
I haven't touched this library for a while, but a couple people emailed me with bugs and feature requests, so I decided to have another crack at it. As usual, 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. If you find a bug, the best way to get my attention is with a test project showing the problem. Otherwise I have to recreate your use case manually, and I may or may not have time to do that.
Release Notes
Known Issues
Download here
Changes:
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 (www.json.org). It is a popular language used as part of many other protocols, like REST.
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. The example below is in the library, along with several others.
//All variables must be of type JSONVAR. They can include other structures or put into arrays, as needed.
TYPE EXAMPLEJSONSTRUCT :
STRUCT
var1: JSONVAR;
var2: JSONVAR;
var3: JSONVAR;
obj: EXAMPLEJSONSTRUCT_1;
arrayobj: ARRAY[1..3] OF JSONVAR;
END_STRUCT
END_TYPE
TYPE EXAMPLEJSONSTRUCT_1 :
STRUCT
var4: JSONVAR;
var5: JSONVAR;
var6: JSONVAR;
var7: ARRAY[1..3] OF JSONVAR;
END_STRUCT
END_TYPE
PROGRAM ExampleJSON_PRG
VAR
ExampleJSONObj: EXAMPLEJSONSTRUCT;
ComposeJSON: STRUCT_TO_JSON;
JSONString: STRING(1000);
NewJSONObj: EXAMPLEJSONSTRUCT;
ParseJSON: JSON_TO_STRUCT;
JSONString2: STRING(1000);
END_VAR
//JSONVAR variable values can be set in a number of different ways
ExampleJSONObj.var1.Boolean:= TRUE;
ExampleJSONObj.var2.Number:= 34.8756;
ExampleJSONObj.var3.CharString:= 'teststring';
ExampleJSONObj.obj.var4.Number:= 22;
ExampleJSONObj.obj.var5.AsString:= 'FALSE'; //JSONVAR will guess at type, in this case, boolean
ExampleJSONObj.obj.var6.CharString:= 'qwer';
//NOTE: AsString will convert the value to/from STRING, and guess at a type, CharString will not convert, but assume it's a JSON string type.
ExampleJSONObj.obj.var7[1].Number:= 123.234;
ExampleJSONObj.obj.var7[2].AsString:= '55.46'; //JSONVAR will guess at type, in this case, a number
ExampleJSONObj.obj.var7[3].Number:= 985;
ExampleJSONObj.arrayobj[1].Number:= 1;
ExampleJSONObj.arrayobj[2].Number:= 2;
ExampleJSONObj.arrayobj[3].Number:= 3;
//create a JSON string from example object
ComposeJSON(
JSONString:= ADR(JSONString),
JSONStringSize:= SIZEOF(JSONString),
JSONVars:= ADR(ExampleJSONObj),
NumberOfVars:= SIZEOF(ExampleJSONObj) / SIZEOF(JSONVAR)
);
//fill another example object with values from the created JSON string
ParseJSON(
JSONString:= ADR(JSONString2),
JSONStringSize:= SIZEOF(JSONString2),
JSONVars:= ADR(NewJSONObj),
NumberOfVars:= SIZEOF(NewJSONObj) / SIZEOF(JSONVAR)
);