Stephen D
Stephen D

Reputation: 3076

Jackson serializes strange output

I am using Jackson to convert json to an object. However, the json looks wrong. Here is what I am seeing:

"interfaces": {"interfaces": [
  "HA_1",
  "HA_2"
]},

There should not be two interfaces. I want to see:

"interfaces": [
  "HA_1",
  "HA_2"
]},

I am not sure how this is happening. I can show you my conversion classes:

@XmlAccessorType(XmlAccessType.NONE)
public class InterfacesRep implements Serializable {
    private static final long serialVersionUID = -1503363608473342020L;

    @XmlElement(name = "interface", type = String.class)
    private Collection<String> all = new ArrayList<String>();

    public InterfacesRep() {}

    public InterfacesRep(Collection<String> all) {
        this.all = all;
    }

    public Collection<String> getAll() {
        return all;
    }

    public void setAll(List<String> all) {
        this.all = all;
    }
}

And the outer class:

public class OuterRep {

    private static final long serialVersionUID = -1719378545790376294L;

    @XmlElement(name = "interfaces", type=InterfacesRep.class)
    private InterfacesRep interfaces;

    public OuterRep() {
    }

    public InterfacesRep getInterfaces() {
        return interfaces;
    }

    public void setInterfaces(InterfacesRep interfaces) {
        this.interfaces = interfaces;
    }
}

Do you know why I see "interfaces" twice?

Upvotes: 0

Views: 280

Answers (1)

toadzky
toadzky

Reputation: 3846

Because you are defining it on the property at both levels.The outer class has a property name called "interfaces" and the inner class's Collection is also named "interfaces".

This simplest fix (in my mind) would be to not use a wrapper class for the Collection. Just put the collection in the outer class.

On a side note, why are you using Jackson's XML annotations to serialize JSON?

Upvotes: 2

Related Questions