TestJsonBinder测试package mg.utils;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
/**
* 依赖jackson-mapper-lgpl (82)
jackson-core-lgpl (82) 包
* @author meigang 2015-03-31
*
*/
public class JsonBinder {
private static Logger log = Logger.getLogger(JsonBinder.class);
private ObjectMapper mapper;
public JsonBinder(Inclusion inclusion) {
mapper = new ObjectMapper();
//设置输出包含的属性
mapper.getSerializationConfig().setSerializationInclusion(inclusion);
//设置输入时忽略JSON字符串中存在而Java对象实际没有的属性
mapper.getDeserializationConfig().set(
org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
/**
* 创建输出全部属性到Json字符串的Binder.
*/
public static JsonBinder buildNormalBinder() {
return new JsonBinder(Inclusion.ALWAYS);
}
/**
* 创建只输出非空属性到Json字符串的Binder.
*/
public static JsonBinder buildNonNullBinder() {
return new JsonBinder(Inclusion.NON_NULL);
}
/**
* 创建只输出初始值被改变的属性到Json字符串的Binder.
*/
public static JsonBinder buildNonDefaultBinder() {
return new JsonBinder(Inclusion.NON_DEFAULT);
}
/**
* 如果JSON字符串为Null或"null"字符串,返回Null.
* 如果JSON字符串为"[]",返回空集合.
*
* 如需读取集合如List/Map,且不是List<String>这种简单类型时使用如下语句:
* List<MyBean> beanList = binder.getMapper().readValue(listString, new TypeReference<List<MyBean>>() {});
*/
public <T> T fromJson(String jsonString, Class<T> clazz) {
if (StringUtils.isEmpty(jsonString)) {
return null;
}
try {
return mapper.readValue(jsonString, clazz);
} catch (IOException e) {
log.warn("parse json string error:" + jsonString, e);
return null;
}
}
/**
* 如果对象为Null,返回"null".
* 如果集合为空集合,返回"[]".
*/
public String toJson(Object object) {
try {
return mapper.writeValueAsString(object);
} catch (IOException e) {
log.warn("write to json string error:" + object, e);
return null;
}
}
/**
* 设置转换日期类型的format pattern,如果不设置默认打印Timestamp毫秒数.
*/
public void setDateFormat(String pattern) {
if (StringUtils.isNotBlank(pattern)) {
DateFormat df = new SimpleDateFormat(pattern);
mapper.getSerializationConfig().setDateFormat(df);
mapper.getDeserializationConfig().setDateFormat(df);
}
}
/**
* 取出Mapper做进一步的设置或使用其他序列化API.
*/
public ObjectMapper getMapper() {
return mapper;
}
}
输出结果:package mg.utils;
import java.util.Date;
import org.apache.log4j.Logger;
import org.junit.Test;
public class TestJsonBinder {
Logger log = Logger.getLogger(TestJsonBinder.class);
@Test
public void test1(){
Person p = new Person("小刚", "男", 22, new Date());
String pJson = JsonBinder.buildNormalBinder().toJson(p);
log.info(pJson);
}
}
class Person{
private String name;
private String sex;
private int age;
private Date borthday;
public Person(String name, String sex, int age, Date borthday) {
this.name = name;
this.sex = sex;
this.age = age;
this.borthday = borthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBorthday() {
return borthday;
}
public void setBorthday(Date borthday) {
this.borthday = borthday;
}
}
[2015-03-31 19:47:16 INFO ] [main] {mg.utils.TestJsonBinder:14}-{"name":"小刚","sex":"男","age":22,"borthday":1427802435833}