0

I'm using Jaxb and Jettison (originally with Resteasy) to serialize an object to json. One of my objects that I'm trying to serialize includes a 2 dimensional array. How can I configure Jettison to produce a multidimensional array in json?

Here's an example that generates a multidimensional array:

public class Example {
    @XmlRootElement("test")
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Tester {

        int[][] stuff;
    }

    public static void main(String[] args) throws JAXBException {
        Tester tester = new Tester();

        tester.stuff = new int[][]{{1, 2}, {3, 4}};

        StringWriter writer = new StringWriter();

        Configuration config = new Configuration();
        MappedNamespaceConvention con = new MappedNamespaceConvention(config);
        MappedXMLStreamWriter xmlStreamWriter = new MappedXMLStreamWriter(con, writer);

        Marshaller marshaller = JAXBContext.newInstance(Tester.class)
                .createMarshaller();

        marshaller.marshal(tester, xmlStreamWriter);

        System.out.println(writer.toString());
    }
}

Which outputs the following:

{"tester":{"stuff":[{"item":[1,2]},{"item":[3,4]}]}}

But, I want to output the stuff array as a multidimensional json array like the following:

{"tester":{"stuff":[[1,2],[3,4]]}}

This seems to be possible because Resteasy serializes this way out of the box.

Cœur
  • 37,241
  • 25
  • 195
  • 267
John Ericksen
  • 10,995
  • 4
  • 45
  • 75

1 Answers1

0

Come to find out after doing some digging into Resteasy that it uses Jackson when using the default json provider in Jboss. For reference, this code provides the desired result:

public class Example {
    @XmlRootElement("test")
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Tester {

        int[][] stuff;
    }

    public static void main(String[] args) throws JAXBException {
        Tester tester = new Tester();

        tester.stuff = new int[][]{{1, 2}, {3, 4}};

        StringWriter writer = new StringWriter();

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JaxbAnnotationModule());

        System.out.println(objectMapper.writeValueAsString(tester));
    }
}
John Ericksen
  • 10,995
  • 4
  • 45
  • 75