Jackson工具类 -封装
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
public final class JsonUtil{
private static final ObjectMapper objectMapper=new ObjectMapper();
private static final JsonUtil INSTANCE=new JsonUtil();
private JsonUtil(){}
public static JsonUtil getInstance(){
return INSTANCE;
}
static{
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
objectMapper.setDateFormat(dateFormat);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
objectMapper.disable(SerializationFeature.FLUSH_AFTER_WRITE_VALUE);
}
public static <T> byte[] serialize(T obj){
byte[] bytes;
try{
bytes=objectMapper.writeValueAsBytes(obj);
}catch (JsonProcessingException e){
throw new IllegalStateException(e.getMessage(),e);
}
return bytes;
}
public static <T> T deserialize(byte[] data,Class<T> clazz){
T obj;
try{
obj=objectMapper.readValue(data,clazz);
} catch (IOException e) {
throw new IllegalStateException(e.getMessage(),e);
}
return obj;
}
public static <T> T jsonToObject(String json,Class<?> clazz){
T obj;
JavaType javaType=objectMapper.getTypeFactory().constructType(clazz);
try{
obj=objectMapper.readValue(json,javaType);
} catch (IOException e) {
throw new IllegalStateException(e.getMessage(),e);
}
return obj;
}
public static <T> T jsonToObjectList(String json,Class<?> collectionClass,Class<?>... elementClass){
T obj;
JavaType javaType=objectMapper.getTypeFactory().constructParametricType(collectionClass,elementClass);
try{
obj=objectMapper.readValue(json,javaType);
}catch (IOException e){
throw new IllegalStateException(e.getMessage(),e);
}
return obj;
}
public static <T> T jsonToObjectHashMap(String json,Class<?> keyClass,Class<?> valueClass){
T obj;
JavaType javaType=objectMapper.getTypeFactory().constructParametricType(HashMap.class,keyClass,valueClass);
try {
obj=objectMapper.readValue(json,javaType);
}catch (IOException e){
throw new IllegalStateException(e.getMessage(),e);
}
return obj;
}
public static String objectToJson(Object obj){
String json;
try{
json=objectMapper.writeValueAsString(obj);
}catch (IOException e){
throw new IllegalStateException(e.getMessage(),e);
}
return json;
}
}
关于上面工具类的一些测试
public class test {
@Test
public void test(){
List<Bean> beanList=new ArrayList<>();
beanList.add(new Bean(1));
beanList.add(new Bean(2));
beanList.add(new Bean(3));
beanList.add(new Bean(4));
String json=JsonUtil.objectToJson(beanList);
System.out.println(JsonUtil.jsonToObjectList(json,List.class,Bean.class).getClass());
List<Bean> list=JsonUtil.jsonToObjectList(json,List.class,Bean.class);
list.forEach(v->System.out.println(v.getId()));
}
@Test
public void testJsonToObjectMap(){
Map<String,Object> map=new HashMap<>();
map.put("name","ostreamBaba");
map.put("age",20);
map.put("sex",'f');
String json=JsonUtil.objectToJson(map);
System.out.println(json);
Map<String,Object> t=JsonUtil.jsonToObjectHashMap(json,String.class,Object.class);
t.forEach((k,v)-> System.out.println(k+" "+v));
}
}
public class Bean implements Serializable{
private static final long serialVersionUID = 168070289504099944L;
private int id;
public Bean() {}
public Bean(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}