Skip to content

Commit

Permalink
refactored to array*
Browse files Browse the repository at this point in the history
  • Loading branch information
sjuergen committed Sep 15, 2022
1 parent 3303b29 commit a2fa92c
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 72 deletions.
34 changes: 23 additions & 11 deletions src/Document/JsonDocument.st
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ NAMESPACE Simatic.Ax.Json
{axcode:docs-v0:@simatic-ax/json:JsonDocument.md}
CLASS FINAL JsonDocument IMPLEMENTS IJsonContainerElement
VAR PUBLIC
buffer : ARRAY[0..999] OF CHAR;
buffer : REF_TO ARRAY[*] OF CHAR;
END_VAR

VAR PROTECTED
_firstElement : IJsonElementMuteable;
_lastElement : IJsonElementMuteable;
_rootElement : IJsonElement;
_lentgh : DINT;
_bufLowerBound : DINT;
_bufIndex : DINT;
END_VAR

Expand Down Expand Up @@ -42,17 +41,18 @@ NAMESPACE Simatic.Ax.Json
/// Serializes the JSON document ino a ARRAY OF CHAR
{axcode:docs-v0:@simatic-ax/json:JsonDocument.md}
METHOD PUBLIC Serialize : BOOL
VAR_IN_OUT
buf : ARRAY[*] OF CHAR;
END_VAR
VAR_TEMP
_to : IJsonElementMuteable;
_bufLowerBound : DINT;
END_VAR

IF (buffer = NULL) THEN
Serialize := FALSE;
RETURN;
END_IF;
// Clear buffer (not hard before serializing)
THIS.ClearBuffer();

_bufLowerBound := LOWER_BOUND(buf, 1);
_bufLowerBound := LOWER_BOUND(buffer^, 1);
_bufIndex := _bufLowerBound;
THIS.AddChar(c := '{');

Expand All @@ -73,7 +73,11 @@ NAMESPACE Simatic.Ax.Json
VAR_INPUT
c : CHAR;
END_VAR
buffer[_bufIndex] := c;
IF (buffer = NULL) THEN
AddChar := FALSE;
RETURN;
END_IF;
buffer^[_bufIndex] := c;
_bufIndex := _bufIndex + 1;
_lentgh := _lentgh + 1;
AddChar := TRUE;
Expand All @@ -87,8 +91,12 @@ NAMESPACE Simatic.Ax.Json
VAR_TEMP
i : INT;
END_VAR
IF (buffer = NULL) THEN
AddString := FALSE;
RETURN;
END_IF;
FOR i := 1 TO LengthOf(s) DO
buffer[_bufIndex] := s[i];
buffer^[_bufIndex] := s[i];
_bufIndex := _bufIndex + 1;
_lentgh := _lentgh + 1;
END_FOR;
Expand Down Expand Up @@ -141,11 +149,15 @@ NAMESPACE Simatic.Ax.Json
END_VAR
VAR_TEMP
i : DINT;
_bufLowerBound : DINT;
END_VAR

