JsonXMLBinder { public <T> List<T> readArray(Class<? extends T> type, JsonXML config, JAXBContext context, Reader stream) throws XMLStreamException, JAXBException { checkBindable(type); XMLStreamReader reader = createXMLStreamReader(type, config, stream); List<T> result; if (reader.isCharacters() && reader.getText() == null) { result = null; } else { boolean documentArray = JsonXMLStreamConstants.MULTIPLE_PI_TARGET.equals(reader.getPITarget()); Unmarshaller unmarshaller = context.createUnmarshaller(); while (reader.hasNext() && !reader.isStartElement() && !reader.isCharacters()) { reader.next(); } result = new ArrayList<T>(); while (reader.hasNext() || reader.isCharacters() && reader.getText() == null) { if (reader.isCharacters() && reader.getText() == null) { result.add(null); if (reader.hasNext()) { reader.next(); } else { break; } } else { result.add(unmarshal(type, config, unmarshaller, reader)); if (documentArray && reader.hasNext()) { reader.next(); } } } } reader.close(); return result; } JsonXMLBinder(); protected  JsonXMLBinder(boolean writeDocumentArray); protected  JsonXMLBinder(JsonXMLRootProvider rootProvider, boolean writeDocumentArray); boolean isBindable(Class<?> type); T readObject(Class<? extends T> type, JsonXML config, JAXBContext context, Reader stream); void writeObject(Class<?> type, JsonXML config, JAXBContext context, Writer stream, Object value); List<T> readArray(Class<? extends T> type, JsonXML config, JAXBContext context, Reader stream); void writeArray(Class<?> type, JsonXML config, JAXBContext context, Writer stream, Collection<?> collection);  }
@Test public void testReadArraySampleRootElement() throws Exception { JsonXML config = JsonXMLDefault.class.getAnnotation(JsonXML.class); String json = "{\"sampleRootElement\":[{\"@attribute\":\"hello\"},{\"@attribute\":\"world\"}]}"; JAXBContext context = JAXBContext.newInstance(SampleRootElement.class); List<SampleRootElement> list = new JsonXMLBinder().readArray(SampleRootElement.class, config, context, new StringReader(json)); Assert.assertEquals(2, list.size()); Assert.assertEquals("hello", list.get(0).attribute); Assert.assertEquals("world", list.get(1).attribute); }
@Test public void testReadArraySampleRootElement_DocumentArray() throws Exception { JsonXML config = JsonXMLDefault.class.getAnnotation(JsonXML.class); String json = "[{\"sampleRootElement\":{\"@attribute\":\"hello\"}},{\"sampleRootElement\":{\"@attribute\":\"world\"}}]"; JAXBContext context = JAXBContext.newInstance(SampleRootElement.class); List<SampleRootElement> list = new JsonXMLBinder().readArray(SampleRootElement.class, config, context, new StringReader(json)); Assert.assertEquals(2, list.size()); Assert.assertEquals("hello", list.get(0).attribute); Assert.assertEquals("world", list.get(1).attribute); }
@Test public void testReadArraySampleTypeWithNull1() throws Exception { JsonXML config = JsonXMLDefault.class.getAnnotation(JsonXML.class); String json = "{\"sampleType\":[{\"element\":\"hi!\"},null]}"; JAXBContext context = JAXBContext.newInstance(SampleType.class); List<SampleType> list = new JsonXMLBinder().readArray(SampleType.class, config, context, new StringReader(json)); Assert.assertEquals(2, list.size()); Assert.assertEquals("hi!", list.get(0).element); Assert.assertNull(list.get(1).element); }
@Test public void testReadArraySampleTypeWithNull2() throws Exception { JsonXML config = JsonXMLDefault.class.getAnnotation(JsonXML.class); String json = "{\"sampleType\":[null,{\"element\":\"hi!\"}]}"; JAXBContext context = JAXBContext.newInstance(SampleType.class); List<SampleType> list = new JsonXMLBinder().readArray(SampleType.class, config, context, new StringReader(json)); Assert.assertEquals(2, list.size()); Assert.assertNull(list.get(0).element); Assert.assertEquals("hi!", list.get(1).element); }
@Test public void testReadArraySampleTypeWithNull_DocumentArray1() throws Exception { JsonXML config = JsonXMLDefault.class.getAnnotation(JsonXML.class); String json = "[{\"sampleType\":{\"element\":\"hi!\"}},null]"; JAXBContext context = JAXBContext.newInstance(SampleType.class); List<SampleType> list = new JsonXMLBinder().readArray(SampleType.class, config, context, new StringReader(json)); Assert.assertEquals(2, list.size()); Assert.assertEquals("hi!", list.get(0).element); Assert.assertNull(list.get(1)); }
@Test public void testReadArraySampleTypeWithNull_DocumentArray2() throws Exception { JsonXML config = JsonXMLDefault.class.getAnnotation(JsonXML.class); String json = "[null,{\"sampleType\":{\"element\":\"hi!\"}}]"; JAXBContext context = JAXBContext.newInstance(SampleType.class); List<SampleType> list = new JsonXMLBinder().readArray(SampleType.class, config, context, new StringReader(json)); Assert.assertEquals(2, list.size()); Assert.assertNull(list.get(0)); Assert.assertEquals("hi!", list.get(1).element); }
@Test public void testReadArrayNull() throws Exception { JsonXML config = JsonXMLDefault.class.getAnnotation(JsonXML.class); JAXBContext context = JAXBContext.newInstance(SampleType.class); Assert.assertNull(new JsonXMLBinder().readArray(SampleType.class, config, context, new StringReader("null"))); }
@Test public void testReadArrayNullWithVirtualRoot() throws Exception { JsonXML config = JsonXMLVirtualSampleRootElement.class.getAnnotation(JsonXML.class); JAXBContext context = JAXBContext.newInstance(SampleType.class); Assert.assertNotNull(new JsonXMLBinder().readArray(SampleType.class, config, context, new StringReader("null"))); }