package at.gv.egiz.eaaf.modules.auth.sl20.utils; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import java.io.IOException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.lang.NonNull; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.type.TypeFactory; public class JsonMapper { private static final Logger log = LoggerFactory.getLogger(JsonMapper.class); private 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"); } /** * Get the internal mapper implemenation * * @return */ public ObjectMapper getMapper() { return mapper; } /** * Serialize an object to a JSON string. * @param value the object to serialize * @return a JSON string * @throws JsonProcessingException thrown when an error occurs during serialization */ public String serialize(Object value) throws JsonProcessingException { return mapper.writeValueAsString(value); } /** * Deserialize a JSON string. * * @param value the JSON string to deserialize * @param clazz optional parameter that determines the type of the returned object. If not set, an {@link Object} is returned. * @return the deserialized JSON string as an object of type {@code clazz} or {@link Object} * @throws JsonParseException if the JSON string contains invalid content. * @throws JsonMappingException if the input JSON structure does not match structure expected for result type * @throws IOException if an I/O problem occurs (e.g. unexpected end-of-input) */ public Object deserialize(String value, Class clazz) throws JsonParseException, JsonMappingException, IOException{ ObjectMapper mapper = new ObjectMapper(); if (clazz != null) { JavaType javaType = TypeFactory.defaultInstance().constructType(clazz); return mapper.readValue(value, javaType); } else return mapper.readValue(value, Object.class); } public Object deserialize(String value, TypeReference clazz) throws JsonParseException, JsonMappingException, IOException { return mapper.readValue(value, clazz); } }