Skip to content

Serializing boxed value with type information #289

Open
@hoomanv

Description

@hoomanv

Hi,

I need to serialize a value with type information and be able to deserialize it with ease. The value is either a String or BigDecimal.

Expected XML output is like:

<data><text>foo</text></data>
<data><decimal>123</decimal></data>

Here is the Box class I use to wrap the value.

@JacksonXmlRootElement(localName = "data")
class Box
{
    @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
    @JsonSubTypes({
        @Type(value = String.class, name = "text"),
        @Type(value = BigDecimal.class, name = "decimal")
    })
    @JacksonXmlText
    Object value;
}

Here is the result when serializing string "foo" and decimal "123":

<data>foo</data>
<data>
  <value>123</value>
</data>

As you see, type info is missing for string value and is wrong for decimal value.

Removing JacksonXmlText annotation will result in:

<data>
  <value>foo</value>
</data>
<data>
  <value>
    <decimal>123</decimal>
  </value>
</data>

This time type info for decimal value was included, however there is a redundant wrapper element around it.

There is yet another acceptable output format that looks like:

<data type="text">foo</data>
<data type="decimal">123</data>

But so far I had no luck getting it to work either.

Tested with jackson 2.9.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    jaxb-type-idLabel for issues related to JAXB style "flattened" polymorphic type id

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions