通过JAXB为枚举提供定制值序列化 [英] Providing custom value serialization for enums via JAXB

查看:155
本文介绍了通过JAXB为枚举提供定制值序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我正在开展的项目,我们有很多使用的枚举。模型对象本身由很多小类组成;这个模型我们然后通过JAXB序列化为我们的数据库作为XML。现在,我们希望能够使用枚举中的特定方法的返回来序列化我们的枚举值;这是给定的:

 公开枚举限定符{
FOO(1E,Foo类型文档),
BAR(2,酒吧对象);

私人字符串代码,描述;

public限定符(String code,String description){
this.code = code;
this.description = description;
}

public String getCode(){
return this.code;
}

public String getDescription(){
return this.description;
}
}

等。等等。目前,当序列化为XML时,我们得到以下内容:

 <限定符> FOO< / qualifier> 

这是JAXB如何处理它。然而,我们需要该值作为getCode()的返回值,并且我们的许多枚举都遵循该约定(使用相应的静态方法通过代码进行查找),以便上述XML片段如下所示:

 < qualifier> 1E< / qualifier> 

代替。我们可以用 @XmlEnum @XmlEnumValue 进行注释,但这太麻烦了 - 一些枚举最多可以列举30个价值观,手工编辑不是很好。我们也在考虑使用自定义的串行化程序,但是我现在想避免使用该路由(但是如果这样做,那么我没有问题)。



任何想法如何?

解决方案

尝试使用 XmlAdapter 机制。您为每个枚举类型创建一个 XmlAdapter 子类,并且知道如何将XML枚举/解组件。



  public class QualifierAdapter extends XmlAdapter< String,Qualifier> {

public String marshal(限定符限定符){
return qualifier.getCode();
}

public Qualifier unmarshal(String val){
return Qualifier.getFromCode(val); //我假设你有办法这样做
}
}

然后在模型类中:

  @XmlJavaTypeAdapter(QualifierAdapter.class)
私有限定符限定符;

您还可以在包级别,在名为包的文件中声明此-info.java 在与您的模型类相同的包中,使用相当特殊的包注释:

  @ javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters({
@ javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(
type = Qualifier.class,value = QualifierAdapter.class

})
package com.xyz;


For a project I'm working on, we have a lot of enums in use. The model object itself is composed from a lot of tiny classes; this model we then serialize to our DB as XML via JAXB. Now, we want to be able to serialize our enum values using the return of a particular method in the enum; that is given:

public enum Qualifier {
    FOO("1E", "Foo type document"),
    BAR("2", "Bar object");

    private String code, description;

    public Qualifier(String code, String description) {
        this.code = code;
        this.description = description;
    }

    public String getCode() {
        return this.code;
    }

    public String getDescription() {
        return this.description;
    }
}

etc. etc. Currently, when serialized to XML, we get something like:

<qualifier>FOO</qualifier>

which is how JAXB handles it. However, we need the value to be the return of getCode(), and a whole lot of our enums do follow that convention (with a corresponding static method for lookup via code), so that the above XML fragment looks like:

<qualifier>1E</qualifier>

instead. We can annotate it with @XmlEnum and @XmlEnumValue, but that's too tedious -- some enums have up to 30 enumerated values, and hand-editing it is not good. We're also thinking of using a custom serializer instead, but I'd like to avoid going that route for now (but if that's the way to go, then I have no problem with it).

Any ideas how?

解决方案

Try using the XmlAdapter mechanism for this. You create an XmlAdapter subclass for each enum type, and which knows how to marshal/unmarshal the enum to and from XML.

You then associate the adapter with the property, e.g.

public class QualifierAdapter extends XmlAdapter<String, Qualifier> {

   public String marshal(Qualifier qualifier) {
      return qualifier.getCode();
   }

   public Qualifier unmarshal(String val) {
      return Qualifier.getFromCode(val);   // I assume you have a way of doing this
   }
}

and then in the model classes:

@XmlJavaTypeAdapter(QualifierAdapter.class)
private Qualifier qualifier;

You can also declare this at the package level, inside a file called package-info.java in the same package as your model classes, using the rather idiosyncratic package annotations:

@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters({
  @javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(
    type=Qualifier.class, value=QualifierAdapter.class
  )
})
package com.xyz;

这篇关于通过JAXB为枚举提供定制值序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