Json与Bean互转,Timestamp类型的问题 (转)


Json与Java Bean互相转换时,Bean中的Timestamp字段是无法直接处理的,需要实现两个转换器。

DateJsonValueProcessor的作用是Bean转换为Json时将Timepstamp转换为指定的时间格式。

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转换为json中的日期字符串*/
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);
    }
}


TimestampMorpher的作用则与DateJsonValueProcessor相反,它是在Jsonl转换为Bean时,会把指定的时间格式转换为Timestamp


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*/
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 );
}

}


public class JsonTest {
public static void main(String[] args) {
String jsonStr="{\"id\":\"101\",\"name\":\"张三\",\"age\":\"20\",\"birthday\":\"1992-10-19 23:52:18\"}";
Student s=new Student();
Timestamp b=Timestamp.valueOf("1992-10-19 23:52:18");
s.setId(123456);
s.setName("李四");
s.setAge(20);
s.setBirthday(b);
Student s1=jsonToBean(jsonStr);
System.out.println(s1.getBirthday());
System.out.println(beanToJson(s));
}
public static Student jsonToBean(String json){
String[] formats={"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd"};
JSONUtils.getMorpherRegistry().registerMorpher(new TimestampMorpher(formats));
JSONObject jsonObject=JSONObject.fromObject(json);
return (Student)JSONObject.toBean(jsonObject,Student.class);
}

public static String beanToJson(Student s){
JsonConfig config=new JsonConfig();
config.registerJsonValueProcessor(Timestamp.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
JSONObject json=JSONObject.fromObject(s,config);
return json.toString();}
}

猜你喜欢

转载自frank1998819.iteye.com/blog/2255728