java - Joda-Time `DateTime` 对象到 `org.simpleframework.xml` ("Simple XML Serialization"库中字符串的转换器)

标签 java xml serialization jodatime simple-framework

如何为 org.simpleframework.xml 库构建转换器?

我正在使用Simple XML Serialization library (org.simpleframework.xml 包)来自 SimpleFramework.org

我要Joda-Time DateTime要序列化为 ISO 8601 的对象字符串,例如 2014-07-16T00:20:36Z。在重新构造 Java 对象时,我希望从该字符串构造一个 DateTime。 documentation并没有真正解释如何构建转换器。

我知道这与 Transform 有关。和一个Matcher 。在 MythTV-Service-API项目中,我发现了 Transform 的实现和 Matcher 。但我还没有确定如何将它们组合在一起。

最佳答案

您可以在两种方法之间进行选择,如类似问题 Serialization third-party classes with Simple XML (org.simpleframework.xml) 中所讨论的那样。 :

  • 转换器
  • 变换

我不知道比较每个的优缺点。但我确实知道如何实现 Transform 方法。为此,请继续阅读。

转换方法

需要三 block :

  1. Transform interface 的实现.
  2. Matcher interface 的实现.
  3. 一个RegistryMatcher Transform 实现映射到它处理的数据类型的实例。

所有这三个都是 transform package 的一部分.

我建议将您的实现放入项目中的“转换器”包中。

转换实现

您的 Transform 实现可能如下所示。

这里的实现很简单。它假设您希望输出是由 DateTime 的 toString 生成的默认 ISO 8601 字符串。方法。它假设每个文本输入都与 DateTime 中的默认解析器兼容。构造函数。要处理其他格式,请定义一堆 DateTimeFormatter实例,调用 parseDateTime连续对每个方法进行调用,直到格式化程序成功而不抛出 IllegalArgumentException 。另一件需要考虑的事情是时区;您可能想将时区强制为 UTC 或类似的时区。

package com.your.package.converters.simplexml;

import org.joda.time.DateTime;
import org.simpleframework.xml.transform.Transform;
import org.slf4j.LoggerFactory;

/**
 *
 * © 2014 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for such usage and its consequences.
 */
public class JodaTimeDateTimeTransform implements Transform<DateTime>
{
    //static final org.slf4j.Logger logger = LoggerFactory.getLogger( JodaTimeDateTimeTransform.class );

    @Override
    public DateTime read ( String input ) throws Exception
    {
        DateTime dateTime = null;
        try {
            dateTime = new DateTime( input );  // Keeping whatever offset is included. Not forcing to UTC.
        } catch ( Exception e ) {
            //logger.debug( "Joda-Time DateTime Transform failed. Exception: " + e );
        }
        return dateTime;
    }

    @Override
    public String write ( DateTime dateTime ) throws Exception
    {
        String output = dateTime.toString();  // Keeping whatever offset is included. Not forcing to UTC.
        return output;
    }

}

匹配器实现

Matcher 的实现既快速又简单。

 package com.your.package.converters.simplexml;

import org.simpleframework.xml.transform.Transform;
import org.simpleframework.xml.transform.Matcher;

/**
 *
 * © 2014 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for such usage and its consequences.
 */
public class JodaTimeDateTimeMatcher implements Matcher
{
    @Override
    public Transform match ( Class classType ) throws Exception
    {
        // Is DateTime a superclass (or same class) the classType?
        if ( org.joda.time.DateTime.class.isAssignableFrom( classType ) ) {
            return new JodaTimeDateTimeTransform();
        }
        return null;
    }
}

注册表

将这些付诸实践意味着使用注册表。

RegistryMatcher matchers = new RegistryMatcher();
matchers.bind( org.joda.time.DateTime.class , JodaTimeDateTimeTransform.class );
// You could add other data-type handlers, such as the "YearMonth" class in Joda-Time.
//matchers.bind( org.joda.time.YearMonth.class , JodaTimeYearMonthTransform.class );

Strategy strategy = new AnnotationStrategy();
Serializer serializer = new Persister( strategy , matchers );

并继续使用理解 Joda-Time 类型的序列化程序的通常方式。

关于java - Joda-Time `DateTime` 对象到 `org.simpleframework.xml` ("Simple XML Serialization"库中字符串的转换器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24792032/

相关文章:

Java VTD-XML 找不到符号错误。编译java时如何正确引用包?

java - 在文件中写入多个对象并读取它们

perl - 如何将 "serialize"和 "deserialize"命令行参数传入/传出字符串?

serialization - 使用 jackson 序列化 Map.Entry 时出现问题

Java通过jenkins获取主机名和其他环境变量

java - 使用 Google GSON 的 Lotus Domino Java 安全问题

java - boolean 组合的开关结构

java - 通过 Web 应用程序连接两个调用

php - 使用 foreach() 解析 XML 子项

sql-server - 使用 T-SQL 粉碎 XML - 从组内提取行值的正确语法是什么?