All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.futuresight.util.mystique.lever.JacksonConvertor Maven / Gradle / Ivy
/*
* Copyright (c) Balajee TM 2016.
* All rights reserved.
* License - @see
*/
/*
* Created on 25 Aug, 2016 by balajeetm
*/
package com.futuresight.util.mystique.lever;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParser;
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;
/**
* The Class JacksonConvertor.
*
* @author balajeetm
*/
@Component
public class JacksonConvertor implements JsonConvertor {
/** The object mapper. */
private ObjectMapper objectMapper;
/**
* Inits the.
*/
@PostConstruct
protected void init() {
Creator.INSTANCE = this;
}
/**
* Gets the single instance of JsonJacksonConvertor.
*
* @return single instance of JsonJacksonConvertor
*/
public static JsonConvertor getInstance() {
return Creator.INSTANCE;
}
// Efficient Thread safe Lazy Initialization
// works only if the singleton constructor is non parameterized
/**
* The Class Creator.
*/
private static class Creator {
/** The instance. */
public static JsonConvertor INSTANCE = new JacksonConvertor();
}
/**
* Instantiates a new json jackson convertor.
*/
private JacksonConvertor() {
getObjectMapper();
}
/*
* (non-Javadoc)
* @see
* com.cisco.cab.utils.convertor.ConvertorInterface#deserialize(java.lang
* .String, java.lang.Class)
*/
/**
* Deserialize.
*
* @param the generic type
* @param objectString the object string
* @param objectType the object type
* @return the t
* @throws ConvertorException the convertor exception
*/
public T deserialize(String objectString, Class objectType) throws ConvertorException {
T value = null;
try {
value = null != objectString ? getObjectMapper().readValue(objectString, objectType) : value;
}
catch (Exception e) {
throw getConvertorException(e);
}
return value;
}
/*
* (non-Javadoc)
* @see
* com.futuresight.boink.common.objectconvertor.ConvertorInterface#deserialize
* (java.lang.Object, java.lang.Class)
*/
public T deserialize(Object object, Class objectType) throws ConvertorException {
T value = null;
try {
value = null != object ? getObjectMapper().convertValue(object, objectType) : value;
}
catch (Exception e) {
throw getConvertorException(e);
}
return value;
}
/*
* (non-Javadoc)
* @see
* com.futuresight.boink.common.objectconvertor.ConvertorInterface#deserialize
* (java.io.InputStream, java.lang.Class)
*/
public T deserialize(InputStream inputStream, Class objectType) throws ConvertorException {
T value = null;
try {
value = null != inputStream ? getObjectMapper().readValue(inputStream, objectType) : value;
}
catch (Exception e) {
throw getConvertorException(e);
}
return value;
}
/**
* Gets the convertor exception.
*
* @param e the e
* @return the convertor exception
*/
private ConvertorException getConvertorException(Exception e) {
return new ConvertorException(e);
}
/**
* Gets the object mapper.
*
* @return the object mapper
*/
public ObjectMapper getObjectMapper() {
if (null == objectMapper) {
objectMapper = new ObjectMapper();
/*
* The configurations for a object mapper. Eg. Serialization,
* Deserialization, etc. There are enable, disable functions for the
* same. We will use an explicit configure so that we can enable and
* disable this based on configuration
*/
// to prevent exception when encountering unknown property:
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// to allow serialization of "empty" POJOs (no properties to
// serialize)
// (without this setting, an exception is thrown in those cases)
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
// to write java.util.Date, Calendar as number (timestamp):
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.setSerializationInclusion(Include.NON_NULL);
// objectMapper.setSerializationInclusion(Include.NON_EMPTY);
// JsonParser.Feature for configuring parsing settings:
// to allow use of apostrophes (single quotes), non standard
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
// Add Mix Ins if needed
// eg. objectMapper.addMixInAnnotations(MagicProductUmbrella.class,
// Test.class);
// set various strategies
// eg.
// objectMapper.enableDefaultTypingAsProperty(DefaultTyping.OBJECT_AND_NON_CONCRETE,
// "remoteClass");
}
return objectMapper;
}
/*
* (non-Javadoc)
* @see
* com.cisco.cab.utils.convertor.ConvertorInterface#serialize(java.lang.
* Object)
*/
/**
* Serialize.
*
* @param value the value
* @return the string
* @throws ConvertorException the convertor exception
*/
public String serialize(Object value) throws ConvertorException {
try {
return getObjectMapper().writeValueAsString(value);
}
catch (Exception e) {
throw getConvertorException(e);
}
}
/*
* (non-Javadoc)
* @see com.futuresight.boink.common.objectconvertor.ConvertorInterface#
* deserializeGroup(java.io.InputStream, java.lang.Class, java.lang.Class)
*/
@SuppressWarnings("unchecked")
public T deserializeGroup(InputStream inputStream, Class groupClass, Class> elementClass)
throws ConvertorException {
JavaType javaType = getJavaType(groupClass, elementClass);
try {
return (T) getObjectMapper().readValue(inputStream, javaType);
}
catch (Exception e) {
throw getConvertorException(e);
}
}
/*
* (non-Javadoc)
* @see com.futuresight.boink.common.objectconvertor.ConvertorInterface#
* deserializeList(java.io.InputStream, java.lang.Class)
*/
@SuppressWarnings("unchecked")
public List deserializeList(InputStream inputStream, Class elementClass) throws ConvertorException {
JavaType javaType = getJavaType(List.class, elementClass);
try {
return (List) getObjectMapper().readValue(inputStream, javaType);
}
catch (Exception e) {
throw getConvertorException(e);
}
}
/*
* (non-Javadoc)
* @see com.futuresight.boink.common.objectconvertor.ConvertorInterface#
* deserializeList(java.lang.Object, java.lang.Class)
*/
@SuppressWarnings("unchecked")
public List deserializeList(Object object, Class elementClass) throws ConvertorException {
JavaType javaType = getJavaType(List.class, elementClass);
try {
return (List) getObjectMapper().convertValue(object, javaType);
}
catch (Exception e) {
throw getConvertorException(e);
}
}
/*
* (non-Javadoc)
* @see
* com.cisco.cab.utils.convertor.ConvertorInterface#groupSerialize(java.
* lang.String, java.lang.Class, java.lang.Class)
*/
/**
* Deserialize group.
*
* @param the generic type
* @param the generic type
* @param objectString the object string
* @param groupClass the group class
* @param elementClass the element class
* @return the t
* @throws ConvertorException the convertor exception
*/
public T deserializeGroup(String objectString, Class groupClass, Class elementClass)
throws ConvertorException {
return deserializeGroupOnJavaType(objectString, groupClass, elementClass);
}
/**
* Deserialize group on java type.
*
* @param the generic type
* @param the generic type
* @param objectString the object string
* @param groupClass the group class
* @param elementClass the element class
* @return the t
*/
private T deserializeGroupOnJavaType(String objectString, Class groupClass, Class elementClass) {
JavaType javaType = getJavaType(groupClass, elementClass);
try {
return getObjectMapper().readValue(objectString, javaType);
}
catch (Exception e) {
throw getConvertorException(e);
}
}
/**
* Gets the java type.
*
* @param the generic type
* @param the generic type
* @param groupClass the group class
* @param elementClass the element class
* @return the java type
*/
private JavaType getJavaType(Class groupClass, Class elementClass) {
JavaType javaType = null;
if (List.class.isAssignableFrom(groupClass)) {
javaType = TypeFactory.defaultInstance().constructCollectionType(List.class, elementClass);
}
else if (Collection.class.isAssignableFrom(groupClass)) {
javaType = TypeFactory.defaultInstance().constructCollectionType(Collection.class, elementClass);
}
return javaType;
}
/* (non-Javadoc)
* @see com.futuresight.utl.mystique.ConvertorInterface#deserializeList(java.lang.String, java.lang.Class)
*/
@SuppressWarnings("unchecked")
public List deserializeList(String jsonString, Class elementClass) throws ConvertorException {
JavaType javaType = getJavaType(List.class, elementClass);
try {
return (List) getObjectMapper().readValue(jsonString, javaType);
}
catch (Exception e) {
throw getConvertorException(e);
}
}
}