Skip to content

Commit

Permalink
Merge pull request #29 from simatic-ax/performance-optimization-in-se…
Browse files Browse the repository at this point in the history
…rialize

Performance optimization in serialize
  • Loading branch information
sjuergen authored Sep 8, 2022
2 parents 91426df + eabe551 commit c2c10a4
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 64 deletions.
137 changes: 73 additions & 64 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,78 +139,87 @@ However, the norm does not define, how duplicate keys should be processed. In ca

## [JsonDocument](docs/JsonDocument.md)

### AbstractJsonElement
## [JsonObject](docs/JsonObject.md)

---
## JsonObject
# Examples

### Definition

Inheritance: AbstractJsonElement-->JsonObject
## Example ho to use the JSON library
This example shows in how to create, serialize, parse and reset a JSON document.

### Public members
|||
|-|-|
|value : DINT;| Value of this element
|key : STRING := 'NoKeySet';| Key of this element
### Methods
|||
|-|-|
|ToString() : STRING;| Returns the JSON string of this element|
|GetRootElement() : IJsonElement| Returns root element of the JSON object|
|AddElement(elem : IJsonElement)| Add a new element to the JSON object|
|||
---
## DoubleInt

### Definition

Inheritance: AbstractJsonElement-->JsonNumber-->DoubleInt
```
USING Simatic.Ax.Conversion;
USING Simatic.Ax.Json;
### Public members
|||
|-|-|
|value : DINT;| Value of this element
|key : STRING := 'NoKeySet';| Key of this element
CONFIGURATION MyConfiguration
TASK Main (INTERVAL := T#100ms, PRIORITY := 1);
PROGRAM P1 WITH Main: MyProgram;
VAR_GLOBAL
SerializedDocument : STRING;
ParsedValue : DINT;
END_VAR
END_CONFIGURATION
PROGRAM MyProgram
VAR_EXTERNAL
SerializedDocument : STRING;
ParsedValue : DINT;
END_VAR
VAR
doc : JsonDocument;
myBoolean : JsonBoolean := (Value := TRUE, Key := 'myBoolean');
myInt : JsonInt := (Value := 1234, Key := 'myInt');
myDint : JsonDInt := (Value := DINT#12345678, Key := 'myDint');
myObject : JsonObject := (Key := 'myObject');
deserializer : Deserializer;
keyFound : BOOL;
dintValue : DINT;
keyArray : ARRAY[0..1] OF STRING := ['myObject', 'myDint'];
step : Steps;
END_VAR
VAR_TEMP
END_VAR
CASE step OF
Steps#CreateDocument2:
// Create nested JSON document which looks like:
// {"myBoolean": true, "myObject": {"myInt": 1234, "myDint": 12345678}}
myObject.AddElement(myInt);
myObject.AddElement(myDint);
doc.AddElement(myBoolean);
doc.AddElement(myObject);
step := Steps#SerializeDocument2;
Steps#SerializeDocument2:
// Serialize the document {"myBoolean": true, "myObject": {"myInt": 1234, "myDint": 12345678}}
doc.Serialize(doc.buffer);
SerializedDocument := Arrays.ToString(arr := doc.buffer);
step := Steps#ParseDocument2;
;
Steps#ParseDocument2:
// Parse Document for the value of the nested key `myObject.myDint` and expect `12345678`
// Get Values from a nested element
deserializer.SetBuffer(REF(doc.buffer));
keyFound := deserializer.TryParse(keyArray, dintValue);
ParsedValue := dintValue;
step := Steps#ResetJSonDocument2;
;
Steps#ResetJSonDocument2:
// ResetJSonDocument 2nd configuration
doc.Reset();
step := Steps#CreateDocument2;
;
END_CASE;
END_PROGRAM
TYPE
Steps : (CreateDocument2, SerializeDocument2, ParseDocument2, ResetJSonDocument2) := CreateDocument2;
END_TYPE
```

### Methods
## Application Example

|||
|-|-|
|ToString() : STRING;| Returns the JSON string of this element
A complete application example, you can find here:

