Marshal object as one of its properties with Jackson (both for objects with values ​​and primitive wrapper types)

EDIT: The previous answer doesn't work (it still creates a nested object) I'm using Jersey and Jackson.

I have a class like

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

      

used both in

public class Person{

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

      

I would like to marshal a Name object as the value of its identity property. How can I achieve this?

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

      

instead

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

      

I tried both to specify in Person that the Name object should be sorted as self.getValue () and either specify in the Name class a marshal without any element wrapping (its field directly) with no luck.

+3


source to share


1 answer


Possible solution is to replace @XmlValue annotation with Jackson @JsonValue so that it works (tested). I go from http://wiki.fasterxml.com/JacksonJAXBAnnotations that it may be the only solution at the moment

According to this 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, especially if you are only using JAXB annotations would greatly appreciate

0


source







All Articles