zoukankan      html  css  js  c++  java
  • fastjson对Date的处理

    对日期的序列化:

    一种方法是通过注解

    Java代码

    1
    2
    @JSONField (format="yyyy-MM-dd HH:mm:ss")  
    public Date birthday;

    另一种是通过SerializeConfig:

    Java代码  收藏代码

    1
    2
    3
    4
    5
    6
    private static SerializeConfig mapping = new SerializeConfig();  
    private static String dateFormat;  
    static {  
        dateFormat = "yyyy-MM-dd HH:mm:ss";  
        mapping.put(Date.classnew SimpleDateFormatSerializer(dateFormat));  
    }

    json字符串中使用单引号:

    String text = JSON.toJSONString(object, SerializerFeature.UseSingleQuotes);

    字段显示不同的key:

    1
    2
    3
    4
    5
    6
    7
    public class User {  
        @JSONField(name="ID")  
        public int getId() { ... }  
    }  
        
    User user = ...;  
    JSON.toJSONString(user); // {"ID":001}

    自定义序列化代码示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    public class JsonUtil {  
        private static SerializeConfig mapping = new SerializeConfig();  
        private static String dateFormat;  
        static {  
            dateFormat = "yyyy-MM-dd HH:mm:ss";  
        }  
       
        /** 
         * 默认的处理时间 
         *  
         * @param jsonText 
         * @return 
         */  
        public static String toJSON(Object jsonText) {  
            return JSON.toJSONString(jsonText,  
                    SerializerFeature.WriteDateUseDateFormat);  
        }  
       
        /** 
         * 自定义时间格式 
         *  
         * @param jsonText 
         * @return 
         */  
        public static String toJSON(String dateFormat, String jsonText) {  
            mapping.put(Date.classnew SimpleDateFormatSerializer(dateFormat));  
            return JSON.toJSONString(jsonText, mapping);  
        }  
    }

    自定义反序列化示例:

    先自定义一个日期解析类:

    Java代码  收藏代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    public class MyDateFormatDeserializer extends DateFormatDeserializer {  
       
            private String myFormat;  
       
            public MyDateFormatDeserializer(String myFormat) {  
                super();  
                this.myFormat = myFormat;  
            }  
       
            @Override  
            protected <Date> Date cast(DefaultJSONParser parser, Type clazz, Object fieldName, Object val) {  
                if (myFormat == null) {  
                    return null;  
                }  
                if (val instanceof String) {  
                    String strVal = (String) val;  
                    if (strVal.length() == 0) {  
                        return null;  
                    }  
       
                    try {  
                        return (Date) new SimpleDateFormat(myFormat).parse((String)val);  
                    catch (ParseException e) {  
                        throw new JSONException("parse error");  
                    }  
                }  
                throw new JSONException("parse error");  
            }  
        }

     

  • 相关阅读:
    [bzoj4417] [洛谷P3990] [Shoi2013] 超级跳马
    [bzoj4011] [洛谷P3244] [HNOI2015] 落忆枫音
    [bzoj1875] [洛谷P2151] [SDOI2009] HH去散步
    [bzoj4827] [洛谷P3723] [Hnoi2017] 礼物
    [bzoj2326] [洛谷P3216] [HNOI2011] 数学作业
    [bzoj3105] [cqoi2013] 新Nim游戏
    [YTU]_2353 ( 长方柱类【C++ 类定义】)
    [YTU]_2627 (职工工资统计)
    [YTU]_2769( 结构体--成绩统计)
    [YTU]_2577( 小数计算——结构体)
  • 原文地址:https://www.cnblogs.com/exmyth/p/5189753.html
Copyright © 2011-2022 走看看