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(); /** * The default constructor where the default pretty printer is disabled. */ public JsonMapper() { this(false); } /** * The constructor. * @param prettyPrint enables or disables the default pretty printer */ public JsonMapper(@NonNull boolean prettyPrint) { log.trace("Initializing JSON object-mapper ... "); mapper.configure(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY, true); mapper.configure(DeserializationFeature.FAIL_ON_TRAILING_TOKENS, true); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES , true); 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(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 Object deserialize(String value, Class 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 Object deserialize(InputStream is, Class 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); } } }