JsonXMLBinder { public void writeObject(Class<?> type, JsonXML config, JAXBContext context, Writer stream, Object value) throws XMLStreamException, JAXBException { checkBindable(type); XMLStreamWriter writer = createXMLStreamWriter(type, config, stream); if (value == null) { writer.writeCharacters(null); } else { Marshaller marshaller = context.createMarshaller(); marshal(type, config, marshaller, writer, value); } writer.close(); } 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 testWriteObjectSampleRootElement() throws Exception { JsonXML config = JsonXMLDefault.class.getAnnotation(JsonXML.class); SampleRootElement sampleRootElement = new SampleRootElement(); sampleRootElement.attribute = "hello"; sampleRootElement.elements = Arrays.asList("world"); StringWriter writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(SampleRootElement.class); new JsonXMLBinder().writeObject(SampleRootElement.class, config, context, writer, sampleRootElement); String json = "{\"sampleRootElement\":{\"@attribute\":\"hello\",\"elements\":\"world\"}}"; Assert.assertEquals(json, writer.toString()); }
@Test public void testWriteObjectSampleType() throws Exception { JsonXML config = JsonXMLDefault.class.getAnnotation(JsonXML.class); SampleType sampleType = new SampleType(); sampleType.element = "hi!"; StringWriter writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(SampleType.class); new JsonXMLBinder().writeObject(SampleType.class, config, context, writer, sampleType); String json = "{\"sampleType\":{\"element\":\"hi!\"}}"; Assert.assertEquals(json, writer.toString()); }
@Test public void testWriteObjectNull() throws Exception { JsonXML config = JsonXMLDefault.class.getAnnotation(JsonXML.class); StringWriter writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(SampleType.class); new JsonXMLBinder().writeObject(SampleType.class, config, context, writer, null); Assert.assertEquals("null", writer.toString()); }
@Test public void testWriteObjectNullWithVirtualRoot() throws Exception { JsonXML config = JsonXMLVirtualSampleRootElement.class.getAnnotation(JsonXML.class); StringWriter writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(SampleType.class); new JsonXMLBinder().writeObject(SampleType.class, config, context, writer, null); Assert.assertEquals("null", writer.toString()); }