Home

codesys.com tvm

Version 1.0.0.16
https://www.dropbox.com/scl/fi/16rzprk6dhnr6204ya5vu/PRO_JSON-1.0.0.16.library?rlkey=vohd00hny0uvm9i6a1avf16vq&dl=0
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

Version 1.0.0.15
https://www.dropbox.com/s/9w2p2yrgncalixp/PRO_JSON%201.0.0.15.library?dl=0
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.

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. 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)
);

Discussion

<< < 1 2 (Page 2 of 2)
  • maxkemmeren - 2024-05-02

    I Found my thinking mistake. You made it that you can onlyh parse from and to a struct of the same instance name.

    In my example I parse a writing struct and than parse it back into a reading struct of the same type. But as the writing and reading struct have different instance names this match all levels does not become true making the reading struct empty.

    So parsing to and from 1 struct instance will work.

     
    • tvm - 2024-05-02

      Correct. In an actual use case I think the compose and parse would happen on different machines.

       
  • Sen_ - 2024-07-04

    Hi,
    I am using this library. but i am facing some issue. i dont know if this is how the library is supposed to work!
    I am parsing from a JSON string to a JSON struct and the JSON struct has an array in one of its variable. and the array index starts from 0. but then, in the 0 th index of the array i am not getting the expected value. rather the values start from 1st array index.

    Is this the actual working of the library?
    or am i missing some configuration?

     
  • Sen_ - 2024-07-04
     

    Last edit: Sen_ 2024-07-04
<< < 1 2 (Page 2 of 2)

Log in to post a comment.