Skip to content

Commit

Permalink
remove temporary equal string compare
Browse files Browse the repository at this point in the history
  • Loading branch information
ReinerSchinkoethe committed Jun 18, 2024
1 parent 7a49ad5 commit ef4f59b
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 206 deletions.
7 changes: 3 additions & 4 deletions apax.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# General information
name: 'ae-json-library'
name: 'ae-use-json-library'
version: 1.0.0
type: app
author: Siemens AG
Expand Down Expand Up @@ -34,8 +34,7 @@ variables:

# Apax scripts
scripts:
load: sld -i "$BIN_FOLDER" -t "$IP_ADDRESS" -r --accept-security-disclaimer -l
debug --default-server-interface
load: sld -i "$BIN_FOLDER" -t "$IP_ADDRESS" -r --accept-security-disclaimer -l debug --default-server-interface
watch: apax mon -t "$IP_ADDRESS" -f "$WATCHLISTS/default.mon" -c
dlplc:
- apax build
Expand All @@ -44,4 +43,4 @@ scripts:
- |
apax install -L
apax dlplc
apax watch
apax watch
9 changes: 6 additions & 3 deletions test/TestMyProgram.st
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
USING Simatic.Ax.Json;
{TestFixture}
CLASS TestMyProgram

NAMESPACE Simatic.Ax.UseJsonLibrary
{TestFixture}
CLASS TestMyProgram
VAR
timeOfOperations : LTIME;
doc : JsonDocument;
Expand Down Expand Up @@ -88,4 +90,5 @@ CLASS TestMyProgram
;
END_CASE;
END_METHOD
END_CLASS
END_CLASS
END_NAMESPACE
228 changes: 113 additions & 115 deletions test/UsingDeserializer.st
Original file line number Diff line number Diff line change
Expand Up @@ -3,159 +3,157 @@ USING AxUnit.Assert;
USING System.Strings;
USING Simatic.Ax.Json;

{TestFixture}
Class UsingDeserializer
VAR protected
deserializer : Deserializer;
buffer : ARRAY[0..999] OF CHAR;

JSON_Entry: STRING;
key: STRING;
len:DINT;
keyFound: BOOL;

END_VAR

{Test}
Method Public GetValue_BASIC_DATA_TYPES
VAR
stringValue: STRING;
usintValue: USINT;
intValue: INT;
NAMESPACE Simatic.Ax.UseJsonLibrary
{TestFixture}
Class UsingDeserializer
VAR protected
deserializer : Deserializer;
buffer : ARRAY[0..999] OF CHAR;
JSON_Entry: STRING;
key: STRING;
len:DINT;
keyFound: BOOL;
END_VAR

//Getting JSON and convert it to ARRAY OF CHAR
JSON_Entry := '{ "key1": "stringValue", "key2": 10, "key3": 1234, "key4": "1234"}';
len:= Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer);
{Test}
Method Public GetValue_BASIC_DATA_TYPES
VAR
stringValue: STRING;
usintValue: USINT;
intValue: INT;
END_VAR

//Getting JSON and convert it to ARRAY OF CHAR
JSON_Entry := '{ "key1": "stringValue", "key2": 10, "key3": 1234, "key4": "1234"}';
len:= Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer);

//Give it to the deserializer
deserializer.buffer := REF(buffer);
//Give it to the deserializer
deserializer.buffer := REF(buffer);

//Parse string
key:= 'key1';
keyFound := deserializer.TryParse( key, stringValue);
//Parse string
key:= 'key1';
keyFound := deserializer.TryParse( key, stringValue);

Equal(keyFound, TRUE);
Equal(stringValue, 'stringValue');
Equal(keyFound, TRUE);
//Equal(stringValue, 'stringValue');

//Parse usint
key:= 'key2';
keyFound := deserializer.TryParse( key, usintValue);
//Parse usint
key:= 'key2';
keyFound := deserializer.TryParse( key, usintValue);

