JsonXMLBinder { public <T> T readObject(Class<? extends T> type, JsonXML config, JAXBContext context, Reader stream) throws XMLStreamException, JAXBException { checkBindable(type); XMLStreamReader reader = createXMLStreamReader(type, config, stream); T result; if (reader.isCharacters() && reader.getText() == null) { result = null; } else { reader.require(XMLStreamConstants.START_DOCUMENT, null, null); Unmarshaller unmarshaller = context.createUnmarshaller(); result = unmarshal(type, config, unmarshaller, reader); reader.require(XMLStreamConstants.END_DOCUMENT, null, null); } 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 testReadObjectSampleRootElement() throws Exception { JsonXML config = JsonXMLDefault.class.getAnnotation(JsonXML.class); String json = "{\"sampleRootElement\":{\"@attribute\":\"hello\",\"elements\":[\"world\"]}}"; JAXBContext context = JAXBContext.newInstance(SampleRootElement.class); SampleRootElement sampleRootElement = new JsonXMLBinder().readObject(SampleRootElement.class, config, context, new StringReader(json)); Assert.assertEquals("hello", sampleRootElement.attribute); Assert.assertEquals("world", sampleRootElement.elements.get(0)); }
@Test public void testReadObjectSampleType() throws Exception { JsonXML config = JsonXMLDefault.class.getAnnotation(JsonXML.class); String json = "{\"sampleType\":{\"element\":\"hi!\"}}"; JAXBContext context = JAXBContext.newInstance(SampleType.class); SampleType sampleType = new JsonXMLBinder().readObject(SampleType.class, config, context, new StringReader(json)); Assert.assertEquals("hi!", sampleType.element); }
@Test public void testReadObjectNull() throws Exception { JsonXML config = JsonXMLDefault.class.getAnnotation(JsonXML.class); JAXBContext context = JAXBContext.newInstance(SampleRootElement.class); Assert.assertNull(new JsonXMLBinder().readObject(SampleRootElement.class, config, context, new StringReader("null"))); }
@Test public void testReadObjectNullWithVirtualRoot() throws Exception { JsonXML config = JsonXMLVirtualSampleRootElement.class.getAnnotation(JsonXML.class); JAXBContext context = JAXBContext.newInstance(SampleRootElement.class); Assert.assertNotNull(new JsonXMLBinder().readObject(SampleRootElement.class, config, context, new StringReader("null"))); }