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

Commit 8ce8c67

Browse files
Add json deserialization of single referable and of list of referables
1 parent f2b3456 commit 8ce8c67

4 files changed

Lines changed: 207 additions & 12 deletions

File tree

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
*/
1616
package io.adminshell.aas.v3.dataformat.json;
1717

18+
import java.util.List;
1819
import java.util.Map;
1920

2021
import com.fasterxml.jackson.core.JsonProcessingException;
22+
import com.fasterxml.jackson.core.type.TypeReference;
2123
import com.fasterxml.jackson.databind.DeserializationFeature;
2224
import com.fasterxml.jackson.databind.json.JsonMapper;
2325
import com.fasterxml.jackson.databind.module.SimpleAbstractTypeResolver;
@@ -31,11 +33,12 @@
3133
import io.adminshell.aas.v3.dataformat.json.modeltype.ModelTypeProcessor;
3234
import io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment;
3335
import io.adminshell.aas.v3.model.EmbeddedDataSpecification;
36+
import io.adminshell.aas.v3.model.Referable;
3437

3538
/**
3639
* Class for deserializing/parsing AAS JSON documents.
3740
*/
38-
public class JsonDeserializer implements Deserializer {
41+
public class JsonDeserializer implements Deserializer, ReferableDeserializer {
3942

4043
protected JsonMapper mapper;
4144
protected SimpleAbstractTypeResolver typeResolver;
@@ -99,4 +102,23 @@ public <T> void useImplementation(Class<T> aasInterface, Class<? extends T> impl
99102
typeResolver.addMapping(aasInterface, implementation);
100103
buildMapper();
101104
}
105+
106+
@Override
107+
public <T extends Referable> T readReferable(String referable, Class<T> outputClass) throws DeserializationException {
108+
try {
109+
return mapper.treeToValue(ModelTypeProcessor.preprocess(referable), outputClass);
110+
} catch (JsonProcessingException ex) {
111+
throw new DeserializationException("error deserializing Referable", ex);
112+
}
113+
}
114+
115+
@Override
116+
public <T extends Referable> List<T> readReferables(String referables, Class<T> outputClass) throws DeserializationException {
117+
try {
118+
String parsed = mapper.writeValueAsString(ModelTypeProcessor.preprocess(referables)) ;
119+
return mapper.readValue(parsed,new TypeReference<List<T>>(){});
120+
} catch (JsonProcessingException ex) {
121+
throw new DeserializationException("error deserializing list of Referable", ex);
122+
}
123+
}
102124
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.DeserializationException;
19+
import io.adminshell.aas.v3.model.Referable;
20+
21+
import java.util.List;
22+
23+
/**
24+
* Deserializer Interface for deserialization of referables
25+
*/
26+
public interface ReferableDeserializer {
27+
28+
/**
29+
* Deserializes a given string into an instance of
30+
* the given Referable
31+
*
32+
* @param referable a string representation of the
33+
* Referable
34+
* @param outputClass most specific class of the given Referable
35+
* @param <T> type of the returned element
36+
* @return an instance of the referable
37+
* @throws DeserializationException
38+
*/
39+
<T extends Referable> T readReferable(String referable, Class<T> outputClass) throws DeserializationException;
40+
41+
/**
42+
* Deserializes a given string into an instance of
43+
* a list of the given Referables
44+
*
45+
* @param referables a string representation of an
46+
* array of Referables
47+
* @param outputClass most specific class of the given Referable
48+
* @param <T> type of the returned element
49+
* @return an instance of a list of the referables
50+
* @throws DeserializationException
51+
*/
52+
<T extends Referable> List<T> readReferables(String referables, Class<T> outputClass) throws DeserializationException;
53+
54+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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.DeserializationException;
19+
import io.adminshell.aas.v3.dataformat.Deserializer;
20+
import io.adminshell.aas.v3.model.*;
21+
import io.adminshell.aas.v3.model.impl.DefaultProperty;
22+
import io.adminshell.aas.v3.model.impl.DefaultSubmodel;
23+
import org.junit.Test;
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+
import static org.junit.Assert.assertEquals;
34+
35+
public class JsonReferableDeserializerTest {
36+
37+
private static final Logger logger = LoggerFactory.getLogger(JsonReferableDeserializerTest.class);
38+
39+
@Test
40+
public void testReadAAS() throws IOException, DeserializationException {
41+
File fileExpected = new File("src/test/resources/assetAdministrationShell.json");
42+
String expected = Files.readString(fileExpected.toPath());
43+
AssetAdministrationShell aas = new JsonDeserializer().readReferable(expected, AssetAdministrationShell.class);
44+
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
45+
AssetAdministrationShell aasExpected = environment.getAssetAdministrationShells().get(0);
46+
47+
assertEquals(aasExpected, aas);
48+
}
49+
50+
@Test
51+
public void testReadAASs() throws IOException, DeserializationException {
52+
File fileExpected = new File("src/test/resources/assetAdministrationShellList.json");
53+
String expected = Files.readString(fileExpected.toPath());
54+
List<AssetAdministrationShell> aas = new JsonDeserializer().readReferables(expected, AssetAdministrationShell.class);
55+
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
56+
List<AssetAdministrationShell> aasExpected = Arrays.asList(environment.getAssetAdministrationShells().get(0)
57+
,environment.getAssetAdministrationShells().get(1)) ;
58+
59+
assertEquals(aasExpected, aas);
60+
}
61+
62+
@Test
63+
public void testReadSubmodel() throws IOException, DeserializationException {
64+
File fileExpected = new File("src/test/resources/submodel.json");
65+
String expected = Files.readString(fileExpected.toPath());
66+
Submodel submodel = new JsonDeserializer().readReferable(expected,Submodel.class);
67+
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
68+
Submodel submodelExpected = environment.getSubmodels().get(0);
69+
70+
assertEquals(submodelExpected, submodel);
71+
}
72+
73+
@Test
74+
public void testReadSubmodels() throws IOException, DeserializationException {
75+
File fileExpected = new File("src/test/resources/submodelList.json");
76+
String expected = Files.readString(fileExpected.toPath());
77+
List<Submodel> submodels = new JsonDeserializer().readReferables(expected,Submodel.class);
78+
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
79+
List<Submodel> submodelsExpected = Arrays.asList(environment.getSubmodels().get(0),environment.getSubmodels().get(1));
80+
81+
assertEquals(submodelsExpected, submodels);
82+
}
83+
84+
@Test
85+
public void testReadSubmodelElement() throws IOException, DeserializationException {
86+
File fileExpected = new File("src/test/resources/submodelElement.json");
87+
String expected = Files.readString(fileExpected.toPath());
88+
SubmodelElement submodelElement = new JsonDeserializer().readReferable(expected,SubmodelElement.class);
89+
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
90+
SubmodelElement submodelElementExpected = environment.getSubmodels().get(0).getSubmodelElements().get(0);
91+
92+
assertEquals(submodelElementExpected, submodelElement);
93+
}
94+
95+
@Test
96+
public void testReadSubmodelElements() throws IOException, DeserializationException {
97+
File fileExpected = new File("src/test/resources/submodelElementList.json");
98+
String expected = Files.readString(fileExpected.toPath());
99+
List<SubmodelElement> submodelElements = new JsonDeserializer().readReferables(expected,SubmodelElement.class);
100+
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
101+
List<SubmodelElement> submodelElementsExpected = Arrays.asList(
102+
environment.getSubmodels().get(0).getSubmodelElements().get(0),
103+
environment.getSubmodels().get(0).getSubmodelElements().get(1)); ;
104+
105+
assertEquals(submodelElementsExpected, submodelElements);
106+
}
107+
108+
@Test
109+
public void testReadSubmodelElementCollection() throws IOException, DeserializationException {
110+
File fileExpected = new File("src/test/resources/submodelElementCollection.json");
111+
String expected = Files.readString(fileExpected.toPath());
112+
SubmodelElementCollection submodelElementCollection = new JsonDeserializer().readReferable(expected,SubmodelElementCollection.class);
113+
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
114+
SubmodelElement submodelElementCollectionExpected = environment.getSubmodels().get(6).getSubmodelElements().get(5); ;
115+
116+
assertEquals(submodelElementCollectionExpected, submodelElementCollection);
117+
}
118+
119+
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,52 +38,52 @@ public class JsonReferableSerializerTest {
3838
public void testSerializeAAS() throws IOException, SerializationException, JSONException {
3939
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
4040
AssetAdministrationShell assetAdministrationShell = environment.getAssetAdministrationShells().get(0);
41-
validateAndCompare("src/test/resources/assetAdministrationShell.json",assetAdministrationShell);
41+
compare("src/test/resources/assetAdministrationShell.json",assetAdministrationShell);
4242
}
4343

4444
@Test
45-
public void testSerializeMultipleAAS() throws IOException, SerializationException, JSONException {
45+
public void testSerializeAASs() throws IOException, SerializationException, JSONException {
4646
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
47-
validateAndCompare("src/test/resources/assetAdministrationShellList.json",
47+
compare("src/test/resources/assetAdministrationShellList.json",
4848
environment.getAssetAdministrationShells().get(0), environment.getAssetAdministrationShells().get(1));
4949
}
5050

5151
@Test
5252
public void testSerializeSubmodel() throws IOException, SerializationException, JSONException {
5353
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
5454
Submodel submodel = environment.getSubmodels().get(0);
55-
validateAndCompare("src/test/resources/submodel.json",submodel);
55+
compare("src/test/resources/submodel.json",submodel);
5656
}
5757

5858
@Test
59-
public void testSerializeMultipleSubmodels() throws IOException, SerializationException, JSONException {
59+
public void testSerializeSubmodels() throws IOException, SerializationException, JSONException {
6060
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
61-
validateAndCompare("src/test/resources/submodelList.json", environment.getSubmodels().get(0), environment.getSubmodels().get(1));
61+
compare("src/test/resources/submodelList.json", environment.getSubmodels().get(0), environment.getSubmodels().get(1));
6262
}
6363

6464
@Test
6565
public void testSerializeSubmodelelement() throws IOException, SerializationException, JSONException {
6666
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
6767
SubmodelElement submodelElement = environment.getSubmodels().get(0).getSubmodelElements().get(0);
68-
validateAndCompare("src/test/resources/submodelElement.json",submodelElement);
68+
compare("src/test/resources/submodelElement.json",submodelElement);
6969
}
7070

7171
@Test
72-
public void testSerializeMultipleSubmodelelements() throws IOException, SerializationException, JSONException {
72+
public void testSerializeSubmodelelements() throws IOException, SerializationException, JSONException {
7373
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
7474
SubmodelElement submodelElement0 = environment.getSubmodels().get(0).getSubmodelElements().get(0);
7575
SubmodelElement submodelElement1 = environment.getSubmodels().get(0).getSubmodelElements().get(1);
76-
validateAndCompare("src/test/resources/submodelElementList.json",submodelElement0,submodelElement1);
76+
compare("src/test/resources/submodelElementList.json",submodelElement0,submodelElement1);
7777
}
7878

7979
@Test
8080
public void testSerializeSubmodelelementCollection() throws IOException, SerializationException, JSONException {
8181
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
8282
SubmodelElement submodelElementCollection = environment.getSubmodels().get(6).getSubmodelElements().get(5);
83-
validateAndCompare("src/test/resources/submodelElementCollection.json",submodelElementCollection);
83+
compare("src/test/resources/submodelElementCollection.json",submodelElementCollection);
8484
}
8585

86-
private void validateAndCompare(String filePathForExpected, Referable... referable) throws IOException, SerializationException, JSONException {
86+
private void compare(String filePathForExpected, Referable... referable) throws IOException, SerializationException, JSONException {
8787
File fileExpected = new File(filePathForExpected);
8888
String expected = Files.readString(fileExpected.toPath());
8989
String actual;

0 commit comments

Comments
 (0)