2011-09-15 79 views
3

我使用xstrem將jodatime本地日期序列化爲xml。 但是,當輸出生成的xml時,LocalDate的格式不易讀。 見下圖:Xstream的jodatime本地日期顯示

<date> 
    <iLocalMillis>1316563200000</iLocalMillis> 
    <iChronology class="org.joda.time.chrono.ISOChronology" reference="../../tradeDate/iChronology"/> 

任何想法如何,我可以得到XStream把顯示的格式不開車送我去了牆的日期?

回答

4

這裏是我已經成功地使用。我相信我在第一篇文章中提到的鏈接中使用了信息。

import java.lang.reflect.Constructor; 

import org.joda.time.DateTime; 

import com.thoughtworks.xstream.converters.Converter; 
import com.thoughtworks.xstream.converters.MarshallingContext; 
import com.thoughtworks.xstream.converters.UnmarshallingContext; 
import com.thoughtworks.xstream.io.HierarchicalStreamReader; 
import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 


public final class JodaTimeConverter implements Converter { 

    @Override 
    @SuppressWarnings("unchecked") 
    public boolean canConvert(final Class type) { 
      return (type != null) && DateTime.class.getPackage().equals(type.getPackage()); 
    } 

    @Override 
    public void marshal(final Object source, final HierarchicalStreamWriter writer, 
      final MarshallingContext context) { 
      writer.setValue(source.toString()); 
    } 

    @Override 
    @SuppressWarnings("unchecked") 
    public Object unmarshal(final HierarchicalStreamReader reader, 
      final UnmarshallingContext context) { 
      try { 
        final Class requiredType = context.getRequiredType(); 
        final Constructor constructor = requiredType.getConstructor(Object.class); 
        return constructor.newInstance(reader.getValue()); 
      } catch (final Exception e) { 
       throw new RuntimeException(String.format(
       "Exception while deserializing a Joda Time object: %s", context.getRequiredType().getSimpleName()), e); 
      } 
    } 

} 

你可以用它喜歡:

XStream xstream = new XStream(new StaxDriver()); 
xstream.registerConverter(new JodaTimeConverter()); 
3

從@Ben卡爾森的版本有一個問題,如果你的對象樹包含來自同一個包的DateTime其他類。

一個更強大的版本,日期時間轉換爲XML和背部不需要反思,以及:

public static class JodaTimeConverter implements Converter 
{ 
    @Override 
    @SuppressWarnings("unchecked") 
    public boolean canConvert(final Class type) 
    { 
     return DateTime.class.isAssignableFrom(type); 
    } 

    @Override 
    public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) 
    { 
     writer.setValue(source.toString()); 
    } 

    @Override 
    @SuppressWarnings("unchecked") 
    public Object unmarshal(HierarchicalStreamReader reader, 
          UnmarshallingContext context) 
    { 
     return new DateTime(reader.getValue()); 
    } 
} 

註冊轉換器使用XStream使用它:

XStream xstream = new XStream(); 
xstream.registerConverter(new JodaTimeConverter()); 
2

我們需要一個以將Joda DateTime轉換爲XML屬性/從一個XML屬性轉換。爲此,轉換器需要實現SingleValueConverter接口。我們的最終實施:

package com.squins.xstream.joda; 

import org.joda.time.DateTime; 

import com.thoughtworks.xstream.converters.ConversionException; 
import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter; 


public final class JodaDateTimeConverter extends AbstractSingleValueConverter 
{ 

    @Override 
    public boolean canConvert(final Class type) 
    { 
    return DateTime.class.equals(type); 
    } 

    @Override 
    public Object fromString(String str) 
    { 
    try 
    { 
     return new DateTime(str); 
    } 
    catch (final Exception e) 
    { 
     throw new ConversionException("Cannot parse date " + str); 
    } 
    } 
} 
0

我已經使用了一個它是here。粘貼爲了簡單:

public class JodaTimeConverter implements Converter 
{ 
    @Override 
    public boolean canConvert(Class type) { 
     return type != null && DateTime.class.getPackage().equals(type.getPackage()); 
    } 

    @Override 
    public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { 
     writer.setValue(source.toString()); 
    } 

    @Override 
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { 
     try { 
      Constructor constructor = context.getRequiredType().getConstructor(Object.class); 
      return constructor.newInstance(reader.getValue()); 
     } catch (Exception e) { // NOSONAR 
      throw new SerializationException(String.format(
      "An exception occurred while deserializing a Joda Time object: %s", 
      context.getRequiredType().getSimpleName()), e); 
     } 
    } 
} 

其他樣品沒有工作。 乾杯!