Skip to content
This repository was archived by the owner on Feb 15, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
*/
package io.adminshell.aas.v3.dataformat.json;

import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.module.SimpleAbstractTypeResolver;
Expand All @@ -31,11 +33,12 @@
import io.adminshell.aas.v3.dataformat.json.modeltype.ModelTypeProcessor;
import io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment;
import io.adminshell.aas.v3.model.EmbeddedDataSpecification;
import io.adminshell.aas.v3.model.Referable;

/**
* Class for deserializing/parsing AAS JSON documents.
*/
public class JsonDeserializer implements Deserializer {
public class JsonDeserializer implements Deserializer, ReferableDeserializer {

protected JsonMapper mapper;
protected SimpleAbstractTypeResolver typeResolver;
Expand Down Expand Up @@ -99,4 +102,23 @@ public <T> void useImplementation(Class<T> aasInterface, Class<? extends T> impl
typeResolver.addMapping(aasInterface, implementation);
buildMapper();
}

@Override
public <T extends Referable> T readReferable(String referable, Class<T> outputClass) throws DeserializationException {
try {
return mapper.treeToValue(ModelTypeProcessor.preprocess(referable), outputClass);
} catch (JsonProcessingException ex) {
throw new DeserializationException("error deserializing Referable", ex);
}
}

@Override
public <T extends Referable> List<T> readReferables(String referables, Class<T> outputClass) throws DeserializationException {
try {
String parsed = mapper.writeValueAsString(ModelTypeProcessor.preprocess(referables)) ;
return mapper.readValue(parsed,new TypeReference<List<T>>(){});
} catch (JsonProcessingException ex) {
throw new DeserializationException("error deserializing list of Referable", ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
Expand All @@ -30,12 +31,15 @@
import io.adminshell.aas.v3.dataformat.core.serialization.EmbeddedDataSpecificationSerializer;
import io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment;
import io.adminshell.aas.v3.model.EmbeddedDataSpecification;
import io.adminshell.aas.v3.model.Referable;

import java.util.List;

/**
* Class for serializing an instance of AssetAdministrationShellEnvironment to
* Class for serializing an instance of AssetAdministrationShellEnvironment or Referables to
* JSON.
*/
public class JsonSerializer implements Serializer {
public class JsonSerializer implements Serializer, ReferableSerializer {

protected JsonMapper mapper;

Expand Down Expand Up @@ -74,4 +78,27 @@ public String write(AssetAdministrationShellEnvironment aasEnvironment) throws S
throw new SerializationException("error serializing AssetAdministrationShellEnvironment", ex);
}
}

@Override
public String write(Referable referable) throws SerializationException {
try {
return mapper.writeValueAsString(ModelTypeProcessor.postprocess(mapper.valueToTree(referable)));
} catch (JsonProcessingException ex) {
throw new SerializationException("error serializing Referable", ex);
}
}

@Override
public String write(List<Referable> referables) throws SerializationException {
if(referables.isEmpty()){
return null;
}
try {
ObjectWriter objectWriter = mapper.writerFor(mapper.getTypeFactory().constructCollectionType(List.class, referables.get(0).getClass()));
String json = objectWriter.writeValueAsString(referables);
return mapper.writeValueAsString(ModelTypeProcessor.postprocess(this.mapper.readTree(json)));
} catch (JsonProcessingException ex) {
throw new SerializationException("error serializing list of Referables", ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2021 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e. V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.adminshell.aas.v3.dataformat.json;

import io.adminshell.aas.v3.dataformat.DeserializationException;
import io.adminshell.aas.v3.model.Referable;

import java.util.List;

/**
* Deserializer Interface for deserialization of referables
*/
public interface ReferableDeserializer {

/**
* Deserializes a given string into an instance of
* the given Referable
*
* @param referable a string representation of the
* Referable
* @param outputClass most specific class of the given Referable
* @param <T> type of the returned element
* @return an instance of the referable
* @throws DeserializationException
*/
<T extends Referable> T readReferable(String referable, Class<T> outputClass) throws DeserializationException;

/**
* Deserializes a given string into an instance of
* a list of the given Referables
*
* @param referables a string representation of an
* array of Referables
* @param outputClass most specific class of the given Referable
* @param <T> type of the returned element
* @return an instance of a list of the referables
* @throws DeserializationException
*/
<T extends Referable> List<T> readReferables(String referables, Class<T> outputClass) throws DeserializationException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2021 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e. V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.adminshell.aas.v3.dataformat.json;

import io.adminshell.aas.v3.dataformat.SerializationException;
import io.adminshell.aas.v3.model.Referable;

import java.util.List;

/**
* Serializer Interface for serialization of referables
*/
public interface ReferableSerializer {

/**
* Serializes a given instance of a Referable to string
*
* @param referable the referable to serialize
* @return the string representation of the referable
* @throws SerializationException if serialization fails
*/
String write(Referable referable) throws SerializationException;

/**
*
* @param referables the referables to serialize
* @return the string representation of the list of referables
* @throws SerializationException if serialization fails
*/
String write(List<Referable> referables) throws SerializationException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright (c) 2021 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e. V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.adminshell.aas.v3.dataformat.json;

import io.adminshell.aas.v3.dataformat.DeserializationException;
import io.adminshell.aas.v3.dataformat.Deserializer;
import io.adminshell.aas.v3.model.*;
import io.adminshell.aas.v3.model.impl.DefaultProperty;
import io.adminshell.aas.v3.model.impl.DefaultSubmodel;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class JsonReferableDeserializerTest {

private static final Logger logger = LoggerFactory.getLogger(JsonReferableDeserializerTest.class);

@Test
public void testReadAAS() throws IOException, DeserializationException {
File fileExpected = new File("src/test/resources/assetAdministrationShell.json");
String expected = Files.readString(fileExpected.toPath());
AssetAdministrationShell aas = new JsonDeserializer().readReferable(expected, AssetAdministrationShell.class);
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
AssetAdministrationShell aasExpected = environment.getAssetAdministrationShells().get(0);

assertEquals(aasExpected, aas);
}

@Test
public void testReadAASs() throws IOException, DeserializationException {
File fileExpected = new File("src/test/resources/assetAdministrationShellList.json");
String expected = Files.readString(fileExpected.toPath());
List<AssetAdministrationShell> aas = new JsonDeserializer().readReferables(expected, AssetAdministrationShell.class);
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
List<AssetAdministrationShell> aasExpected = Arrays.asList(environment.getAssetAdministrationShells().get(0)
,environment.getAssetAdministrationShells().get(1)) ;

assertEquals(aasExpected, aas);
}

@Test
public void testReadSubmodel() throws IOException, DeserializationException {
File fileExpected = new File("src/test/resources/submodel.json");
String expected = Files.readString(fileExpected.toPath());
Submodel submodel = new JsonDeserializer().readReferable(expected,Submodel.class);
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
Submodel submodelExpected = environment.getSubmodels().get(0);

assertEquals(submodelExpected, submodel);
}

@Test
public void testReadSubmodels() throws IOException, DeserializationException {
File fileExpected = new File("src/test/resources/submodelList.json");
String expected = Files.readString(fileExpected.toPath());
List<Submodel> submodels = new JsonDeserializer().readReferables(expected,Submodel.class);
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
List<Submodel> submodelsExpected = Arrays.asList(environment.getSubmodels().get(0),environment.getSubmodels().get(1));

assertEquals(submodelsExpected, submodels);
}

@Test
public void testReadSubmodelElement() throws IOException, DeserializationException {
File fileExpected = new File("src/test/resources/submodelElement.json");
String expected = Files.readString(fileExpected.toPath());
SubmodelElement submodelElement = new JsonDeserializer().readReferable(expected,SubmodelElement.class);
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
SubmodelElement submodelElementExpected = environment.getSubmodels().get(0).getSubmodelElements().get(0);

assertEquals(submodelElementExpected, submodelElement);
}

@Test
public void testReadSubmodelElements() throws IOException, DeserializationException {
File fileExpected = new File("src/test/resources/submodelElementList.json");
String expected = Files.readString(fileExpected.toPath());
List<SubmodelElement> submodelElements = new JsonDeserializer().readReferables(expected,SubmodelElement.class);
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
List<SubmodelElement> submodelElementsExpected = Arrays.asList(
environment.getSubmodels().get(0).getSubmodelElements().get(0),
environment.getSubmodels().get(0).getSubmodelElements().get(1)); ;

assertEquals(submodelElementsExpected, submodelElements);
}

@Test
public void testReadSubmodelElementCollection() throws IOException, DeserializationException {
File fileExpected = new File("src/test/resources/submodelElementCollection.json");
String expected = Files.readString(fileExpected.toPath());
SubmodelElementCollection submodelElementCollection = new JsonDeserializer().readReferable(expected,SubmodelElementCollection.class);
AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT;
SubmodelElement submodelElementCollectionExpected = environment.getSubmodels().get(6).getSubmodelElements().get(5); ;

assertEquals(submodelElementCollectionExpected, submodelElementCollection);
}

}
Loading