工程
代码
package com.my.json.helper; import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date; import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
/**
* 将Bean中的Timestamp、Date转换为json中的日期字符串
* @author zhangyi
*
*/
public class DateJsonValueProcessor implements JsonValueProcessor {
public static final String Default_DATE_PATTERN ="yyyy-MM-dd";
private DateFormat dateFormat ;
public DateJsonValueProcessor(String datePattern){
try{
dateFormat = new SimpleDateFormat(datePattern);}
catch(Exception e ){
dateFormat = new SimpleDateFormat(Default_DATE_PATTERN);
}
}
public Object processArrayValue(Object value, JsonConfig jsonConfig) {
return process(value);
}
public Object processObjectValue(String key, Object value,JsonConfig jsonConfig) {
return process(value);
}
private Object process(Object value){
return dateFormat.format((Date)value);
}
}
package com.my.json.helper; import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import net.sf.ezmorph.MorphException;
import net.sf.ezmorph.object.AbstractObjectMorpher; /**
* 将json串中的日期字符串转换为bean中的Timestamp、Date
*
* @author zhangyi
*
*/
public class TimestampMorpher extends AbstractObjectMorpher {
/*** 日期字符串格式 */
private String[] formats; public TimestampMorpher(String[] formats) {
this.formats = formats;
} public Object morph(Object value) {
if (value == null) {
return null;
}
if (Timestamp.class.isAssignableFrom(value.getClass())) {
return (Timestamp) value;
}
if (!supports(value.getClass())) {
throw new MorphException(value.getClass() + " 是不支持的类型");
}
String strValue = (String) value;
SimpleDateFormat dateParser = null;
for (int i = 0; i < formats.length; i++) {
if (null == dateParser) {
dateParser = new SimpleDateFormat(formats[i]);
} else {
dateParser.applyPattern(formats[i]);
}
try {
return new Timestamp(dateParser.parse(strValue.toLowerCase()).getTime());
} catch (ParseException e) {
e.printStackTrace();
}
}
return null;
} @Override
public Class morphsTo() {
return Timestamp.class;
} public boolean supports(Class clazz) {
return String.class.isAssignableFrom(clazz);
} }
package com.my.json.helper; import java.sql.Timestamp; import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.JSONUtils; public class Util { public static Object jsonToBean(String json,Class cls) {
String[] formats = { "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd" };
JSONUtils.getMorpherRegistry().registerMorpher(new TimestampMorpher(formats));
JSONObject jsonObject = JSONObject.fromObject(json);
return JSONObject.toBean(jsonObject, cls);
} public static String beanToJson(Object obj,String dateFormat) {
JsonConfig config = new JsonConfig();
config.registerJsonValueProcessor(Timestamp.class, new DateJsonValueProcessor(dateFormat));
JSONObject json = JSONObject.fromObject(obj, config);
return json.toString();
} }
package com.my.json.helper; import java.sql.Timestamp; public class Student { private int id;
private String name;
private int age;
private Timestamp birthday; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public Timestamp getBirthday() {
return birthday;
} public void setBirthday(Timestamp birthday) {
this.birthday = birthday;
} }
package com.my.json.helper; import java.sql.Timestamp; public class JsonTest {
public static void main(String[] args) { Student s = new Student();
s.setId(123456);
s.setName("李四");
s.setAge(20);
Timestamp b = Timestamp.valueOf("1992-10-19 23:52:18");//设置Timestamp、Date类型的值
s.setBirthday(b); //含有Timestamp、Date的bean转化为Json
String jsonStr = Util.beanToJson(s,"yyyy-MM-dd HH:mm:ss");
System.out.println(jsonStr); //json转为bean(含有Timestamp、Date)
Student s1 = (Student)Util.jsonToBean(jsonStr,Student.class);
System.out.println(s1.getBirthday().toLocaleString()); } }