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

Commit 5647c0e

Browse files
Merge branch 'refactoring/aml-deserializer' of github.com:JensMueller2709/java-serializer into refactoring/aml-deserializer
2 parents 66d3028 + acf17d9 commit 5647c0e

File tree

25 files changed

+1594
-74
lines changed

25 files changed

+1594
-74
lines changed

dataformat-aml/pom.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,4 @@
138138
</plugin>
139139
</plugins>
140140
</build>
141-
<repositories>
142-
<repository>
143-
<id>eis-public-repo</id>
144-
<name>maven-public</name>
145-
<url>https://maven.iais.fraunhofer.de/artifactory/eis-ids-public</url>
146-
</repository>
147-
</repositories>
148141
</project>

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
}

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: 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: 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+
}

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@
1818
import java.util.List;
1919
import java.util.Objects;
2020

21-
import io.adminshell.aas.v3.model.Constraint;
22-
import io.adminshell.aas.v3.model.EmbeddedDataSpecification;
23-
import io.adminshell.aas.v3.model.LangString;
24-
import io.adminshell.aas.v3.model.ModelingKind;
25-
import io.adminshell.aas.v3.model.Property;
26-
import io.adminshell.aas.v3.model.Reference;
21+
import io.adminshell.aas.v3.model.*;
2722

2823
public class CustomProperty implements Property {
2924

@@ -49,6 +44,8 @@ public class CustomProperty implements Property {
4944

5045
protected String idShort;
5146

47+
protected List<Extension> extensions;
48+
5249
protected CustomProperty() {
5350
}
5451

@@ -188,4 +185,14 @@ final public Reference getSemanticId() {
188185
final public void setSemanticId(Reference semanticId) {
189186
this.semanticId = semanticId;
190187
}
188+
189+
@Override
190+
public List<Extension> getExtensions() {
191+
return extensions;
192+
}
193+
194+
@Override
195+
public void setExtensions(List<Extension> list) {
196+
this.extensions = list;
197+
}
191198
}
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+
}

0 commit comments

Comments
 (0)