Maven依赖:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-avro</artifactId>
<version>2.9.0</version>
</dependency>
<!-- 如果使用@Slf4j 注解,需要引入lombok依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
工具栏实现:
@Slf4j
public class JsonUtil {
public static ObjectMapper objectMapper = new ObjectMapper();
static {
//***********************序列化****************************
//对象的所有字段全部列入
objectMapper.setSerializationInclusion(Inclusion.ALWAYS);
//取消默认转换timestamps
objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
//忽略空Bean转json的错误
objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
//同意日期格式为:"yyyy-MM-dd HH:mm:ss"
objectMapper.setDateFormat(new SimpleDateFormat(DateTimeUtil.STANDARD_FORMAT));
//***********************序列化****************************
//***********************反序列化****************************
//忽略 在json字符串中存在,但是java对象中不存在对应属性的情况,防止错误。
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//***********************反序列化****************************
}
/**
* Object转json字符串
*
* @param obj
* @param <T>
* @return
*/
public static <T> String obj2String(T obj) {
if (obj == null) {
return null;
}
try {
return obj instanceof String ? (String) obj : objectMapper.writeValueAsString(obj);
} catch (Exception e) {
log.warn("Parse object to String error", e);
return null;
}
}
/**
* Object转json字符串 将转换的json结果格式化
*
* @param obj
* @param <T>
* @return
*/
public static <T> String obj2StringPretty(T obj) {
if (obj == null) {
return null;
}
try {
return obj instanceof String ? (String) obj : objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
} catch (Exception e) {
log.warn("Parse object to String error", e);
return null;
}
}
/**
* String转Object
*
* @param str
* @param clazz Object类型
* @param <T>
* @return
*/
public static <T> T string2Obj(String str, Class<T> clazz) {
if (StringUtil.isEmpty(str) || clazz == null) {
return null;
}
try {
return clazz.equals(String.class) ? (T) str : objectMapper.readValue(str, clazz);
} catch (Exception e) {
log.warn("Parse String to object error", e);
return null;
}
}
/**
* String转Object 适用于多泛型 eg: 将String转换成 Map<UserClass,CourseClass>
*
* @param str
* @param typeReference Map<Class1,Class2>
* @param <T>
* @return TypeReference 导入包:import org.codehaus.jackson.type.TypeReference;
*/
public static <T> T string2Obj(String str, TypeReference<T> typeReference) {
if (StringUtil.isEmpty(str) || typeReference == null) {
return null;
}
try {
return (T) (typeReference.getType().equals(String.class) ? (T) str : objectMapper.readValue(str, typeReference));
} catch (Exception e) {
log.warn("Parse String to object error", e);
return null;
}
}
/**
* String转Object 使用存在Collection集合 eg:将String转换成List<UserClass>
*
* @param str
* @param collectionClass 集合类型
* @param elementClasses 集合中对象类型,多个对象类型时传入数组
* @param <T>
* @return
*/
public static <T> T string2Obj(String str, Class<?> collectionClass, Class<?>... elementClasses) {
JavaType javaType = objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
try {
return objectMapper.readValue(str, javaType);
} catch (Exception e) {
log.warn("Parse String to object error", e);
return null;
}
}
}