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


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

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

     1 import java.text.DateFormat;
     2 import java.text.SimpleDateFormat;
     3 import java.util.Date;
     4 import net.sf.json.JsonConfig;
     5 import net.sf.json.processors.JsonValueProcessor;
     6 /*** 将Bean中的Timestamp转换为json中的日期字符串*/
     7 public class DateJsonValueProcessor implements JsonValueProcessor {
     8     public static final String Default_DATE_PATTERN ="yyyy-MM-dd";
     9     private DateFormat dateFormat ;
    10     public DateJsonValueProcessor(String datePattern){
    11         try{
    12             dateFormat  = new SimpleDateFormat(datePattern);}
    13         catch(Exception e ){
    14             dateFormat = new SimpleDateFormat(Default_DATE_PATTERN);
    15         }
    16     }
    17     public Object processArrayValue(Object value, JsonConfig jsonConfig) {
    18         return process(value);
    19     }
    20     public Object processObjectValue(String key, Object value,JsonConfig jsonConfig) {
    21         return process(value);
    22     }
    23     private Object process(Object value){
    24         return dateFormat.format((Date)value);
    25     }
    26 }

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

     1 import java.sql.Timestamp;
     2 import java.text.ParseException;
     3 import java.text.SimpleDateFormat;
     4 import net.sf.ezmorph.MorphException;
     5 import net.sf.ezmorph.object.AbstractObjectMorpher;
     6 /*** 将json串中的日期字符串转换为bean中的Timestamp*/
     7 public class TimestampMorpher extends AbstractObjectMorpher {
     8 /*** 日期字符串格式*/
     9 private String[] formats;
    10 public TimestampMorpher(String[] formats) {
    11     this.formats = formats;
    12 }
    13 public Object morph(Object value) {
    14 if( value == null){
    15     return null;
    16 }
    17 if( Timestamp.class.isAssignableFrom(value.getClass()) ){
    18     return (Timestamp) value;
    19 }
    20 if( !supports( value.getClass()) ){
    21     throw new MorphException( value.getClass() + " 是不支持的类型");
    22 }
    23 String strValue=(String) value;
    24 SimpleDateFormat dateParser=null;
    25 for( int i = 0; i < formats.length ; i++ ){
    26     if( null == dateParser ){
    27         dateParser=new SimpleDateFormat(formats[i]);
    28     }else{
    29         dateParser.applyPattern(formats[i]);
    30     }
    31     try{
    32         return new Timestamp( dateParser.parse( strValue.toLowerCase()).getTime() );}
    33     catch (ParseException e) {
    34         //e.printStackTrace();
    35     }
    36 }
    37 return null;
    38 }
    39 @Override
    40 public Class morphsTo() {
    41     return Timestamp.class;
    42 }
    43 public boolean supports( Class clazz ){
    44     return String.class.isAssignableFrom( clazz );
    45 }
    46 
    47 }

    测试一下。

     1 public class JsonTest {
     2 public static void main(String[] args) {
     3     String jsonStr="{"id":"101","name":"张三","age":"20","birthday":"1992-10-19 23:52:18"}";
     4     Student s=new Student();
     5     Timestamp b=Timestamp.valueOf("1992-10-19 23:52:18");
     6     s.setId(123456);
     7     s.setName("李四");
     8     s.setAge(20);
     9     s.setBirthday(b);
    10     Student s1=jsonToBean(jsonStr);
    11     System.out.println(s1.getBirthday());
    12     System.out.println(beanToJson(s));
    13 }
    14 public static Student jsonToBean(String json){
    15     String[] formats={"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd"};
    16     JSONUtils.getMorpherRegistry().registerMorpher(new TimestampMorpher(formats));
    17     JSONObject jsonObject=JSONObject.fromObject(json);
    18     return (Student)JSONObject.toBean(jsonObject,Student.class);
    19 }
    20 
    21 public static String beanToJson(Student s){
    22     JsonConfig config=new JsonConfig();
    23     config.registerJsonValueProcessor(Timestamp.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
    24     JSONObject json=JSONObject.fromObject(s,config);
    25     return json.toString();}
    26 }
  • 相关阅读:
    编程总结2
    编程总结3
    《秋季学期学习总结》
    《人生路上对我影响最大的三位老师》
    第八周编程总结
    第七周编程总结
    第六周编程总结
    第五周作业
    第四周编程总结
    第三周编程总结
  • 原文地址:https://www.cnblogs.com/ryanchancrj/p/4291280.html
Copyright © 2020-2023  润新知