diff options
author | Thomas <> | 2024-02-01 17:24:48 +0100 |
---|---|---|
committer | Thomas <> | 2024-02-01 17:24:48 +0100 |
commit | c9886d89daa2a36b4cd96f4e956ef9e59aee291c (patch) | |
tree | f3e4557f40b82b09e650c0d7a2859e8e2f01b213 /eaaf_core | |
parent | b6a931c21927675491af40bc32cebfa5d432124e (diff) | |
download | EAAF-Components-c9886d89daa2a36b4cd96f4e956ef9e59aee291c.tar.gz EAAF-Components-c9886d89daa2a36b4cd96f4e956ef9e59aee291c.tar.bz2 EAAF-Components-c9886d89daa2a36b4cd96f4e956ef9e59aee291c.zip |
feat(core): add Jackson Joda module
Hint: Modules will be only loaded if it is available on classpath
Diffstat (limited to 'eaaf_core')
-rw-r--r-- | eaaf_core/pom.xml | 7 | ||||
-rw-r--r-- | eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/utils/DefaultJsonMapper.java | 66 |
2 files changed, 70 insertions, 3 deletions
diff --git a/eaaf_core/pom.xml b/eaaf_core/pom.xml index 3b137720..1859e165 100644 --- a/eaaf_core/pom.xml +++ b/eaaf_core/pom.xml @@ -61,7 +61,12 @@ <groupId>com.fasterxml.jackson.module</groupId> <artifactId>jackson-module-parameter-names</artifactId> <scope>provided</scope> - </dependency> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.datatype</groupId> + <artifactId>jackson-datatype-joda</artifactId> + <scope>provided</scope> + </dependency> <dependency> <groupId>org.slf4j</groupId> diff --git a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/utils/DefaultJsonMapper.java b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/utils/DefaultJsonMapper.java index 8303e860..1e67125b 100644 --- a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/utils/DefaultJsonMapper.java +++ b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/utils/DefaultJsonMapper.java @@ -1,6 +1,8 @@ package at.gv.egiz.eaaf.core.impl.utils; import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; import com.fasterxml.jackson.annotation.PropertyAccessor; @@ -9,10 +11,10 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.Module; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.type.TypeFactory; -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import at.gv.egiz.eaaf.core.exceptions.EaafJsonMapperException; import lombok.Getter; @@ -27,6 +29,9 @@ import lombok.extern.slf4j.Slf4j; @Slf4j public final class DefaultJsonMapper { + private static final String JAVA_TIME_MODULE_CLASS = "com.fasterxml.jackson.datatype.jsr310.JavaTimeModule"; + private static final String JODA_TIME_MODULE_CLASS = "com.fasterxml.jackson.datatype.joda.JodaModule"; + @Getter private static final ObjectMapper jsonMapper = new ObjectMapper(); @@ -39,7 +44,9 @@ public final class DefaultJsonMapper { jsonMapper.setVisibility(PropertyAccessor.GETTER, Visibility.PUBLIC_ONLY); jsonMapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.PUBLIC_ONLY); - jsonMapper.registerModule(new JavaTimeModule()); + registerModuleIfAvailable(JAVA_TIME_MODULE_CLASS); + registerModuleIfAvailable(JODA_TIME_MODULE_CLASS); + jsonMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); } @@ -101,6 +108,61 @@ public final class DefaultJsonMapper { throw new EaafJsonMapperException(e.getMessage(), e); } + } + + private static void registerModuleIfAvailable(String fullModuleClasspath) { + Module module = checkAndLoadModule(fullModuleClasspath); + if (module != null) { + jsonMapper.registerModule(module); + log.info("Register Jackson module: {}", module.getModuleName()); + + } else { + log.debug("Skipping Jackson module not on classpath: {}", fullModuleClasspath); + + } + } + + private static Module checkAndLoadModule(String fullModuleClasspath) { + Class<?> clazz = getModuleClass(fullModuleClasspath); + if (clazz != null) { + return getModuleInstance(clazz); + } + + return null; + } + + private static Module getModuleInstance(Class<?> clazz) { + try { + final Constructor<?> constructor = clazz.getConstructor((Class[]) null); + if (constructor != null) { + log.trace("Found method: {} for class:{}. Creating instanze .... ", + constructor.getName(), clazz.getName()); + final Object rawModule = constructor.newInstance(null); + return rawModule instanceof Module ? (Module) rawModule : null; + + } else { + log.warn("Find module:{}, no supported constructor found!", clazz.getName()); + + } + + } catch (NoSuchMethodException | SecurityException | IllegalAccessException + | IllegalArgumentException | InvocationTargetException | InstantiationException e) { + log.debug("Can not create instanze of Jackson module: {}. Reason: {}", clazz.getName(), e.getMessage()); + log.trace("Detailed error", e); + ; + } + return null; } + + private static Class<?> getModuleClass(String fullClassName) { + try { + return Class.forName(fullClassName); + + } catch (final ClassNotFoundException e1) { + log.debug("No {} implemenation in ClassPath. Jackson module will not be available", fullClassName); + return null; + + } + } } |