JsonXMLBinder { public void writeArray(Class<?> type, JsonXML config, JAXBContext context, Writer stream, Collection<?> collection) throws XMLStreamException, JAXBException { checkBindable(type); XMLStreamWriter writer = createXMLStreamWriter(type, config, stream); if (collection == null) { writer.writeCharacters(null); } else { Marshaller marshaller = context.createMarshaller(); if (!writeDocumentArray) { writer.writeStartDocument(); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); } writer.writeProcessingInstruction(JsonXMLStreamConstants.MULTIPLE_PI_TARGET); for (Object value : collection) { if (value == null) { writer.writeCharacters(null); } else { marshal(type, config, marshaller, writer, value); } } if (!writeDocumentArray) { writer.writeEndDocument(); } } 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 testWriteArraySampleRootElement() throws Exception { JsonXML config = JsonXMLDefault.class.getAnnotation(JsonXML.class); 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(); JAXBContext context = JAXBContext.newInstance(SampleRootElement.class); new JsonXMLBinder().writeArray(SampleRootElement.class, config, context, writer, list); String json = "[{\"sampleRootElement\":{\"@attribute\":\"hello\"}},{\"sampleRootElement\":{\"@attribute\":\"world\"}}]"; Assert.assertEquals(json, writer.toString()); }
@Test public void testWriteArraySampleRootElement_Document() throws Exception { JsonXML config = JsonXMLDefault.class.getAnnotation(JsonXML.class); 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(); JAXBContext context = JAXBContext.newInstance(SampleRootElement.class); new JsonXMLBinder(false).writeArray(SampleRootElement.class, config, context, writer, list); String json = "{\"sampleRootElement\":[{\"@attribute\":\"hello\"},{\"@attribute\":\"world\"}]}"; Assert.assertEquals(json, writer.toString()); }
@Test public void testWriteArraySampleType() throws Exception { JsonXML config = JsonXMLDefault.class.getAnnotation(JsonXML.class); 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(); JAXBContext context = JAXBContext.newInstance(SampleType.class); new JsonXMLBinder().writeArray(SampleType.class, config, context, writer, list); String json = "[{\"sampleType\":{\"element\":\"hello\"}},{\"sampleType\":{\"element\":\"world\"}}]"; Assert.assertEquals(json, writer.toString()); }
@Test public void testWriteArraySampleRootElementWithNull1() throws Exception { JsonXML config = JsonXMLVirtualSampleRootElement.class.getAnnotation(JsonXML.class); List<SampleRootElement> list = new ArrayList<SampleRootElement>(); list.add(new SampleRootElement()); list.get(0).attribute = "hello"; list.add(null); StringWriter writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(SampleRootElement.class); new JsonXMLBinder().writeArray(SampleRootElement.class, config, context, writer, list); String json = "[{\"@attribute\":\"hello\"},null]"; Assert.assertEquals(json, writer.toString()); }
@Test public void testWriteArraySampleRootElementWithNull2() throws Exception { JsonXML config = JsonXMLVirtualSampleRootElement.class.getAnnotation(JsonXML.class); List<SampleRootElement> list = new ArrayList<SampleRootElement>(); list.add(null); list.add(new SampleRootElement()); list.get(1).attribute = "hello"; StringWriter writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(SampleRootElement.class); new JsonXMLBinder().writeArray(SampleRootElement.class, config, context, writer, list); String json = "[null,{\"@attribute\":\"hello\"}]"; Assert.assertEquals(json, writer.toString()); }
@Test public void testWriteArraySampleRootElementWithNull3() throws Exception { JsonXML config = JsonXMLDefault.class.getAnnotation(JsonXML.class); List<SampleRootElement> list = new ArrayList<SampleRootElement>(); list.add(null); StringWriter writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(SampleRootElement.class); new JsonXMLBinder().writeArray(SampleRootElement.class, config, context, writer, list); String json = "[null]"; Assert.assertEquals(json, writer.toString()); }
@Test public void testWriteArrayEmpty1() throws Exception { JsonXML config = JsonXMLDefault.class.getAnnotation(JsonXML.class); StringWriter writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(SampleRootElement.class); new JsonXMLBinder().writeArray(SampleRootElement.class, config, context, writer, new ArrayList<SampleRootElement>()); Assert.assertEquals("[]", writer.toString()); }
@Test public void testWriteArrayEmpty2() throws Exception { JsonXML config = JsonXMLVirtualSampleRootElement.class.getAnnotation(JsonXML.class); StringWriter writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(SampleRootElement.class); new JsonXMLBinder().writeArray(SampleRootElement.class, config, context, writer, new ArrayList<SampleRootElement>()); Assert.assertEquals("[]", writer.toString()); }
@Test public void testWriteArrayNull() throws Exception { JsonXML config = JsonXMLDefault.class.getAnnotation(JsonXML.class); StringWriter writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(SampleType.class); new JsonXMLBinder().writeArray(SampleType.class, config, context, writer, null); Assert.assertEquals("null", writer.toString()); }
@Test public void testWriteArrayNullWithVirtualRoot() throws Exception { JsonXML config = JsonXMLVirtualSampleRootElement.class.getAnnotation(JsonXML.class); StringWriter writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(SampleType.class); new JsonXMLBinder().writeArray(SampleType.class, config, context, writer, null); Assert.assertEquals("null", writer.toString()); }