Gonen I
Gonen I

Reputation: 6107

@XmlElementWrapper Serialization not working using Jackson JaxB Annotations

We are using Jackson 2.9.4 and JaxB Annotations to serialize to JSON.

The @XMLElementWrapper annotation is not adding an extra wrapping level as expected. Instead, it is just changing the name of the element.

    @XmlRootElement(name = "revision")
    class A {
        private List<Integer> tickets = Arrays.asList(3,4,5);

        @XmlElement(name = "ticket")
        @XmlElementWrapper(name = "tickets")
        public List<Integer> getTickets() { return tickets; }
    }

        public void test() throws Exception {
            ObjectMapper mapper = new ObjectMapper();
            JaxbAnnotationModule jaxbAnnotationModule = new JaxbAnnotationModule();
            mapper.registerModule(jaxbAnnotationModule);
            A a = new A();
            System.out.println(mapper.writeValueAsString(a));

        }

Expected output is

{"tickets":{"ticket":[3,4,5]}}

but Actual output is

{"tickets":[3,4,5]}

Upvotes: 2

Views: 1443

Answers (1)

Gonen I
Gonen I

Reputation: 6107

I am considering working around this using a custom Jackson serializer and a custom annotation which will specify the name of the wrapper layer.

The serializer needs the name of the new wrapped layer.

The user can specify the name using a custom annotation.

Jackson creates instances of Custom Serializers in a Serializer Factory so I need to override the function which creates the Serializer, read the annotation, and let the serializer instance know the name specified there.

I am a bit concerned about fragility here as overriding serialization factory is not well documented at all, and I'm not sure what side effects it may cause.

public class XMLWrapperCollectionSerializer extends JsonSerializer<Collection> {
    private String innerFieldName = null;
    public void setInnerFieldName(String value) { this.innerFieldName = value; }
    public String getInnerFieldName() { return innerFieldName; }
    @Override
     public void serialize(Collection myClass, JsonGenerator generator, SerializerProvider provider) 
        throws JsonGenerationException, IOException {
        generator.writeStartObject();
        generator.writeFieldName(innerFieldName);
        provider.defaultSerializeValue(myClass, generator);
        generator.writeEndObject();
    } 
}

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public  @interface XMLWrapperSerializerAttributes{
  String innerFieldName();
}

public class CustomSerializerFactory extends BeanSerializerFactory {

    public CustomSerializerFactory() {
        super(null);
    }

    @Override
    protected JsonSerializer<Object> findSerializerFromAnnotation(SerializerProvider prov, Annotated a) throws JsonMappingException
    {
        JsonSerializer<Object> serializer = super.findSerializerFromAnnotation(prov,  a);

        Object serializerAsObject = (Object)serializer;
        if (serializerAsObject instanceof XMLWrapperCollectionSerializer )
        {
            XMLWrapperCollectionSerializer wrapperSerializer = (XMLWrapperCollectionSerializer) serializerAsObject;
            if ( ((XMLWrapperCollectionSerializer) serializerAsObject).getInnerFieldName() == null )
            {
             XMLWrapperSerializerAttributes annotation = a.getAnnotation(XMLWrapperSerializerAttributes.class);
             if ( annotation == null )
                throw new RuntimeException("XMLWrapperListSerializer must have innerFieldName, by annotation or code");
             wrapperSerializer.setInnerFieldName(annotation.innerFieldName());
            }
        }
        return serializer;
    }


}
@XmlRootElement(name = "revision")
class A {
    private List<Integer> tickets = new ArrayList(Arrays.asList(3,4,5));

    @XMLWrapperSerializerAttributes(innerFieldName="ticket")
    @JsonSerialize(using=XMLWrapperCollectionSerializer.class)
    @XmlElementWrapper(name = "tickets")
    @XmlElement(name = "ticket")
    public List<Integer> getTickets() { return tickets; }

public class XMLElementWrapperTest{

    @Test
    public void test() throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializerFactory(new CustomSerializerFactory());
        JaxbAnnotationModule jaxbAnnotationModule = new JaxbAnnotationModule();
        mapper.registerModule(jaxbAnnotationModule);
        A a = new A();

Upvotes: 1

Related Questions