JsonXMLArrayProvider extends AbstractJsonXMLProvider { @Override public void write( Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, Writer stream, Object entry) throws IOException, WebApplicationException { Class<?> componentType = getComponentType(type, genericType); JsonXML config = getJsonXML(componentType, annotations); Collection<?> collection; if (entry == null) { collection = null; } else if (type.isArray()) { collection = Arrays.asList((Object[]) entry); } else { collection = (Collection<?>) entry; } try { writeArray(componentType, config, getContext(componentType, mediaType), stream, collection); } catch (XMLStreamException e) { throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR); } catch (JAXBException e) { throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR); } } 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 testWriteSampleRootElementList() throws Exception { JsonXMLArrayProvider provider = new JsonXMLArrayProvider(null); Annotation[] annotations = new Annotation[0]; Type type = getClass().getDeclaredField("sampleRootElementList").getGenericType(); List<SampleRootElement> list = new ArrayList<SampleRootElement>(); list.add(new SampleRootElement()); list.get(0).attribute = "hello"; list.add(new SampleRootElement()); list.get(1).attribute = "world"; StringWriter writer = new StringWriter(); provider.write(List.class, type, annotations, MediaType.APPLICATION_JSON_TYPE, null, writer, list); String json = "[{\"sampleRootElement\":{\"@attribute\":\"hello\"}},{\"sampleRootElement\":{\"@attribute\":\"world\"}}]"; Assert.assertEquals(json, writer.toString()); }
@Test public void testWriteSampleTypeList() throws Exception { JsonXMLArrayProvider provider = new JsonXMLArrayProvider(null); Annotation[] annotations = new Annotation[]{JsonXMLDefault.class.getAnnotation(JsonXML.class)}; Type type = getClass().getDeclaredField("sampleTypeList").getGenericType(); List<SampleType> list = new ArrayList<SampleType>(); list.add(new SampleType()); list.get(0).element = "hello"; list.add(new SampleType()); list.get(1).element = "world"; StringWriter writer = new StringWriter(); provider.write(List.class, type, annotations, MediaType.APPLICATION_JSON_TYPE, null, writer, list); String json = "[{\"sampleType\":{\"element\":\"hello\"}},{\"sampleType\":{\"element\":\"world\"}}]"; Assert.assertEquals(json, writer.toString()); }
@Test public void testWriteEmptyList() throws Exception { JsonXMLArrayProvider provider = new JsonXMLArrayProvider(null); Annotation[] annotations = new Annotation[0]; Type type = getClass().getDeclaredField("sampleRootElementList").getGenericType(); List<SampleRootElement> list = new ArrayList<SampleRootElement>(); StringWriter writer = new StringWriter(); provider.write(List.class, type, annotations, MediaType.APPLICATION_JSON_TYPE, null, writer, list); String json = "[]"; Assert.assertEquals(json, writer.toString()); }
@Test public void testWriteEmptyListWithVirtualRoot() throws Exception { JsonXMLArrayProvider provider = new JsonXMLArrayProvider(null); Annotation[] annotations = new Annotation[]{JsonXMLVirtualSampleRootElement.class.getAnnotation(JsonXML.class)}; Type type = getClass().getDeclaredField("sampleRootElementList").getGenericType(); List<SampleRootElement> list = new ArrayList<SampleRootElement>(); StringWriter writer = new StringWriter(); provider.write(List.class, type, annotations, MediaType.APPLICATION_JSON_TYPE, null, writer, list); String json = "[]"; Assert.assertEquals(json, writer.toString()); }