AutoArrayTarget implements JsonStreamTarget { @Override public void close() throws IOException { while (!events.isEmpty()) { events.pollFirst().write(delegate); } delegate.close(); } AutoArrayTarget(JsonStreamTarget delegate); @Override void name(String name); @Override void value(Object value); @Override void startObject(); @Override void endObject(); @Override void startArray(); @Override void endArray(); @Override void close(); @Override void flush();  }
@Test public void testTextContent() throws Exception { StringWriter result = new StringWriter(); XMLStreamWriter writer = createXmlStreamWriter(result); writer.writeStartDocument(); writer.writeStartElement("alice"); writer.writeCharacters("bob"); writer.writeEndElement(); writer.writeEndDocument(); writer.close(); Assert.assertEquals("{\"alice\":\"bob\"}", result.toString()); }
@Test public void testArray() throws Exception { StringWriter result = new StringWriter(); XMLStreamWriter writer = createXmlStreamWriter(result); writer.writeStartDocument(); writer.writeStartElement("alice"); writer.writeStartElement("bob"); writer.writeCharacters("charlie"); writer.writeEndElement(); writer.writeStartElement("bob"); writer.writeCharacters("david"); writer.writeEndElement(); writer.writeEndElement(); writer.writeEndDocument(); writer.close(); Assert.assertEquals("{\"alice\":{\"bob\":[\"charlie\",\"david\"]}}", result.toString()); }
@Test public void testArray2() throws Exception { StringWriter result = new StringWriter(); XMLStreamWriter writer = createXmlStreamWriter(result); writer.writeStartDocument(); writer.writeStartElement("alice"); writer.writeStartElement("bob"); writer.writeCharacters("charlie"); writer.writeEndElement(); writer.writeStartElement("bob"); writer.writeCharacters("david"); writer.writeEndElement(); writer.writeEmptyElement("edgar"); writer.writeEndElement(); writer.writeEndDocument(); writer.close(); Assert.assertEquals("{\"alice\":{\"bob\":[\"charlie\",\"david\"],\"edgar\":null}}", result.toString()); }
@Test public void testArray3() throws Exception { StringWriter result = new StringWriter(); JsonXMLStreamWriter writer = createXmlStreamWriter(result); writer.writeStartDocument(); writer.writeStartElement("alice"); writer.writeStartArray("bob"); writer.writeStartElement("bob"); writer.writeCharacters("charlie"); writer.writeEndElement(); writer.writeEndArray(); writer.writeEndElement(); writer.writeEndDocument(); writer.close(); Assert.assertEquals("{\"alice\":{\"bob\":[\"charlie\"]}}", result.toString()); }
@Test public void testNested() throws Exception { StringWriter result = new StringWriter(); XMLStreamWriter writer = createXmlStreamWriter(result); writer.writeStartDocument(); writer.writeStartElement("alice"); writer.writeStartElement("bob"); writer.writeCharacters("charlie"); writer.writeEndElement(); writer.writeStartElement("david"); writer.writeCharacters("edgar"); writer.writeEndElement(); writer.writeEndElement(); writer.writeEndDocument(); writer.close(); Assert.assertEquals("{\"alice\":{\"bob\":\"charlie\",\"david\":\"edgar\"}}", result.toString()); }
@Test public void testAttributes() throws Exception { StringWriter result = new StringWriter(); XMLStreamWriter writer = createXmlStreamWriter(result); writer.writeStartDocument(); writer.writeStartElement("alice"); writer.writeAttribute("charlie", "david"); writer.writeCharacters("bob"); writer.writeEndElement(); writer.writeEndDocument(); writer.close(); Assert.assertEquals("{\"alice\":{\"@charlie\":\"david\",\"$\":\"bob\"}}", result.toString()); }
@Test public void testNamespaces() throws Exception { StringWriter result = new StringWriter(); XMLStreamWriter writer = createXmlStreamWriter(result); writer.setDefaultNamespace("http: writer.writeStartDocument(); writer.writeStartElement("alice"); writer.writeDefaultNamespace("http: writer.writeCharacters("bob"); writer.writeEndElement(); writer.writeEndDocument(); writer.close(); Assert.assertEquals("{\"alice\":{\"@xmlns\":\"http: }
@Test public void testEmpty() throws Exception { StringWriter result = new StringWriter(); XMLStreamWriter writer = createXmlStreamWriter(result); writer.writeStartDocument(); writer.writeStartElement("alice"); writer.writeEndElement(); writer.writeEndDocument(); writer.close(); Assert.assertEquals("{\"alice\":null}", result.toString()); }