Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson XML to JSON not formatting lists correctly

I am converting raw XML input to JSON and I am having issues with the desired structure of the output (lists not displaying correctly). The XML structure being imported can differ to the example below so using a POJO and simply annotating is not feasible. Sample XML input below:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<gang>
    <id>435dfb3f-1129-4375-b0f9-09955d7434cc</id>
    <name>Brew's Crews</name>
    <members>
        <member>
            <member>d3433b1c-a93d-4af1-b698-89fcd921e48d</member>
            <dateJoined/>
        </member>
        <member>
            <member>8ac9f5bc-5710-4cb1-a75d-839e211f0286</member>
            <dateJoined/>
        </member>
    </members>
    <anthem/>
    <logo>http://localhost:8080/cloud/master-index-record/raw/58338b91-2390-44a7-ac31-581c5dd921e1</logo>
</gang>

I am using Jackson to convert the XML to JSON (the XML is already a string and represented by the 'result' variable)

XmlMapper xmlMapper = new XmlMapper();
Object entry = xmlMapper.readValue(result, Object.class);   
ObjectMapper jsonMapper = new ObjectMapper();
return  jsonMapper.writeValueAsString(entry);

The following JSON is produced:

{
  "id": "435dfb3f-1129-4375-b0f9-09955d7434cc",
  "name": "Brew's Crews",
  "members": {
    "member": {
      "member": "8ac9f5bc-5710-4cb1-a75d-839e211f0286",
      "dateJoined": null
    }
  },
  "anthem": null,
  "logo": "http://localhost:8080/cloud/master-index-record/raw/58338b91-2390-44a7-ac31-581c5dd921e1"
}

My issue is that the 'members' elements should be grouped in a JSON array like the following:

"members":[{"member":blah,"dateJoined":null},{"member":blah2,"dateJoined":null}]

But they are not....is there a specific configuration needed to be done to the XML mapper or Object mapper to achieve these desired results?

like image 381
mkez00 Avatar asked Nov 09 '22 02:11

mkez00


1 Answers

Jackson in general will not be able to process generalized types such as Objects. When you do -

Object entry = xmlMapper.readValue(result, Object.class);   

Jackson doesn't know the structure of the object it's supposed to deserialize.

You can try this -

class Member {
    private String memberId;
    private String dateJoined;
}

class Gang {
    private String id;
    private String name;
    private List<Member> members;
    private String anthem;
    private String logo;
}

class Test {

    public static void main(String[] args) {
        String result = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><Gang><id>435dfb3f-1129-4375-b0f9-09955d7434cc</id><name>Brew's Crews</name><members><member><memberId>d3433b1c-a93d-4af1-b698-89fcd921e48d</memberId><dateJoined/></member><member><memberId>8ac9f5bc-5710-4cb1-a75d-839e211f0286</memberId><dateJoined/></member></members><anthem/><logo>http://localhost:8080/cloud/master-index-record/raw/58338b91-2390-44a7-ac31-581c5dd921e1</logo></Gang>";

        XmlMapper xmlMapper = new XmlMapper();
        xmlMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

        Object entry = xmlMapper.readValue(result, Gang.class);   
        ObjectMapper jsonMapper = new ObjectMapper();
        jsonMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

        System.out.println(jsonMapper.writeValueAsString(entry));
    }

}
like image 184
TR1 Avatar answered Nov 14 '22 22:11

TR1