Skip to content
This repository was archived by the owner on Feb 15, 2024. It is now read-only.

Commit f2b3456

Browse files
Add json serialization of single referable and of list of referables
1 parent dcd741c commit f2b3456

11 files changed

Lines changed: 1251 additions & 3 deletions

File tree

dataformat-json/src/main/java/io/adminshell/aas/v3/dataformat/json/JsonSerializer.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.fasterxml.jackson.annotation.JsonInclude;
1919
import com.fasterxml.jackson.core.JsonProcessingException;
2020
import com.fasterxml.jackson.databind.DeserializationFeature;
21+
import com.fasterxml.jackson.databind.ObjectWriter;
2122
import com.fasterxml.jackson.databind.SerializationFeature;
2223
import com.fasterxml.jackson.databind.json.JsonMapper;
2324
import com.fasterxml.jackson.databind.module.SimpleModule;
@@ -30,12 +31,15 @@
3031
import io.adminshell.aas.v3.dataformat.core.serialization.EmbeddedDataSpecificationSerializer;
3132
import io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment;
3233
import io.adminshell.aas.v3.model.EmbeddedDataSpecification;
34+
import io.adminshell.aas.v3.model.Referable;
35+
36+
import java.util.List;
3337

3438
/**
35-
* Class for serializing an instance of AssetAdministrationShellEnvironment to
39+
* Class for serializing an instance of AssetAdministrationShellEnvironment or Referables to
3640
* JSON.
3741
*/
38-
public class JsonSerializer implements Serializer {
42+
public class JsonSerializer implements Serializer, ReferableSerializer {
3943

4044
protected JsonMapper mapper;
4145

@@ -74,4 +78,27 @@ public String write(AssetAdministrationShellEnvironment aasEnvironment) throws S
7478
throw new SerializationException("error serializing AssetAdministrationShellEnvironment", ex);
7579
}
7680
}
81+
82+
@Override
83+
public String write(Referable referable) throws SerializationException {
84+
try {
85+
return mapper.writeValueAsString(ModelTypeProcessor.postprocess(mapper.valueToTree(referable)));
86+
} catch (JsonProcessingException ex) {
87+
throw new SerializationException("error serializing Referable", ex);
88+
}
89+
}
90+
91+
@Override
92+
public String write(List<Referable> referables) throws SerializationException {
93+
if(referables.isEmpty()){
94+
return null;
95+
}
96+
try {
97+
ObjectWriter objectWriter = mapper.writerFor(mapper.getTypeFactory().constructCollectionType(List.class, referables.get(0).getClass()));
98+
String json = objectWriter.writeValueAsString(referables);
99+
return mapper.writeValueAsString(ModelTypeProcessor.postprocess(this.mapper.readTree(json)));
100+
} catch (JsonProcessingException ex) {
101+
throw new SerializationException("error serializing list of Referables", ex);
102+
}
103+
}
77104
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2021 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e. V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.adminshell.aas.v3.dataformat.json;
17+
18+
import io.adminshell.aas.v3.dataformat.SerializationException;
19+
import io.adminshell.aas.v3.model.Referable;
20+
21+
import java.util.List;
22+
23+
/**
24+
* Serializer Interface for serialization of referables
25+
*/
26+
public interface ReferableSerializer {
27+
28+
/**
29+
* Serializes a given instance of a Referable to string
30+
*
31+
* @param referable the referable to serialize
32+
* @return the string representation of the referable
33+
* @throws SerializationException if serialization fails
34+
*/
35+
String write(Referable referable) throws SerializationException;
36+
37+
/**
38+
*
39+
* @param referables the referables to serialize
40+
* @return the string representation of the list of referables
41+
* @throws SerializationException if serialization fails
42+
*/
43+
String write(List<Referable> referables) throws SerializationException;
44+
45+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (c) 2021 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e. V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.adminshell.aas.v3.dataformat.json;
17+
18+
import io.adminshell.aas.v3.dataformat.SerializationException;
19+
import io.adminshell.aas.v3.model.*;
20+
import org.json.JSONException;
21+
import org.junit.Test;
22+
import org.skyscreamer.jsonassert.JSONAssert;
23+
import org.skyscreamer.jsonassert.JSONCompareMode;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
27+
import java.io.File;
28+
import java.io.IOException;
29+
import java.nio.file.Files;
30+
import java.util.Arrays;
31+
import java.util.List;
32+
33+
public class JsonReferableSerializerTest {
34+
35+
private static final Logger logger = LoggerFactory.getLogger(JsonReferableSerializerTest.class);
36+
37+
@Test
38+
public void testSerializeAAS() throws IOException, SerializationException, JSONException {
39+
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
40+
AssetAdministrationShell assetAdministrationShell = environment.getAssetAdministrationShells().get(0);
41+
validateAndCompare("src/test/resources/assetAdministrationShell.json",assetAdministrationShell);
42+
}
43+
44+
@Test
45+
public void testSerializeMultipleAAS() throws IOException, SerializationException, JSONException {
46+
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
47+
validateAndCompare("src/test/resources/assetAdministrationShellList.json",
48+
environment.getAssetAdministrationShells().get(0), environment.getAssetAdministrationShells().get(1));
49+
}
50+
51+
@Test
52+
public void testSerializeSubmodel() throws IOException, SerializationException, JSONException {
53+
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
54+
Submodel submodel = environment.getSubmodels().get(0);
55+
validateAndCompare("src/test/resources/submodel.json",submodel);
56+
}
57+
58+
@Test
59+
public void testSerializeMultipleSubmodels() throws IOException, SerializationException, JSONException {
60+
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
61+
validateAndCompare("src/test/resources/submodelList.json", environment.getSubmodels().get(0), environment.getSubmodels().get(1));
62+
}
63+
64+
@Test
65+
public void testSerializeSubmodelelement() throws IOException, SerializationException, JSONException {
66+
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
67+
SubmodelElement submodelElement = environment.getSubmodels().get(0).getSubmodelElements().get(0);
68+
validateAndCompare("src/test/resources/submodelElement.json",submodelElement);
69+
}
70+
71+
@Test
72+
public void testSerializeMultipleSubmodelelements() throws IOException, SerializationException, JSONException {
73+
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
74+
SubmodelElement submodelElement0 = environment.getSubmodels().get(0).getSubmodelElements().get(0);
75+
SubmodelElement submodelElement1 = environment.getSubmodels().get(0).getSubmodelElements().get(1);
76+
validateAndCompare("src/test/resources/submodelElementList.json",submodelElement0,submodelElement1);
77+
}
78+
79+
@Test
80+
public void testSerializeSubmodelelementCollection() throws IOException, SerializationException, JSONException {
81+
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
82+
SubmodelElement submodelElementCollection = environment.getSubmodels().get(6).getSubmodelElements().get(5);
83+
validateAndCompare("src/test/resources/submodelElementCollection.json",submodelElementCollection);
84+
}
85+
86+
private void validateAndCompare(String filePathForExpected, Referable... referable) throws IOException, SerializationException, JSONException {
87+
File fileExpected = new File(filePathForExpected);
88+
String expected = Files.readString(fileExpected.toPath());
89+
String actual;
90+
if(referable.length>1){
91+
actual = new JsonSerializer().write(List.of(referable));
92+
} else {
93+
actual = new JsonSerializer().write(Arrays.stream(referable).findFirst().get());
94+
}
95+
logger.info(actual);
96+
97+
JSONAssert.assertEquals(expected, actual, JSONCompareMode.NON_EXTENSIBLE);
98+
JSONAssert.assertEquals(actual, expected, JSONCompareMode.NON_EXTENSIBLE);
99+
100+
}
101+
102+
}

dataformat-json/src/test/java/io/adminshell/aas/v3/dataformat/json/JsonSerializerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class JsonSerializerTest {
4747

4848
@Test
4949
public void testSerializeNull() throws JsonProcessingException, IOException, SerializationException {
50-
assertEquals("null", new JsonSerializer().write(null));
50+
assertEquals("null", new JsonSerializer().write((AssetAdministrationShellEnvironment) null));
5151
}
5252

5353
@Test
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
{
2+
"idShort": "TestAssetAdministrationShell",
3+
"description": [
4+
{
5+
"language": "en-us",
6+
"text": "An Example Asset Administration Shell for the test application"
7+
},
8+
{
9+
"language": "de",
10+
"text": "Ein Beispiel-Verwaltungsschale für eine Test-Anwendung"
11+
}
12+
],
13+
"modelType":
14+
{
15+
"name": "AssetAdministrationShell"
16+
},
17+
"identification":
18+
{
19+
"id": "https://acplt.org/Test_AssetAdministrationShell",
20+
"idType": "Iri"
21+
},
22+
"administration":
23+
{
24+
"version": "0.9",
25+
"revision": "0"
26+
},
27+
"derivedFrom":
28+
{
29+
"keys": [
30+
{
31+
"type": "AssetAdministrationShell",
32+
"idType": "Iri",
33+
"value": "https://acplt.org/TestAssetAdministrationShell2"
34+
}
35+
]
36+
},
37+
"assetInformation":
38+
{
39+
"assetKind": "Instance",
40+
"globalAssetId":
41+
{
42+
"keys": [
43+
{
44+
"type": "Asset",
45+
"idType": "Iri",
46+
"value": "https://acplt.org/Test_Asset"
47+
}
48+
]
49+
},
50+
"billOfMaterial": [
51+
{
52+
"keys": [
53+
{
54+
"type": "Submodel",
55+
"idType": "Iri",
56+
"value": "http://acplt.org/Submodels/Assets/TestAsset/BillOfMaterial"
57+
}
58+
]
59+
}
60+
]
61+
},
62+
"submodels": [
63+
{
64+
"keys": [
65+
{
66+
"type": "Submodel",
67+
"idType": "Iri",
68+
"value": "https://acplt.org/Test_Submodel"
69+
}
70+
]
71+
},
72+
{
73+
"keys": [
74+
{
75+
"type": "Submodel",
76+
"idType": "Iri",
77+
"value": "http://acplt.org/Submodels/Assets/TestAsset/BillOfMaterial"
78+
}
79+
]
80+
},
81+
{
82+
"keys": [
83+
{
84+
"type": "Submodel",
85+
"idType": "Iri",
86+
"value": "http://acplt.org/Submodels/Assets/TestAsset/Identification"
87+
}
88+
]
89+
}
90+
]
91+
}

0 commit comments

Comments
 (0)