|
| 1 | +# Usage |
| 2 | + |
| 3 | +The library and its modules are released at Maven Central. Thus, you can use the respective serializers in your Java Maven project by adding the following dependency: |
| 4 | + |
| 5 | +```xml |
| 6 | +<dependency> |
| 7 | + <groupId>io.admin-shell.aas</groupId> |
| 8 | + <artifactId>dataformat-**serializer**</artifactId> |
| 9 | + <version>**version**</version> |
| 10 | +<dependency> |
| 11 | +``` |
| 12 | + |
| 13 | +For example, uses the following dependency declaration to embed the `JSON dataformat`: |
| 14 | + |
| 15 | +```xml |
| 16 | +<dependency> |
| 17 | + <groupId>io.admin-shell.aas</groupId> |
| 18 | + <artifactId>dataformat-json</artifactId> |
| 19 | + <version>1.1.1</version> |
| 20 | +<dependency> |
| 21 | +``` |
| 22 | + |
| 23 | + |
| 24 | +> [!NOTE] |
| 25 | +> |
| 26 | +> Maven will automatically resolve the dependencies of the library and therefore also include the model implementation from the [java-model](https://github.com/admin-shell-io/java-model) project. |
| 27 | +
|
| 28 | +## Serialization |
| 29 | + |
| 30 | +The library supports serialization of an AAS environment to a `File`, `OutputStream` or `String`. See the following interface to get more details on what methods can be used to serialize an environment: |
| 31 | + |
| 32 | +https://github.com/admin-shell-io/java-serializer/blob/main/dataformat-core/src/main/java/io/adminshell/aas/v3/dataformat/Serializer.java |
| 33 | + |
| 34 | +Here is a small example on how to serialize a given environment to a JSON-`String`: |
| 35 | + |
| 36 | +```Java |
| 37 | +AssetAdministrationShellEnvironment env = ...; |
| 38 | +String serializedEnvironment = new JsonSerializer().write(env); |
| 39 | +``` |
| 40 | + |
| 41 | +## Deserialization |
| 42 | + |
| 43 | +Likewise, you can deserialize an AAS environment from a `File`, `InputStream` or `String`. See the following interface to get more details on what methods can be used to deserialize an environment: |
| 44 | + |
| 45 | +https://github.com/admin-shell-io/java-serializer/blob/main/dataformat-core/src/main/java/io/adminshell/aas/v3/dataformat/Deserializer.java |
| 46 | + |
| 47 | +Here is a small example on how to deserialize a given JSON-`String` and draw some information out of the returned model: |
| 48 | + |
| 49 | +```Java |
| 50 | +String serializedEnvironment = "..."; |
| 51 | +AssetAdministrationShellEnvironment env = new JsonDeserializer().read(serializedEnvironment); |
| 52 | +List<AssetAdministrationShell> aasList = env.getAssetAdministrationShells(); |
| 53 | +aasList.forEach(aas -> System.out.println("Environment contains AAS: " + aas.getIdShort())); |
| 54 | +``` |
| 55 | + |
| 56 | +## AASX |
| 57 | + |
| 58 | +In order to be able to also embed files into a serialized environment, it is possible to create an .aasx-package. The AASX module makes use of the xml-dataformat to handle the `AASEnvironment`. |
| 59 | + |
| 60 | +```Java |
| 61 | +byte[] fileContent = ...; |
| 62 | +AssetAdministrationShellEnvironment env = ...; |
| 63 | +InMemoryFile file = new InMemoryFile(fileContent, "aasx/MyFile.pdf"); |
| 64 | +List<InMemoryFile> fileList = new ArrayList<>() |
| 65 | +fileList.add(file); |
| 66 | +ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 67 | +new AASXSerializer().write(env, fileList, out); |
| 68 | +``` |
| 69 | + |
| 70 | +> [!NOTE] |
| 71 | +> |
| 72 | +> The given filepath is relative to the .aasx package root and has to correspondent to the relative path given in the `File`-`SubmodelElement` that points to this file. |
| 73 | +
|
| 74 | +## Constraint Validation |
| 75 | + |
| 76 | +According to the AAS specification in "Details of The Asset Administration Shell", there are a number of constraints a valid model has to fulfill. The java-model implementation allows the creation of models that are not valid according to these constraints. The java-dataformat library contains a validation module to test them. The following snippet shows a submodel with an invalid idShort. Therefore, this snippet will throw a `ValidationException`. |
| 77 | + |
| 78 | +```Java |
| 79 | +Referable invalidReferable = new DefaultSubmodel.Builder() |
| 80 | + .identification( |
| 81 | + new DefaultIdentifier.Builder().identifier("submodel").idType(IdentifierType.CUSTOM).build()) |
| 82 | + .idShort("_idShort") |
| 83 | + .build(); |
| 84 | +ShaclValidator.getInstance().validate(invalidReferable); |
| 85 | +``` |
0 commit comments