JsonXMLObjectProvider extends AbstractJsonXMLProvider { @Override public Object read( Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, Reader stream) throws IOException, WebApplicationException { JsonXML config = getJsonXML(type, annotations); try { return readObject(type, config, getContext(type, mediaType), stream); } 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 testReadSampleRootElement() throws Exception { JsonXMLObjectProvider provider = new JsonXMLObjectProvider(null); Annotation[] annotations = new Annotation[0]; String json = "{\"sampleRootElement\":{\"@attribute\":\"hello\",\"elements\":[\"world\"]}}"; SampleRootElement sampleRootElement = (SampleRootElement)provider.read(SampleRootElement.class, null, annotations, MediaType.APPLICATION_JSON_TYPE, null, new StringReader(json)); Assert.assertEquals("hello", sampleRootElement.attribute); Assert.assertEquals("world", sampleRootElement.elements.get(0)); }
@Test public void testReadSampleType() throws Exception { JsonXMLObjectProvider provider = new JsonXMLObjectProvider(null); Annotation[] annotations = new Annotation[]{JsonXMLDefault.class.getAnnotation(JsonXML.class)}; String json = "{\"sampleType\":{\"element\":\"hi!\"}}"; SampleType sampleType = (SampleType)provider.read(SampleType.class, null, annotations, MediaType.APPLICATION_JSON_TYPE, null, new StringReader(json)); Assert.assertEquals("hi!", sampleType.element); }
@Test public void testReadNull() throws Exception { JsonXMLObjectProvider provider = new JsonXMLObjectProvider(null); Annotation[] annotations = new Annotation[0]; String json = "null"; SampleRootElement sampleRootElement = (SampleRootElement)provider.read(SampleRootElement.class, null, annotations, MediaType.APPLICATION_JSON_TYPE, null, new StringReader(json)); Assert.assertNull(sampleRootElement); }
@Test public void testReadNullWithVirtualRoot() throws Exception { JsonXMLObjectProvider provider = new JsonXMLObjectProvider(null); Annotation[] annotations = new Annotation[]{JsonXMLVirtualSampleRootElement.class.getAnnotation(JsonXML.class)}; String json = "null"; SampleRootElement sampleRootElement = (SampleRootElement)provider.read(SampleRootElement.class, null, annotations, MediaType.APPLICATION_JSON_TYPE, null, new StringReader(json)); Assert.assertNotNull(sampleRootElement); }