aas-core-works / aas-core3.0-csharp

Manipulate, verify and de/serialize asset administration shells in C#.
Other
7 stars 1 forks source link

[Question] how to work with jsonazation with list of AssetAdministrationShell returned from shells endpoint? #23

Closed EmilAlipiev closed 1 year ago

EmilAlipiev commented 1 year ago

Hi, I am trying to understand how I can use the jsonazation. for example, using the endpoint https://v3security.admin-shell-io.com/shells, It returns a list of AssetAdministrationShell with a paging information. So like below code in c#, i get the result as string and parse as jsonNode, then use Jsonization to deserialize it. But which function, i couldnt figure out? I mean to say that Deserialize functions doesnt return any list. Here i will neeed a list of AAS. There is only 1 single AAS. If you can enlighten me, I appreciate.

var result = await Api.GetShellList();

var jsonNode = System.Text.Json.Nodes.JsonNode.Parse(result);
// De-serialize from the JSON node
var shells = AasCore.Aas3_0.Jsonization.Deserialize.AssetAdministrationShellFrom(jsonNode);

edit @mristin: fixed formatting

mristin commented 1 year ago

Hi @EmilAlipiev,

You have to cast the node to a JSON array, and then parse each item individually:

var jsonArray = (
    jsonNode as System.Text.Json.Nodes.JsonArray
)  ?? throw new System.InvalidCastException("Expected a JSON array");

foreach (var shellJson in jsonArray) {
    var shell = AasCore.Aas3_0.Jsonization.Deserialize.AssetAdministrationShellFrom(
        shellJson
    );

    // Do something with `shell`
}

Please feel free to re-open the issue if you have further questions.

EmilAlipiev commented 1 year ago

@mristin thanks for the quick answer. thats what we thought also and doing it with a foreach. But is that not very performance-expensive if there are many shells in the list? Here all I want to get is basic information like "IdShort". I dont know if this was foreseen before but can we possibly get a de/serializer for such list object?

Beside that; I was examining the source code of "AasxServer Blazor" and there this nuget/library is not used directly but used through "AasxCsharpLibrary" which converts the things in between like lists etc. Is that library was designed only for this application? I think that it is written by same team. thats why asking here. thanks :)

mristin commented 1 year ago

@EmilAlipiev

But is that not very performance-expensive if there are many shells in the list? Here all I want to get is basic information like "IdShort". I dont know if this was foreseen before but can we possibly get a de/serializer for such list object?

You have to test, but I assume the de-serialization from text to JSON eclipses the transformation JSON nodes -> AAS C# objects.

If you only need ID-shorts, and you don't care about other information, your best bet is probably just to parse the information directly from the JSON nodes.

Beside that; I was examining the source code of "AasxServer Blazor" and there this nuget/library is not used directly but used through "AasxCsharpLibrary" which converts the things in between like lists etc. Is that library was designed only for this application?

The library has been designed for general reading and manipulation of AAS data, including AAS servers. It is a foundational building block, but not the server implementation itself.

Additionally, aasx-server predates aas-core. It originally used C# code from AASX Package Explorer, but eventually moved to aas-core.

I think that it is written by same team. thats why asking here. thanks :)

Aasx-server is one of the users of aas-core, but aas-core has not been developed only for aasx-server.

It is also not the same people. The aasx-server is being developed by a couple of Fraunhofer institutes (mainly IOSB-INA), while the contributors to aas-core are mainly ZHAW, TU Dresden, and RWTH Aachen. We all know each other and collaborate tightly, though, and some of the features required by aasx-server were developed in aas-core (such as object wrapping/enhancing).