Home>

When outputting a Java object to a JSON string using jackson
If I want to output the following [Output I want] by executing the corresponding source code,
Is it possible to deal with annotations alone?

[Output to be output]
{
    "fugaList": [
        {
            "fuga": {
                "prop1": "AAAA",
                "prop2": "BBBB"
            }
        },
        {
            "fuga": {
                "prop1": "XXXX",
                "prop2": "YYYY"
            }
        }
    ]
}
[Actual output result]
{
    "fugaList": [
        {
            "prop1": "AAAA",
            "prop2": "BBBB"
        },
        {
            "prop1": "XXXX",
            "prop2": "YYYY"
        }
    ]
}
Applicable source code
public class Hoge {
    @JsonProperty ("fugaList")
    private List<Fuga>fugaList;
    public Hoge (List<Fuga>fugaList) {
        this.fugaList = fugaList;
    }
    public List<Fuga>getFugaList () {
        return fugaList;
    }
}
public class Fuga {
    @JsonProperty ("prop1")
    private String prop1;
    @JsonProperty ("prop2")
    private String prop2;
    public Fuga (String prop1, String prop2) {
        this.prop1 = prop1;
        this.prop2 = prop2;
    }
    public String getProp1 () {
        return prop1;
    }
    public String getProp2 () {
        return prop2;
    }
}
public class Test {
    public static void main (String [] args) throws Exception {
        Fuga fuga1 = new Fuga ("AAAA", "BBBB");
        Fuga fuga2 = new Fuga ("XXXX", "YYYY");
        List<Fuga>fugaList = new ArrayList<Fuga>();
        fugaList.add (fuga1);
        fugaList.add (fuga2);
        Hoge hoge = new Hoge (fugaList);
        ObjectMapper mapper = new ObjectMapper ();
        System.out.println (mapper.writeValueAsString (hoge));
    }
}