Skip to content

Commit

Permalink
Merge pull request #37 from simatic-ax/refactor_to_array_star
Browse files Browse the repository at this point in the history
feat!: refactor to array star
  • Loading branch information
sjuergen authored Sep 15, 2022
2 parents 3303b29 + 6492bf5 commit d5e8ede
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 76 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ PROGRAM MyProgram
END_VAR
VAR
doc : JsonDocument;
docBuffer : ARRAY[0..200] OF CHAR;
myBoolean : JsonBoolean := (Value := TRUE, Key := 'myBoolean');
myInt : JsonInt := (Value := 1234, Key := 'myInt');
myDint : JsonDInt := (Value := DINT#12345678, Key := 'myDint');
Expand All @@ -190,6 +191,7 @@ PROGRAM MyProgram
step := Steps#SerializeDocument2;
Steps#SerializeDocument2:
// Serialize the document {"myBoolean": true, "myObject": {"myInt": 1234, "myDint": 12345678}}
doc.buffer := REF(docBuffer);
doc.Serialize(doc.buffer);
SerializedDocument := Arrays.ToString(arr := doc.buffer);
step := Steps#ParseDocument2;
Expand Down
8 changes: 4 additions & 4 deletions docs/JsonDocument.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ classDiagram
IJsonContainerElement<|--JsonDocument
IJsonContainerElement : AddElement(IJsonElement)
JsonDocument : STRING ToString()
JsonDocument : BOOL Serialize(ARRAY[*] OF CHAR)
JsonDocument : BOOL Serialize()
JsonDocument : IJsonElement GetRootElement()
JsonDocument : ClearBuffer(BOOL)
JsonDocument : Reset()
Expand All @@ -23,8 +23,8 @@ JsonDocument : Reset()

Returns the JSON string of the JSON document (max. 254 characters)

### Serialize(IN_OUT buf : ARRAY[*] OF CHAR) : BOOL
Serializes the JSON document ino a ARRAY OF CHAR
### Serialize() : BOOL
Serializes the JSON document ino a ARRAY OF CHAR. JsonDocument.buffer must be set before.

### GetRootElement() : IJsonElement
Returns root element of the JSON document
Expand All @@ -33,7 +33,7 @@ Returns root element of the JSON document
Add a new element to the JSON document

### ClearBuffer(hard : BOOL)
Clear the buffer logically (fast). If `hard = TRUE` then delete the buffer also physically (slow)
Clear the buffer logically (fast). If `hard = TRUE` then delete the buffer also physically (slow) JsonDocument.buffer must be set before.

### Reset()
Reset the complete object tree and the destination buffer of the JSON document for the purpose, a new JSON object should be created.
Expand Down
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
Loading

0 comments on commit d5e8ede

Please sign in to comment.