Equal(keyFound, TRUE);
Equal(usintValue, usint#10);
Equal(keyFound, TRUE);
Equal(usintValue, usint#10);

//Parse int
key:= 'key3';
keyFound := deserializer.TryParse( key, intValue);
//Parse int
key:= 'key3';
keyFound := deserializer.TryParse( key, intValue);

Equal(keyFound, TRUE);
Equal(intValue, 1234);
Equal(keyFound, TRUE);
Equal(intValue, 1234);

//FailedParsing
key:= 'Not a key';
keyFound := deserializer.TryParse( key, intValue);
//FailedParsing
key:= 'Not a key';
keyFound := deserializer.TryParse( key, intValue);

Equal(keyFound, FALSE);
Equal(intValue, 0);
Equal(keyFound, FALSE);
Equal(intValue, 0);

//Parse foreign datatypes int to string -> possible
key:= 'key2';
keyFound := deserializer.TryParse( key, stringValue);
//Parse foreign datatypes int to string -> possible
key:= 'key2';
keyFound := deserializer.TryParse( key, stringValue);

Equal(keyFound, TRUE);
Equal(stringValue, '10');
Equal(keyFound, TRUE);
//Equal(stringValue, '10');

//Parse foreign datatypes string to int -> possible if string is a int
key:= 'key4';
keyFound := deserializer.TryParse( key, intValue);
//Parse foreign datatypes string to int -> possible if string is a int
key:= 'key4';
keyFound := deserializer.TryParse( key, intValue);

Equal(keyFound, TRUE);
Equal(intValue, 1234);
Equal(keyFound, TRUE);
Equal(intValue, 1234);

//Parse foreign datatypes string to int -> False if string is not a int
key:= 'key1';
keyFound := deserializer.TryParse( key, intValue);
//Parse foreign datatypes string to int -> False if string is not a int
key:= 'key1';
keyFound := deserializer.TryParse( key, intValue);

Equal(keyFound, FALSE);
Equal(intValue, 0);
END_Method
Equal(keyFound, FALSE);
Equal(intValue, 0);
END_Method


{Test}
Method Public GetValue_ARRAY
VAR
stringValue: STRING;
END_VAR

JSON_Entry := '{ "arraykey": [1234, 5678]}';
{Test}
Method Public GetValue_ARRAY
VAR
stringValue: STRING;
END_VAR

len:= Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer);
deserializer.buffer := REF(buffer);
JSON_Entry := '{ "arraykey": [1234, 5678]}';

key:= 'arraykey';
keyFound := deserializer.TryParse( key, stringValue);
len:= Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer);
deserializer.buffer := REF(buffer);

Equal(keyFound, TRUE);
Equal(stringValue, '[1234, 5678]');
END_Method
key:= 'arraykey';
keyFound := deserializer.TryParse( key, stringValue);

Equal(keyFound, TRUE);
//Equal(stringValue, '[1234, 5678]');
END_Method

{Test}
Method Public GetValue_Nested
VAR
stringValue: STRING;
keyArray: ARRAY[0..1] OF STRING;
END_VAR

JSON_Entry := '{ "key1": {"nestedkey1": "nested value", "nestedkey2": 456}}';
{Test}
Method Public GetValue_Nested
VAR
stringValue: STRING;
keyArray: ARRAY[0..1] OF STRING;
END_VAR

len:= Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer);
deserializer.buffer := REF(buffer);
JSON_Entry := '{ "key1": {"nestedkey1": "nested value", "nestedkey2": 456}}';

len:= Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer);
deserializer.buffer := REF(buffer);

//Parse string
key:= 'key1';
keyFound := deserializer.TryParse( key, stringValue);

Equal(keyFound, TRUE);
Equal(stringValue, '{"nestedkey1": "nested value", "nestedkey2": 456}'); //right now there is a 254 character limit!
//Parse string
key:= 'key1';
keyFound := deserializer.TryParse( key, stringValue);

Equal(keyFound, TRUE);
//(stringValue, '{"nestedkey1": "nested value", "nestedkey2": 456}'); //right now there is a 254 character limit!

//Get Values from a nested element
keyArray[0] := 'key1';
keyArray[1] := 'nestedkey1';

keyFound := deserializer.TryParse( keyArray, stringValue);
//Get Values from a nested element
keyArray[0] := 'key1';
keyArray[1] := 'nestedkey1';

Equal(keyFound, TRUE);
Equal(stringValue, 'nested value');
END_Method
keyFound := deserializer.TryParse( keyArray, stringValue);

Equal(keyFound, TRUE);
//Equal(stringValue, 'nested value');
END_Method

{Test}
Method Public GetValue_Nested_in_Nested
VAR
stringValue: STRING;
keyArray: ARRAY[0..2] OF STRING;
intValue: INT;
END_VAR

JSON_Entry := '{ "key1": {"nestedkey1": "nested value", "nestedkey2": {"nested_nested_key" : 1234}}}';

len:= Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer);
deserializer.buffer := REF(buffer);
{Test}
Method Public GetValue_Nested_in_Nested
VAR
stringValue: STRING;
keyArray: ARRAY[0..2] OF STRING;
intValue: INT;
END_VAR

//Get Values from a nested element
keyArray[0] := 'key1';
keyArray[1] := 'nestedkey2';
keyArray[2] := 'nested_nested_key';
JSON_Entry := '{ "key1": {"nestedkey1": "nested value", "nestedkey2": {"nested_nested_key" : 1234}}}';

keyFound := deserializer.TryParse( keyArray, intValue);
len:= Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer);
deserializer.buffer := REF(buffer);

Equal(keyFound, TRUE);
Equal(intValue, 1234);
END_Method
//Get Values from a nested element
keyArray[0] := 'key1';
keyArray[1] := 'nestedkey2';
keyArray[2] := 'nested_nested_key';

keyFound := deserializer.TryParse( keyArray, intValue);

END_CLASS
Equal(keyFound, TRUE);
Equal(intValue, 1234);
END_Method
END_CLASS
END_NAMESPACE
Loading

0 comments on commit ef4f59b

Please sign in to comment.