Whimusical
Whimusical

Reputation: 6649

Marshal an object as one of its properties with Jackson (as for value objects i.e primitive types wrappers)

EDIT: Previous answer does not work (it stills create a nested object) I'm using Jersey and Jackson.

I got a class like

@XmlAccessorType(XmlAccessType.NONE)
public class Name {
   private String value;
   @XmlValue
   public String getValue(){...}
   public void setValue(String value){...}
}

used as in

public class Person{

   @XmlElement(name = "IDName")
   public Name getName(){...}
}

I'd like to marshal Name object as the value of it's identity property. How can I achieve that?

<Person>
  <IDName>foo</IDName>
</Person>

instead of

<Person>
  <IDName>
      <Value>foo</Value>
  </IDName>
</Person>

I'd tried both to indicate in Person that Name object should be marshalled as itself.getValue() and either to indicate within Name class to marshal without any element wrapper (its fields directly) with no luck.

Upvotes: 3

Views: 451

Answers (1)

Whimusical
Whimusical

Reputation: 6649

A possible solution is replacing @XmlValue annotation with Jackson's @JsonValue to make it work (tested). I infer from http://wiki.fasterxml.com/JacksonJAXBAnnotations that it can be the only solution for now

According to this the official documentation

@javax.xml.bind.annotation.XmlValue
The field/property to which this annotation is applied will be named "value".

So maybe it's limited by design. Any better answer, specially if using JAXB annotations alone, will be much appreciated

Upvotes: 1

Related Questions