IF (buffer = NULL) THEN
RETURN;
END_IF;
_bufLowerBound := LOWER_BOUND(buffer^, 1);
IF (hard) THEN
FOR i := _bufLowerBound TO _bufIndex DO
buffer[i] := TO_CHAR(BYTE#16#00);
buffer^[i] := TO_CHAR(BYTE#16#00);
END_FOR;
END_IF;
_bufLowerBound := 0;
Expand Down
16 changes: 10 additions & 6 deletions test/Document/TestCleanResetDocument.st
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ NAMESPACE Document
doc : JsonDocument;
cmpBuff : ARRAY[0..399] OF CHAR;
cmpBuffEmpty : ARRAY[0..399] OF CHAR;
buffer : ARRAY[0..200] OF CHAR;
len : DINT;
END_VAR

Expand All @@ -36,16 +37,18 @@ NAMESPACE Document
o2.AddElement(o3);
o1.AddElement(o2);
doc.AddElement(o1);
doc.Serialize(doc.buffer);
doc.buffer := REF(buffer);
doc.Serialize();

len := Strings.ToArray.OfCharCount(str := expString, arr := cmpBuff);
res := CompareArray(first := doc.buffer, scnd := cmpBuff, count := TO_INT(len));
res := CompareArray(first := buffer, scnd := cmpBuff, count := TO_INT(len));
Equal(expected := TRUE, actual := res);
Equal(expected := LengthOf(expString), actual := doc.GetLength());
END_METHOD

{Test}
METHOD PUBLIC TestCleanBuffer
doc.buffer := REF(buffer);
// Fill Buffer
THIS.Test_Build_nested_object_And_expect_correct_JSON();
Equal(expected := LengthOf(expString), actual := doc.GetLength());
Expand All @@ -55,13 +58,14 @@ NAMESPACE Document
Equal(expected := 0, actual := doc.GetLength());

// compare result buffer with compare array
res := CompareArray(first := doc.buffer, scnd := cmpBuff, count := TO_INT(len));
res := CompareArray(first := buffer, scnd := cmpBuff, count := TO_INT(len));
Equal(expected := TRUE, actual := res);

// Serialize again
doc.Serialize(doc.buffer);

doc.Serialize();
len := Strings.ToArray.OfCharCount(str := expString, arr := cmpBuff);
res := CompareArray(first := doc.buffer, scnd := cmpBuff, count := TO_INT(len));
res := CompareArray(first := buffer, scnd := cmpBuff, count := TO_INT(len));
Equal(expected := TRUE, actual := res);
Equal(expected := LengthOf(expString), actual := doc.GetLength());

Expand All @@ -79,7 +83,7 @@ NAMESPACE Document
Equal(expected := 0, actual := doc.GetLength());

// compare result buffer with compare array
res := CompareArray(first := doc.buffer, scnd := cmpBuffEmpty, count := TO_INT(len));
res := CompareArray(first := buffer, scnd := cmpBuffEmpty, count := TO_INT(len));
Equal(expected := TRUE, actual := res);

END_METHOD
Expand Down
6 changes: 4 additions & 2 deletions test/Document/TestDoubleNestetdObject.st
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ NAMESPACE Document
res : BOOL;
doc : JsonDocument;
cmpBuff : ARRAY[0..399] OF CHAR;
buffer : ARRAY[0..200] OF CHAR;
len : DINT;
END_VAR

Expand All @@ -35,10 +36,11 @@ NAMESPACE Document
o2.AddElement(o3);
o1.AddElement(o2);
doc.AddElement(o1);
doc.Serialize(doc.buffer);
doc.buffer := REF(buffer);
doc.Serialize();

len := Strings.ToArray.OfCharCount(str := expString, arr := cmpBuff);
res := CompareArray(first := doc.buffer, scnd := cmpBuff, count := TO_INT(len));
res := CompareArray(first := buffer, scnd := cmpBuff, count := TO_INT(len));
Equal(expected := TRUE, actual := res);
Equal(expected := LengthOf(expString), actual := doc.GetLength());
END_METHOD
Expand Down
48 changes: 28 additions & 20 deletions test/Document/TestJsonDocument.st
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ NAMESPACE Document
expString : STRING;
actString : STRING;
result : BOOL;

buffer : ARRAY[0..200] OF CHAR;


END_VAR
Expand All @@ -49,8 +49,9 @@ NAMESPACE Document

// Test Serialize
expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray);
doc.Serialize(doc.buffer);
result := CompareArray(first := doc.buffer, scnd := expArray, count := TO_INT(expArrayCnt));
doc.buffer := REF(buffer);
doc.Serialize();
result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt));
Equal(expected := TRUE, actual := result);

END_METHOD
Expand All @@ -66,9 +67,10 @@ NAMESPACE Document

// Test Serialize
expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray);
doc.Serialize(doc.buffer);
doc.Serialize(doc.buffer);
result := CompareArray(first := doc.buffer, scnd := expArray, count := TO_INT(expArrayCnt));
doc.buffer := REF(buffer);
doc.Serialize();
doc.Serialize();
result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt));
Equal(expected := TRUE, actual := result);
END_METHOD

Expand All @@ -84,8 +86,9 @@ NAMESPACE Document

// Test Serialize
expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray);
doc.Serialize(doc.buffer);
result := CompareArray(first := doc.buffer, scnd := expArray, count := TO_INT(expArrayCnt));
doc.buffer := REF(buffer);
doc.Serialize();
result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt));
Equal(expected := TRUE, actual := result);
END_METHOD

Expand All @@ -99,8 +102,9 @@ NAMESPACE Document

// Test Serialize
expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray);
doc.Serialize(doc.buffer);
result := CompareArray(first := doc.buffer, scnd := expArray, count := TO_INT(expArrayCnt));
doc.buffer := REF(buffer);
doc.Serialize();
result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt));
Equal(expected := TRUE, actual := result);
END_METHOD

Expand All @@ -118,8 +122,9 @@ NAMESPACE Document

// Test Serialize
expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray);
doc.Serialize(doc.buffer);
result := CompareArray(first := doc.buffer, scnd := expArray, count := TO_INT(expArrayCnt));
doc.buffer := REF(buffer);
doc.Serialize();
result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt));
Equal(expected := TRUE, actual := result);
END_METHOD

Expand All @@ -137,8 +142,9 @@ NAMESPACE Document

// Test Serialize
expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray);
doc.Serialize(doc.buffer);
result := CompareArray(first := doc.buffer, scnd := expArray, count := TO_INT(expArrayCnt));
doc.buffer := REF(buffer);
doc.Serialize();
result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt));
Equal(expected := TRUE, actual := result);
END_METHOD

Expand All @@ -154,8 +160,9 @@ NAMESPACE Document

// Test Serialize
expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray);
doc.Serialize(doc.buffer);
result := CompareArray(first := doc.buffer, scnd := expArray, count := TO_INT(expArrayCnt));
doc.buffer := REF(buffer);
doc.Serialize();
result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt));
Equal(expected := TRUE, actual := result);
END_METHOD

Expand Down Expand Up @@ -190,8 +197,8 @@ NAMESPACE Document

// // Test Serialize
// expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray);
// doc.Serialize(doc.buffer);
// result := CompareArray(first := doc.buffer, scnd := expArray, count := TO_INT(expArrayCnt));
// doc.Serialize();
// result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt));
// Equal(expected := TRUE, actual := result);
END_METHOD

Expand All @@ -216,8 +223,9 @@ NAMESPACE Document

// Test Serialize
expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray);
doc.Serialize(doc.buffer);
result := CompareArray(first := doc.buffer, scnd := expArray, count := TO_INT(expArrayCnt));
doc.buffer := REF(buffer);
doc.Serialize();
result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt));
Equal(expected := TRUE, actual := result);

END_METHOD
Expand Down
3 changes: 2 additions & 1 deletion test/Document/TestJsonLongDocument.st
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ NAMESPACE Document
e2 : JsonLInt := (Key := 'elem2', Value := 1);
e3 : JsonDInt := (Key := 'elem3', Value := 2);
doc :JsonDocument;
buffer : ARRAY[0..200] OF CHAR;
END_VAR

METHOD PUBLIC MyMethod
doc.AddElement(e1);
doc.AddElement(e2);
doc.AddElement(e3);
doc.Serialize(doc.buffer); //--> {elem1: 0, elem2: 1, elem3: 2}\n
doc.Serialize(); //--> {elem1: 0, elem2: 1, elem3: 2}\n
// nice to have --> doc.Deserialize('{elem1: 99, elem2: 100, elem3: 111}\n')
END_METHOD

Expand Down
36 changes: 22 additions & 14 deletions test/Document/TestResetNestedDocument.st
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,58 @@ NAMESPACE Document
myDint : JsonDInt := (Value := DINT#12345678, Key := 'myDint');
myString : JsonString := (Value := 'this is my string', Key := 'myString');
myObject : JsonObject := (Key := 'myObject');
buffer : ARRAY[0..200] OF CHAR;
END_VAR

{Test}
METHOD PUBLIC Serialize_BOOLEAN
doc.buffer := REF(buffer);

doc.AddElement(myBoolean);
doc.Serialize(doc.buffer);
doc.Serialize();

str := Arrays.ToString(doc.buffer, 0, 18);
str := Arrays.ToString(buffer, 0, 18);
Equal(expected := '{"myBoolean": true}', actual := str);
END_METHOD

{Test}
METHOD PUBLIC Serialize_INT
doc.buffer := REF(buffer);
doc.AddElement(myInt);
doc.Serialize(doc.buffer);
doc.Serialize();

str := Arrays.ToString(doc.buffer, 0, 14);
str := Arrays.ToString(buffer, 0, 14);
Equal(expected := '{"myInt": 1234}', actual := str);
END_METHOD

{Test}
METHOD PUBLIC Serialize_DINT
doc.buffer := REF(buffer);
doc.AddElement(myDint);
doc.Serialize(doc.buffer);
doc.Serialize();

str := Arrays.ToString(doc.buffer, 0, 19);
str := Arrays.ToString(buffer, 0, 19);
Equal(expected := '{"myDint": 12345678}', actual := str);
END_METHOD

{Test}
METHOD PUBLIC Serialize_STRING
doc.buffer := REF(buffer);
doc.AddElement(myString);
doc.Serialize(doc.buffer);
doc.Serialize();

str := Arrays.ToString(doc.buffer, 0, 32);
str := Arrays.ToString(buffer, 0, 32);
Equal(expected := '{"myString": "this is my string"}', actual := str);
END_METHOD

{Test}
METHOD PUBLIC Serialize_INT_DINT
doc.buffer := REF(buffer);
doc.AddElement(myInt);
doc.AddElement(myDint);
doc.Serialize(doc.buffer);
doc.Serialize();

str := Arrays.ToString(doc.buffer, 0, 34);
str := Arrays.ToString(buffer, 0, 34);
Equal(expected := '{"myInt": 1234, "myDint": 12345678}', actual := str);
END_METHOD

Expand All @@ -73,9 +80,10 @@ NAMESPACE Document
doc.AddElement(myBoolean);
doc.AddElement(myObject);

doc.Serialize(doc.buffer);
doc.buffer := REF(buffer);
doc.Serialize();

str := Arrays.ToString(doc.buffer, 0, 67);
str := Arrays.ToString(buffer, 0, 67);
Equal(expected := '{"myBoolean": true, "myObject": {"myInt": 1234, "myDint": 12345678}}', actual := str);


Expand All @@ -89,8 +97,8 @@ NAMESPACE Document
doc.AddElement(myBoolean);
doc.AddElement(myObject);

doc.Serialize(doc.buffer);
str := Arrays.ToString(doc.buffer, 0, 67);
doc.Serialize();
str := Arrays.ToString(buffer, 0, 67);
Equal(expected := '{"myBoolean": true, "myObject": {"myInt": 1234, "myDint": 12345678}}', actual := str);

END_METHOD
Expand Down
Loading

0 comments on commit a2fa92c

Please sign in to comment.