jackson的一些总结
Jackson 的所有类都可以认为是围绕着 ObjectMapper 对象打造的, 所以, 熟悉了 ObjectMapper 也就掌握了 Jackson
一个简单的配置如下:
static ObjectMapper objectMapper = null;
static {
objectMapper = new ObjectMapper();
// 未知字段报警,
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 不序列化空字段
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// 设置时间格式
SimpleDateFormat myDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
objectMapper.setDateFormat(myDateFormat);
}
大部分配置都可以通过 configure 进行配置,
DeserializationFeature 包含了所有可以配置的枚举
常用注解就是 序列化和反序列化时指定名称
具体看这里
Jackson常用注解
序列化简单:
public static String toJson(Object obj) { try { return objectMapper.writeValueAsString(obj); } catch (JsonProcessingException e) { logger.error("对象转换为字符串时出错", e); } return obj.toString(); }
反序列化:
public static
都需要使用到下面2个例子,用来返回需要实例化的对象类型
public static JavaType getComplexType(Class> collectionClass, Class>... elementClasses) { return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses); } public static JavaType getComplexType(Class> collectionClass, JavaType... elementClasses) { return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses); }
String str = "[1,2,3,4,5]"; // 把他序列化为 List
调用方法
public static
List
String str = "[{\"created\":\"2015-05-26\"}]"; // 把他转换为 List
因为泛型类中嵌套了泛型类, 所以需要从内到外一个个解泛型
调用方法
public static
JavaType type = JsonUtils.getComplexType(HashMap.class, String.class, Object.class); List
即可完成复杂类型解套.
最后是封装的一个工具类
package crm.app.trade.utils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import crm.data.buyer.model.BuyerDo;
import org.apache.commons.beanutils.PropertyUtilsBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* json转换类
* @author xionger
*/
public class JsonUtils {
static Logger logger = LoggerFactory.getLogger(JsonUtils.class);
static ObjectMapper objectMapper = null;
static {
objectMapper = new ObjectMapper();
// 未知字段报警,
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 不序列化空字段
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// 设置时间格式
SimpleDateFormat myDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
objectMapper.setDateFormat(myDateFormat);
}
public static
try {
return objectMapper.readValue(json, clazz);
} catch (IOException e) {
logger.error("Json转换成对象时出错", e);
}
return null;
}
public static
JavaType javaType = getComplexType(fatherClass, otherClass);
try {
return objectMapper.readValue(json, javaType);
} catch (IOException e) {
logger.error("Json转换成对象时出错", e);
}
return null;
}
/**
*
* @param collectionClass 父类型
* @param elementClasses 子类型
* @return
*/
public static JavaType getComplexType(Class> collectionClass, Class>... elementClasses) {
return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
}
public static JavaType getComplexType(Class> collectionClass, JavaType... elementClasses) {
return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
}
public static
JavaType javaType = getComplexType(ArrayList.class, clazz);
try {
return objectMapper.readValue(json, javaType);
} catch (IOException e) {
logger.error("Json转换成对象时出错", e);
}
return null;
}
public static
JavaType javaType = getComplexType(HashMap.class, keyClass, valueClass);
try {
return objectMapper.readValue(json, javaType);
} catch (IOException e) {
logger.error("Json转换成对象时出错", e);
}
return null;
}
public static String toJson(Object obj) {
try {
return objectMapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
logger.error("对象转换为字符串时出错", e);
}
return obj.toString();
}
public static
return from(toJson(trade), clazz);
}
public static
Map
try {
PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(obj);
for (int i = 0; i < descriptors.length; i++) {
String name = descriptors[i].getName();
if (!"class".equals(name)) {
params.put(name, propertyUtilsBean.getNestedProperty(obj, name));
}
}
} catch (Exception e) {
logger.error("实体类转Map失败" + e);
}
return params;
}
public static
JavaType javaType = getComplexType(fatherType, otherType);
try {
return objectMapper.readValue(json, javaType);
} catch (IOException e) {
logger.error("Json转换成对象时出错", e);
}
return null;
}
}