JsonXMLArrayProvider extends AbstractJsonXMLProvider { @Override public Object read( Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, Reader stream) throws IOException, WebApplicationException { Class<?> componentType = getComponentType(type, genericType); JsonXML config = getJsonXML(componentType, annotations); List<?> list; try { list = readArray(componentType, config, getContext(componentType, mediaType), stream); } catch (XMLStreamException e) { throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR); } catch (JAXBException e) { throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR); } if (list == null) { return null; } else if (type.isArray()) { return toArray(list, componentType); } else { Collection<Object> collection = createCollection(type); if (collection == null) { throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR); } collection.addAll(list); return collection; } } JsonXMLArrayProvider(@Context Providers providers); @Override Object read(
			Class<?> type,
			Type genericType,
			Annotation[] annotations,
			MediaType mediaType,
			MultivaluedMap<String, String> httpHeaders,
			Reader stream); @Override void write(
			Class<?> type,
			Type genericType,
			Annotation[] annotations,
			MediaType mediaType,
			MultivaluedMap<String, Object> httpHeaders,
			Writer stream,
			Object entry);  }
@Test public void testReadRootElementList() throws Exception { JsonXMLArrayProvider provider = new JsonXMLArrayProvider(null); Annotation[] annotations = new Annotation[0]; Type type = getClass().getDeclaredField("sampleRootElementList").getGenericType(); String json = "{\"sampleRootElement\":[{\"@attribute\":\"hello\"},{\"@attribute\":\"world\"}]}"; @SuppressWarnings("unchecked") List<SampleRootElement> list = (List<SampleRootElement>)provider.read(List.class, type, annotations, MediaType.APPLICATION_JSON_TYPE, null, 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 testReadRootElementList_DocumentArray() throws Exception { JsonXMLArrayProvider provider = new JsonXMLArrayProvider(null); Annotation[] annotations = new Annotation[0]; Type type = getClass().getDeclaredField("sampleRootElementList").getGenericType(); String json = "[{\"sampleRootElement\":{\"@attribute\":\"hello\"}},{\"sampleRootElement\":{\"@attribute\":\"world\"}}]"; @SuppressWarnings("unchecked") List<SampleRootElement> list = (List<SampleRootElement>)provider.read(List.class, type, annotations, MediaType.APPLICATION_JSON_TYPE, null, 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 testReadSampleTypeList() throws Exception { JsonXMLArrayProvider provider = new JsonXMLArrayProvider(null); Annotation[] annotations = new Annotation[]{JsonXMLDefault.class.getAnnotation(JsonXML.class)}; Type type = getClass().getDeclaredField("sampleTypeList").getGenericType(); String json = "{\"sampleType\":[{\"element\":\"hello\"},{\"element\":\"world\"}]}"; @SuppressWarnings("unchecked") List<SampleType> list = (List<SampleType>)provider.read(List.class, type, annotations, MediaType.APPLICATION_JSON_TYPE, null, new StringReader(json)); Assert.assertEquals(2, list.size()); Assert.assertEquals("hello", list.get(0).element); Assert.assertEquals("world", list.get(1).element); }
@Test public void testReadSampleTypeList_DocumentArray() throws Exception { JsonXMLArrayProvider provider = new JsonXMLArrayProvider(null); Annotation[] annotations = new Annotation[]{JsonXMLDefault.class.getAnnotation(JsonXML.class)}; Type type = getClass().getDeclaredField("sampleTypeList").getGenericType(); String json = "[{\"sampleType\":{\"element\":\"hello\"}},{\"sampleType\":{\"element\":\"world\"}}]"; @SuppressWarnings("unchecked") List<SampleType> list = (List<SampleType>)provider.read(List.class, type, annotations, MediaType.APPLICATION_JSON_TYPE, null, new StringReader(json)); Assert.assertEquals(2, list.size()); Assert.assertEquals("hello", list.get(0).element); Assert.assertEquals("world", list.get(1).element); }
@Test public void testReadSampleTypeListWithNullValues() throws Exception { JsonXMLArrayProvider provider = new JsonXMLArrayProvider(null); Annotation[] annotations = new Annotation[]{JsonXMLDefault.class.getAnnotation(JsonXML.class)}; Type type = getClass().getDeclaredField("sampleTypeList").getGenericType(); String json = "[null,{\"sampleType\":{\"element\":\"hi!\"}},null]"; @SuppressWarnings("unchecked") List<SampleType> list = (List<SampleType>)provider.read(List.class, type, annotations, MediaType.APPLICATION_JSON_TYPE, null, new StringReader(json)); Assert.assertEquals(3, list.size()); Assert.assertNull(list.get(0)); Assert.assertEquals("hi!", list.get(1).element); Assert.assertNull(list.get(2)); }
@Test public void testReadEmptyList() throws Exception { JsonXMLArrayProvider provider = new JsonXMLArrayProvider(null); Annotation[] annotations = new Annotation[0]; Type type = getClass().getDeclaredField("sampleRootElementList").getGenericType(); String json = "{}"; @SuppressWarnings("unchecked") List<SampleRootElement> list = (List<SampleRootElement>)provider.read(List.class, type, annotations, MediaType.APPLICATION_JSON_TYPE, null, new StringReader(json)); Assert.assertEquals(0, list.size()); }
@Test public void testReadEmptyList2() throws Exception { JsonXMLArrayProvider provider = new JsonXMLArrayProvider(null); Annotation[] annotations = new Annotation[0]; Type type = getClass().getDeclaredField("sampleRootElementList").getGenericType(); String json = "{\"sampleRootElement\":[]}"; @SuppressWarnings("unchecked") List<SampleRootElement> list = (List<SampleRootElement>)provider.read(List.class, type, annotations, MediaType.APPLICATION_JSON_TYPE, null, new StringReader(json)); Assert.assertEquals(0, list.size()); }
@Test public void testReadEmptyList3() throws Exception { JsonXMLArrayProvider provider = new JsonXMLArrayProvider(null); Annotation[] annotations = new Annotation[0]; Type type = getClass().getDeclaredField("sampleRootElementList").getGenericType(); String json = "[]"; @SuppressWarnings("unchecked") List<SampleRootElement> list = (List<SampleRootElement>)provider.read(List.class, type, annotations, MediaType.APPLICATION_JSON_TYPE, null, new StringReader(json)); Assert.assertEquals(0, list.size()); }
@Test public void testReadEmptyListWithVirtualRoot() throws Exception { JsonXMLArrayProvider provider = new JsonXMLArrayProvider(null); Annotation[] annotations = new Annotation[]{JsonXMLVirtualSampleRootElement.class.getAnnotation(JsonXML.class)}; Type type = getClass().getDeclaredField("sampleRootElementList").getGenericType(); String json = "[]"; @SuppressWarnings("unchecked") List<SampleRootElement> list = (List<SampleRootElement>)provider.read(List.class, type, annotations, MediaType.APPLICATION_JSON_TYPE, null, new StringReader(json)); Assert.assertEquals(0, list.size()); }