注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

骇客归来

ぁ枫あ

 
 
 

日志

 
 

JabSorb日期序列化的自定义  

2009-02-17 15:41:15|  分类: Java |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

第一步:

创建一个自定义类,继承自DateSerializer,重写他的两个方法marshall,unmarshall

package org.jabsorb.serializer.impl;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.jabsorb.serializer.MarshallException;
import org.jabsorb.serializer.SerializerState;
import org.jabsorb.serializer.UnmarshallException;
import org.json.JSONException;
import org.json.JSONObject;

public class MyDateSerializer extends DateSerializer
{
    private static final long serialVersionUID = -2218392896139645723L;
    private String formatStr = "yyyy-MM-dd";

    /**
     * @return the formatStr
     */
    public String getFormatStr()
    {
        return formatStr;
    }

    /**
     * @param formatStr
     *            the formatStr to set
     */
    public void setFormatStr(String formatStr)
    {
        this.formatStr = formatStr;
    }

    private SimpleDateFormat formater;

    @Override
    public Object marshall(SerializerState state, Object p, Object o) throws MarshallException
    {
        formater = new SimpleDateFormat(formatStr);
        // long time;
        String dateStr;

        if (o instanceof Date)
        {
            dateStr = formater.format(o);
            // time = ((Date) o).getTime();
        } else
        {
            throw new MarshallException("cannot marshall date using class " + o.getClass());
        }
        JSONObject obj = new JSONObject();
        try
        {
            if (ser.getMarshallClassHints())
            {
                obj.put("javaClass", o.getClass().getName());
            }
            // obj.put("time", time);
            obj.put("time", dateStr);

        } catch (JSONException e)
        {
            throw new MarshallException(e.getMessage(), e);
        }
        return obj;
    }

    public Object unmarshall(SerializerState state, Class clazz, Object o) throws UnmarshallException
    {
        JSONObject jso = (JSONObject) o;
        String timeStr;
        try
        {
            timeStr = jso.getString("time");
        } catch (JSONException e)
        {
            throw new UnmarshallException("Could not get the time in date serialiser", e);
        }
        if (jso.has("javaClass"))
        {
            try
            {
                clazz = Class.forName(jso.getString("javaClass"));
            } catch (ClassNotFoundException e)
            {
                throw new UnmarshallException(e.getMessage(), e);
            } catch (JSONException e)
            {
                throw new UnmarshallException("Could not find javaClass", e);
            }
        }
        Object returnValue = null;
        Date dt;
        try
        {
            dt = formater.parse(timeStr);
        } catch (ParseException e1)
        {
            dt = new Date();
        }
        if (Date.class.equals(clazz))
        {
            returnValue = dt;
        } else if (Timestamp.class.equals(clazz))
        {
            returnValue = new Timestamp(dt.getTime());
        } else if (java.sql.Date.class.equals(clazz))
        {
            returnValue = new java.sql.Date(dt.getTime());
        }
        if (returnValue == null)
        {
            throw new UnmarshallException("invalid class " + clazz);
        }
        state.setSerialized(o, returnValue);
        return returnValue;
    }

}

 

 

第二步:注册

JSONSerializer ser = new JSONSerializer();
        ser.registerSerializer(new MyDateSerializer());
        ser.registerSerializer(new RawJSONArraySerializer());
        ser.registerSerializer(new RawJSONObjectSerializer());
        ser.registerSerializer(new BeanSerializer());
        ser.registerSerializer(new ArraySerializer());
        ser.registerSerializer(new DictionarySerializer());
        ser.registerSerializer(new MapSerializer());
        ser.registerSerializer(new SetSerializer());
        ser.registerSerializer(new ListSerializer());
        ser.registerSerializer(new StringSerializer());
        ser.registerSerializer(new NumberSerializer());
        ser.registerSerializer(new BooleanSerializer());
        ser.registerSerializer(new PrimitiveSerializer());

  评论这张
 
阅读(242)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018