-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: implement asset information serialisation #163
Draft
emildinchev
wants to merge
19
commits into
eclipse-aas4j:main
Choose a base branch
from
sap-contributions:feature/asset-information-serialiser
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
75f6000
cherry pick the assetinformation serialiser
sebbader-sap fba7d7f
fix: correct wrong model version
sebbader-sap 13a3327
Update the model
emildinchev 5304b47
Fix the compilation errors
emildinchev 5b30938
Fix the JSON serialization/deserialization tests
emildinchev 65e70f9
Update JsonSerializer.java
emildinchev 166bd38
Merge pull request #7 from sap-contributions/tmp
emildinchev 389c68a
Fix the model and compile errors
emildinchev b3b937b
Update AssetAdministrationShellElementWalkerVisitor.java
emildinchev 98c6f93
Update EnumSerializer.java
emildinchev 431ca58
Update AASFull.java
emildinchev d3f2e03
Update AssetInformationMixin.java
emildinchev 5447f5c
Cosmetics
emildinchev b272f50
Cosmetics
emildinchev 8a6f9b6
Update SpecificAssetIDMixin.java
emildinchev f94b532
Update XmlSerializerTest.java
emildinchev fd8c081
Update XMLDeserializerTest.java
emildinchev f8c1466
Create test suite to order the tests execution
emildinchev be347ee
Execute only the test suite during the build of dataformat-xml
emildinchev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...n/src/main/java/org/eclipse/digitaltwin/aas4j/v3/dataformat/json/ObjectMapperFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package org.eclipse.digitaltwin.aas4j.v3.dataformat.json; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
import com.fasterxml.jackson.databind.module.SimpleAbstractTypeResolver; | ||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
import org.eclipse.digitaltwin.aas4j.v3.dataformat.core.deserialization.EnumDeserializer; | ||
import org.eclipse.digitaltwin.aas4j.v3.dataformat.core.serialization.EnumSerializer; | ||
import org.eclipse.digitaltwin.aas4j.v3.dataformat.core.util.ReflectionHelper; | ||
|
||
/** | ||
* This class contains the initialization code used by the JsonSerializer and JsonDeserializer. It also contains a | ||
* factory method, that is very useful to be used in SpringBoot environment, to automate the serialization. Here is a | ||
* sample code: | ||
* <pre> | ||
* {@code @Configuration} | ||
* public class JacksonConfig { | ||
* {@code @Bean} | ||
* {@code @Primary} | ||
* public ObjectMapper objectMapper() { | ||
* return ObjectMapperFactory.createMapper(); | ||
* } | ||
* </pre> | ||
* Having such JacksonConfig class in your SpringBoot web application, the framework will always use the created mapper | ||
* for serialization and deserialization, and you don't need to carry about it anymore. | ||
*/ | ||
public class ObjectMapperFactory { | ||
private ObjectMapperFactory() {} | ||
|
||
/** | ||
* The factory method to create the object mapper for serialization/deserialization of AAS objects. | ||
* @return the configured object mapper. According to the Jackson documentation the ObjectMapper class is | ||
* thread-safe. | ||
* | ||
* The returned object mapper can be used directly for AAS serialization/deserialization, or you can use the utility | ||
* JsonSerializer and JsonDeserializer wrapper classes. | ||
*/ | ||
public static ObjectMapper createMapper() { | ||
return createMapper(null); | ||
} | ||
|
||
/** | ||
* A package-private method used by JsonDeserializer wrapper class. It is needed only to support its | ||
* useImplementation method, which require modification of the type resolver. | ||
* @param typeResolver The type resolver used for deserialization. | ||
* @return the configured object mapper. According to the Jackson documentation the ObjectMapper class is | ||
* thread-safe. | ||
*/ | ||
static ObjectMapper createMapper(SimpleAbstractTypeResolver typeResolver) { | ||
ObjectMapper mapper = JsonMapper.builder() | ||
.enable(SerializationFeature.INDENT_OUTPUT) | ||
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY) | ||
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) | ||
.serializationInclusion(JsonInclude.Include.NON_NULL) | ||
.serializationInclusion(JsonInclude.Include.NON_EMPTY) | ||
.annotationIntrospector(new ReflectionAnnotationIntrospector()) | ||
.addModule(buildEnumModule()) | ||
.addModule(buildTypeResolverModule(typeResolver)) | ||
.build(); | ||
ReflectionHelper.JSON_MIXINS.entrySet().forEach(x -> mapper.addMixIn(x.getKey(), x.getValue())); | ||
return mapper; | ||
} | ||
|
||
/** | ||
* A package-private method, to create a type resolver, which is used for serialization/deserialization of AAS | ||
* object. | ||
* @return the type resolver. | ||
*/ | ||
static SimpleAbstractTypeResolver createTypeResolver() { | ||
SimpleAbstractTypeResolver typeResolver = new SimpleAbstractTypeResolver(); | ||
ReflectionHelper.DEFAULT_IMPLEMENTATIONS | ||
.stream() | ||
.forEach(x -> typeResolver.addMapping(x.getInterfaceType(), x.getImplementationType())); | ||
return typeResolver; | ||
} | ||
|
||
private static SimpleModule buildTypeResolverModule(SimpleAbstractTypeResolver typeResolver) { | ||
final SimpleModule module = new SimpleModule(); | ||
if(typeResolver == null) { | ||
typeResolver = createTypeResolver(); | ||
} | ||
module.setAbstractTypes(typeResolver); | ||
return module; | ||
} | ||
|
||
private static SimpleModule buildEnumModule() { | ||
SimpleModule module = new SimpleModule(); | ||
ReflectionHelper.ENUMS.forEach(x -> { | ||
module.addDeserializer(x, new EnumDeserializer<>(x)); | ||
module.addSerializer(x, new EnumSerializer()); | ||
}); | ||
return module; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have decided that it is better to decrease the class visibility to package-private. In such way the class is visible only from dataformat-json project. But it needs to be in the same package as ObjectMapperFactory. |
||
* Copyright (c) 2021 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e. V. | ||
* Copyright (C) 2023 SAP SE or an SAP affiliate company. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -13,7 +14,7 @@ | |
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.eclipse.digitaltwin.aas4j.v3.dataformat.json.internal; | ||
package org.eclipse.digitaltwin.aas4j.v3.dataformat.json; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
|
@@ -31,7 +32,7 @@ | |
import java.util.stream.Collectors; | ||
|
||
/** | ||
* This class helps to dynamically decide how to de-/serialize classes and | ||
* This utility class has package-private visibility and helps to dynamically decide how to de-/serialize classes and | ||
* properties defined in the AAS model library. | ||
* | ||
* This is equivalent to adding the following annotations | ||
|
@@ -52,7 +53,7 @@ | |
* </ul> | ||
* </ul> | ||
*/ | ||
public class ReflectionAnnotationIntrospector extends JacksonAnnotationIntrospector { | ||
class ReflectionAnnotationIntrospector extends JacksonAnnotationIntrospector { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
|
@@ -108,5 +109,4 @@ public JsonInclude.Value findPropertyInclusion(Annotated a) { | |
} | ||
return result; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my opinion the ObjectMapper created by this class can fully replace the JsonSerializer and JsonDeserializer.