JsonXMLObjectProvider extends AbstractJsonXMLProvider { @Override public void write( Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, Writer stream, Object value) throws IOException, WebApplicationException { JsonXML config = getJsonXML(type, annotations); try { writeObject(type, config, getContext(type, mediaType), stream, value); } catch (XMLStreamException e) { throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR); } catch (JAXBException e) { throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR); } } JsonXMLObjectProvider(@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 value);  }
@Test public void testWriteSampleRootElement() throws Exception { JsonXMLObjectProvider provider = new JsonXMLObjectProvider(null); Annotation[] annotations = new Annotation[0]; SampleRootElement sampleRootElement = new SampleRootElement(); sampleRootElement.attribute = "hello"; sampleRootElement.elements = Arrays.asList("world"); StringWriter writer = new StringWriter(); provider.write(SampleRootElement.class, null, annotations, MediaType.APPLICATION_JSON_TYPE, null, writer, sampleRootElement); String json = "{\"sampleRootElement\":{\"@attribute\":\"hello\",\"elements\":[\"world\"]}}"; Assert.assertEquals(json, writer.toString()); }
@Test public void testWriteSampleType() throws Exception { JsonXMLObjectProvider provider = new JsonXMLObjectProvider(null); Annotation[] annotations = new Annotation[]{JsonXMLDefault.class.getAnnotation(JsonXML.class)}; SampleType sampleType = new SampleType(); sampleType.element = "hi!"; StringWriter writer = new StringWriter(); provider.write(SampleType.class, null, annotations, MediaType.APPLICATION_JSON_TYPE, null, writer, sampleType); String json = "{\"sampleType\":{\"element\":\"hi!\"}}"; Assert.assertEquals(json, writer.toString()); }
@Test public void testWriteNull() throws Exception { JsonXMLObjectProvider provider = new JsonXMLObjectProvider(null); Annotation[] annotations = new Annotation[0]; StringWriter writer = new StringWriter(); provider.write(SampleRootElement.class, null, annotations, MediaType.APPLICATION_JSON_TYPE, null, writer, null); String json = "null"; Assert.assertEquals(json, writer.toString()); }
@Test public void testWriteNullWithVirtualRoot() throws Exception { JsonXMLObjectProvider provider = new JsonXMLObjectProvider(null); Annotation[] annotations = new Annotation[]{JsonXMLVirtualSampleRootElement.class.getAnnotation(JsonXML.class)}; StringWriter writer = new StringWriter(); provider.write(SampleRootElement.class, null, annotations, MediaType.APPLICATION_JSON_TYPE, null, writer, null); String json = "null"; Assert.assertEquals(json, writer.toString()); }