## Example
```iec-st
USING Simatic.Ax.Json;
USING AxUnit.Assert;
NAMESPACE Simatic.Ax
CLASS JsonExample
VAR PUBLIC
END_VAR
VAR PROTECTED
doc : JsonDocument;
e1 : JsonDoubleInt := (key := 'Element1', value := 1);
e2 : JsonDoubleInt := (key := 'Element2', value := 2);
e3 : JsonDoubleInt := (key := 'Element3', value := 3);
o1 : JsonObject := (key := 'NestedObject');
END_VAR
METHOD PUBLIC Init;
// Example String:
// {"Element1": 1, {"Element2": 2, "Element3": 3}}
doc.AddElement(e1).AddElement(o1);
o1.AddElement(e2).AddElement(e3);
END_METHOD
METHOD PUBLIC ToString : STRING
ToString := doc.ToString();
END_METHOD
END_CLASS
END_NAMESPACE
```
[JSON Application Example](https://github.com/simatic-ax/ae-json-library)

## Contribution

Expand Down
31 changes: 31 additions & 0 deletions docs/JsonDocument.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,35 @@ Clear the buffer logically (fast). If `hard = TRUE` then delete the buffer also
### Reset()
Reset the complete object tree and the destination buffer of the JSON document for the purpose, a new JSON object should be created.

## Example
```iec-st
USING Simatic.Ax.Json;
USING AxUnit.Assert;
NAMESPACE Simatic.Ax
CLASS JsonExample
VAR PUBLIC
END_VAR
VAR PROTECTED
doc : JsonDocument;
e1 : JsonDoubleInt := (key := 'Element1', value := 1);
e2 : JsonDoubleInt := (key := 'Element2', value := 2);
e3 : JsonDoubleInt := (key := 'Element3', value := 3);
o1 : JsonObject := (key := 'NestedObject');
END_VAR
METHOD PUBLIC Init;
// Example String:
// {"Element1": 1, {"Element2": 2, "Element3": 3}}
doc.AddElement(e1).AddElement(o1);
o1.AddElement(e2).AddElement(e3);
END_METHOD
METHOD PUBLIC ToString : STRING
ToString := doc.ToString();
END_METHOD
END_CLASS
END_NAMESPACE
```
20 changes: 20 additions & 0 deletions docs/JsonObject.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# JsonObject

### Definition

Inheritance: AbstractJsonElement-->JsonObject

### Public members
|||
|-|-|
|value : DINT;| Value of this element
|key : STRING := 'NoKeySet';| Key of this element

### Methods
|||
|-|-|
|ToString() : STRING;| Returns the JSON string of this element|
|GetRootElement() : IJsonElement| Returns root element of the JSON object|
|AddElement(elem : IJsonElement)| Add a new element to the JSON object|
|||
---
9 changes: 9 additions & 0 deletions src/Document/JsonDocument.st
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ NAMESPACE Simatic.Ax.Json
END_VAR

/// Returns the JSON string of the JSON document (max. 254 characters)
{axcode:docs-v0:@simatic-ax/json:JsonDocument.md}
METHOD PUBLIC ToString : STRING
VAR_TEMP
_str : STRING;
Expand All @@ -39,6 +40,7 @@ NAMESPACE Simatic.Ax.Json
END_METHOD

/// 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;
Expand Down Expand Up @@ -66,6 +68,7 @@ NAMESPACE Simatic.Ax.Json
Serialize := TRUE;
END_METHOD

{axcode:docs-v0:@simatic-ax/json:JsonDocument.md}
METHOD PUBLIC AddChar : BOOL
VAR_INPUT
c : CHAR;
Expand All @@ -76,6 +79,7 @@ NAMESPACE Simatic.Ax.Json
AddChar := TRUE;
END_METHOD

{axcode:docs-v0:@simatic-ax/json:JsonDocument.md}
METHOD PUBLIC AddString : BOOL
VAR_INPUT
s : STRING;
Expand All @@ -92,16 +96,19 @@ NAMESPACE Simatic.Ax.Json
END_METHOD

/// Returns the count of the serialized characters
{axcode:docs-v0:@simatic-ax/json:JsonDocument.md}
METHOD PUBLIC GetLength : DINT
GetLength := _lentgh;
END_METHOD

/// Returns root element of the JSON document
{axcode:docs-v0:@simatic-ax/json:JsonDocument.md}
METHOD PUBLIC GetRootElement : IJsonElement
GetRootElement := _rootElement;
END_METHOD

/// Add a new element to the JSON document
{axcode:docs-v0:@simatic-ax/json:JsonDocument.md}
METHOD PUBLIC AddElement : IJsonContainerElement
VAR_INPUT
elem : IJsonElement;
Expand All @@ -127,6 +134,7 @@ NAMESPACE Simatic.Ax.Json
/// Clear the buffer logically (fast).
/// If `hard = TRUE` then delete the buffer also
/// physically (slow)
{axcode:docs-v0:@simatic-ax/json:JsonDocument.md}
METHOD PUBLIC ClearBuffer
VAR_INPUT
hard : BOOL := FALSE;
Expand All @@ -148,6 +156,7 @@ NAMESPACE Simatic.Ax.Json
/// Reset the complete object tree and the destination buffer
/// of the JSON document for the purpose, a new JSON object should
/// be created.
{axcode:docs-v0:@simatic-ax/json:JsonDocument.md}
METHOD PUBLIC Reset
VAR_TEMP
_currentElement : IJsonElementMuteable;
Expand Down
2 changes: 2 additions & 0 deletions src/Elements/Object/JsonObject.st
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
USING System.Strings;

NAMESPACE Simatic.Ax.Json
/// The Object contains nested JsonElements
{axcode:docs-v0:@simatic-ax/json:JsonObject.md}
CLASS FINAL JsonObject EXTENDS AbstractJsonElement IMPLEMENTS IJsonContainerElement

VAR PROTECTED
Expand Down

0 comments on commit c2c10a4

Please sign in to comment.