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 an 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
1. Significant refactoring in several places, for both efficiency and ease of understanding. Some of the code I didn't understand, and I wrote it. Added several new functions and methods.
2. Moved the version number over one decimal place, so 1.0.18.0 follows 1.0.0.16. Odd numbers in the third position will be for development versions
3. Added support for escape characters in strings
4. Input and some new functions from Fabrizio Morlino - added support for JSON5 hex values (can be enabled/disabled with GPL_JSON.JSON5 parameter)
5. STRUCT_TO_JSON - added IgnoreNull input, and removed now redundant VariableArraySize input. When IgnoreNull is true, any variable with a null value will not be composed
6. New variable JSONVAR.Format, of type JSONFORMAT, for forcing certain types to be composed or parsed in certain ways, when the underlying data type could be ambiguous
7. New flags in JSONVARINFO to help track the properties of each object
8. New function FORMAT_JSON to add white space if the Format input is true. Removed all the old white space code that only partially worked.
9. Added support for 0 based arrays. Probably any array indexes should work, but 0 and 1 have been tested
10. Significantly more test cases
Known Issues
1. Multidimensional arrays do not work
2. In certain instances, it's not possible to have an unnamed object, regardless of the MaxLevels input
Download here
Changes:
1. changes to JSONVAR.FB_Init (Simon Dreyer)
1. line 8: delete all names first before populating the array
2. line 25: when copying the string, ensure that no more than GPL_JSON.MAX_NAME_SIZE are copied, to leave a null character at the end
2. changes from discussion at https://github.com/stefandreyer/JSON-Library/issues/1
1. line 129 (End previous object): added second case for when offset < 0 (struct of struct)
3. (Stefan Dreyer) added GPL_JSON.APPLICATION_NAME for when the Application has another name than ‘Application’
4. STRUCT_TO_JSON:
1. added check for ApplicationLevel = 0, misconfigured application name
2. reconfigured error checking and added ErrorMsg
5. JSON_TO_STRUCT
1. added check for ApplicationLevel = 0, misconfigured application name
2. reconfigured error checking and added ErrorMsg
6. STRUCT_TO_JSON: added error and message if JSONString is too small
7. JSONVAR_FB_Init: New array, added a check for the end of an array on the previous var for the case when two arrays are adjacent to each other in a struct
8. added JSONVARS_TABLE_VISU and JSONVAR_TAVLE_VISU for testing and troubleshooting
9. added some points to JSON_TO_STRUCT_VISU and STRUCT_TO_JSON_VISU
10. (Stefan Dreyer) changed STRUCT_TO_JSON line 132 closeLevel:= MaxLevel (was closeLevel:= 1) to fix bug where closing bracket was not added when MaxLevel was defined
11. changed the ASCII enum to make it a bit more readable when looking at the JSONString arrays
12. Restructuring of JSON_TO_STRUCT.Name and Value methods - some length checks added, more efficient and readable
13. Several minor stylistic 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. 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)
);