java - 简单框架的映射 Mime 类型属性

标签 java android deserialization simple-framework

我正在使用 Simple-Framework 对我使用 Volley 库检索的原子提要进行反序列化。条目采用以下格式:

<link href="http://www.example.com/story.html" length="35743" type="html" rel="enclosure" title="" />
<link href="http://www.example.com/image.jpeg" length="35743" type="image/jpeg" />

这个的基本类模型如下:

@Root(strict=false)
public class ItemLink{
    @Attribute
    String href;

    @Attribute
    String length;

    @Attribute
    ContentTypeModel;

}

类型属性映射到枚举:

public enum ContentTypeModel {
    html,
    MP4,
    plaintext
}

具有 image/jpeg mime 类型的项目会导致错误,因为它们不存在于枚举中,但是“/”字符存在问题,它是无效的 java 语法。如何在仍使用枚举的同时映射此类型?是否可以使用 @Path 然后使用 XPath 进行解析?我想避免这种情况,但如果这是唯一的方法,那么我会使用它。

最佳答案

枚举在这里通常有点棘手,但有一个很好的解决方案:实现 Transform .

这是一个如何做到这一点的例子:

枚举ContentTypeModel :

public enum ContentTypeModel
{
    html,
    MP4,
    plaintext,
    image_jpeg // <-- this is new
}

这里没有太多新内容,只有 image/jpeg 的值 - 您可以随意命名。组合enum <--> string稍后完成。

ContentTypeModelTransform :

这里你实现了如何转换value --> stringstring --> value很简单。顺便提一句。如果你不写,只是反序列化,你可以删除 write()执行。只需实现您真正需要的。

public class ContentTypeModelTransform implements Transform<ContentTypeModel>
{
    @Override
    public ContentTypeModel read(String value) throws Exception
    {
        // The special case of 'image/jpeg'
        if( value.equalsIgnoreCase("image/jpeg") == true )
        {
            return ContentTypeModel.image_jpeg;
        }
        else // The default case - use enums valueOf()
        {
            return ContentTypeModel.valueOf(value);
        }
    }


    @Override
    public String write(ContentTypeModel value) throws Exception
    {
        // The special case of 'image/jpeg' - same as in read(), but opposite direction
        if( value == ContentTypeModel.image_jpeg ) 
        {
            return "image/jpeg"; 
        }
        else
        {
            return value.toString();
        }
    }

}

这是一个非常基本的实现,如果您遇到一长串需要手动翻译的值,最好使用字符串开关/大小写(需要 java 7+)或更好的东西。

类(class)ItemLink到目前为止不需要更改,但在调用反序列化方面存在差异:

设置Matcher :

你必须设置一个 Matcher这告诉简单 Transform 的实现是什么你想要你的枚举。

// Create a matcher - it's so small, you can implement it that way ...
final Matcher matcher = new Matcher()
{
    @Override
    public Transform match(Class type) throws Exception
    {
        /*
         * If it's that enum, simple has to use the new transformer
         * instead of the enum default one.
         */
        if( type.equals(ContentTypeModel.class) == true )
        {
            return new ContentTypeModelTransform();
        }

        return null;
    }
};


Serializer ser = new Persister(matcher); // Set matcher in the ctor
// Everything else is as usual
ItemLink il = ser.read(ItemLink.class, xml);

您可以通过多种方式实现和设置匹配器,有关更多信息/示例,请参阅简单文档。

关于java - 简单框架的映射 Mime 类型属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22850205/

相关文章:

Java - for 循环行上发生 NullPointerException

java - 可卸载导入异常 : Could not load imported ontology

java - 从表单中获取日期并保存到数据库中并列出产品

java - 为什么这会返回 null?

android - 使用 minio 来存储 Android 应用程序的附件

java - 使用键作为值反序列化 Jackson

java - 将 json 数组值反序列化为特定的 Java 类字段

安卓.view.InflateException : Binary XML file line #1: Error inflating class <unknown> background pic

android - 关闭 Activity 后如何刷新以前的 fragment ?

c++ - 将 uint8 数组反序列化为 int64 失败但应该可以