Note that there are some explanatory texts on larger screens.

plurals
  1. POProviding custom value serialization for enums via JAXB
    primarykey
    data
    text
    <p>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:</p> <pre><code>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; } } </code></pre> <p>etc. etc. Currently, when serialized to XML, we get something like:</p> <pre><code>&lt;qualifier&gt;FOO&lt;/qualifier&gt; </code></pre> <p>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:</p> <pre><code>&lt;qualifier&gt;1E&lt;/qualifier&gt; </code></pre> <p>instead. We can annotate it with <code>@XmlEnum</code> and <code>@XmlEnumValue</code>, 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).</p> <p>Any ideas how?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload