diff options
Diffstat (limited to 'eaaf_modules/eaaf_module_auth_sl20/src/main/java/at/gv/egiz/eaaf/modules/auth/sl20/utils/JsonMapper.java')
-rw-r--r-- | eaaf_modules/eaaf_module_auth_sl20/src/main/java/at/gv/egiz/eaaf/modules/auth/sl20/utils/JsonMapper.java | 129 |
1 files changed, 9 insertions, 120 deletions
diff --git a/eaaf_modules/eaaf_module_auth_sl20/src/main/java/at/gv/egiz/eaaf/modules/auth/sl20/utils/JsonMapper.java b/eaaf_modules/eaaf_module_auth_sl20/src/main/java/at/gv/egiz/eaaf/modules/auth/sl20/utils/JsonMapper.java index 2387a9f2..6868ac97 100644 --- a/eaaf_modules/eaaf_module_auth_sl20/src/main/java/at/gv/egiz/eaaf/modules/auth/sl20/utils/JsonMapper.java +++ b/eaaf_modules/eaaf_module_auth_sl20/src/main/java/at/gv/egiz/eaaf/modules/auth/sl20/utils/JsonMapper.java @@ -1,44 +1,20 @@ package at.gv.egiz.eaaf.modules.auth.sl20.utils; -import java.io.IOException; -import java.io.InputStream; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.lang.NonNull; - import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; import com.fasterxml.jackson.annotation.PropertyAccessor; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; -import com.fasterxml.jackson.databind.type.TypeFactory; - -import at.gv.egiz.eaaf.core.api.utils.IJsonMapper; -import at.gv.egiz.eaaf.core.exceptions.EaafJsonMapperException; - -public class JsonMapper implements IJsonMapper { - private static final Logger log = LoggerFactory.getLogger(JsonMapper.class); - private final ObjectMapper mapper = new ObjectMapper(); +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; - /** - * The default constructor where the default pretty printer is disabled. - */ - public JsonMapper() { - this(false); +@Slf4j +public class JsonMapper { + + @Getter + private static final ObjectMapper mapper = new ObjectMapper(); - } - - /** - * The constructor. - * - * @param prettyPrint enables or disables the default pretty printer - */ - public JsonMapper(@NonNull final boolean prettyPrint) { + static { log.trace("Initializing JSON object-mapper ... "); mapper.configure(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY, true); mapper.configure(DeserializationFeature.FAIL_ON_TRAILING_TOKENS, true); @@ -46,94 +22,7 @@ public class JsonMapper implements IJsonMapper { mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE); mapper.setVisibility(PropertyAccessor.GETTER, Visibility.PUBLIC_ONLY); mapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.PUBLIC_ONLY); - if (prettyPrint) { - mapper.enable(SerializationFeature.INDENT_OUTPUT); - } - - log.debug("JSON object-mapper initialized"); - - } - - /* - * (non-Javadoc) - * - * @at.gv.egiz.eaaf.core.api.utils.IJsonMapper#getMapper() - */ - public ObjectMapper getMapper() { - return mapper; - - } - - /* - * (non-Javadoc) - * - * @see at.gv.egiz.eaaf.core.api.utils.IJsonMapper#serialize(java.lang.Object) - */ - @Override - public String serialize(final Object value) throws EaafJsonMapperException { - try { - return mapper.writeValueAsString(value); - - } catch (final JsonProcessingException e) { - log.warn("JSON mapping FAILED with error: {}", e.getMessage()); - throw new EaafJsonMapperException(e.getMessage(), e); - - } - - } - - /* - * (non-Javadoc) - * - * @see at.gv.egiz.eaaf.core.api.utils.IJsonMapper#deserialize(java.lang.String, - * java.lang.Class) - */ - @Override - public <T> Object deserialize(final String value, final Class<T> clazz) throws EaafJsonMapperException { - try { - if (clazz != null) { - if (clazz.isAssignableFrom(TypeReference.class)) { - return mapper.readValue(value, clazz); - } else { - final JavaType javaType = TypeFactory.defaultInstance().constructType(clazz); - return mapper.readValue(value, javaType); - - } - - } else { - return mapper.readValue(value, Object.class); - } - - } catch (final IOException e) { - log.warn("JSON mapping FAILED with error: {}", e.getMessage()); - throw new EaafJsonMapperException(e.getMessage(), e); - - } - - } - - @Override - public <T> Object deserialize(final InputStream is, final Class<T> clazz) throws EaafJsonMapperException { - try { - if (clazz != null) { - if (clazz.isAssignableFrom(TypeReference.class)) { - return mapper.readValue(is, clazz); - } else { - final JavaType javaType = TypeFactory.defaultInstance().constructType(clazz); - return mapper.readValue(is, javaType); - - } - - } else { - return mapper.readValue(is, Object.class); - } - - } catch (final IOException e) { - log.warn("JSON mapping FAILED with error: {}", e.getMessage()); - throw new EaafJsonMapperException(e.getMessage(), e); - - } - + } } |