JsonXMLStreamReader extends AbstractXMLStreamReader<JsonXMLStreamReader.ScopeInfo> { @Override public void close() throws XMLStreamException { super.close(); try { source.close(); } catch (IOException e) { throw new XMLStreamException(e); } } JsonXMLStreamReader(JsonStreamSource source, boolean multiplePI, char namespaceSeparator);  JsonXMLStreamReader(JsonStreamSource source, boolean multiplePI, char namespaceSeparator, Map<String, String> namespaceMappings);  JsonXMLStreamReader(JsonStreamSource source, boolean multiplePI, char namespaceSeparator, Map<String, String> namespaceMappings, String textProperty, String attributePrefix); boolean hasNumber(); Number getNumber(); boolean hasBoolean(); Boolean getBoolean(); @Override void close();  }
@Test public void testTextContent() throws Exception { String input = "{\"alice\":\"bob\"}"; XMLStreamReader reader = new JsonXMLInputFactory().createXMLStreamReader(new StringReader(input)); verify(reader, XMLStreamConstants.START_DOCUMENT, null, null); reader.next(); verify(reader, XMLStreamConstants.START_ELEMENT, "alice", null); reader.next(); verify(reader, XMLStreamConstants.CHARACTERS, null, "bob"); reader.next(); verify(reader, XMLStreamConstants.END_ELEMENT, "alice", null); reader.next(); verify(reader, XMLStreamConstants.END_DOCUMENT, null, null); reader.close(); }
@Test public void testNested() throws Exception { String input = "{\"alice\":{\"bob\":\"charlie\",\"david\":\"edgar\"}}"; XMLStreamReader reader = new JsonXMLInputFactory().createXMLStreamReader(new StringReader(input)); verify(reader, XMLStreamConstants.START_DOCUMENT, null, null); reader.next(); verify(reader, XMLStreamConstants.START_ELEMENT, "alice", null); reader.next(); verify(reader, XMLStreamConstants.START_ELEMENT, "bob", null); reader.next(); verify(reader, XMLStreamConstants.CHARACTERS, null, "charlie"); reader.next(); verify(reader, XMLStreamConstants.END_ELEMENT, "bob", null); reader.next(); verify(reader, XMLStreamConstants.START_ELEMENT, "david", null); reader.next(); verify(reader, XMLStreamConstants.CHARACTERS, null, "edgar"); reader.next(); verify(reader, XMLStreamConstants.END_ELEMENT, "david", null); reader.next(); verify(reader, XMLStreamConstants.END_ELEMENT, "alice", null); reader.next(); verify(reader, XMLStreamConstants.END_DOCUMENT, null, null); reader.close(); }
@Test public void testArray() throws Exception { String input = "{\"alice\":{\"bob\":[\"charlie\",\"david\"]}}"; XMLStreamReader reader = new JsonXMLInputFactory().createXMLStreamReader(new StringReader(input)); verify(reader, XMLStreamConstants.START_DOCUMENT, null, null); reader.next(); verify(reader, XMLStreamConstants.START_ELEMENT, "alice", null); reader.next(); verify(reader, XMLStreamConstants.PROCESSING_INSTRUCTION, null, null); Assert.assertEquals(JsonXMLStreamConstants.MULTIPLE_PI_TARGET, reader.getPITarget()); Assert.assertEquals("bob", reader.getPIData()); reader.next(); verify(reader, XMLStreamConstants.START_ELEMENT, "bob", null); reader.next(); verify(reader, XMLStreamConstants.CHARACTERS, null, "charlie"); reader.next(); verify(reader, XMLStreamConstants.END_ELEMENT, "bob", null); reader.next(); verify(reader, XMLStreamConstants.START_ELEMENT, "bob", null); reader.next(); verify(reader, XMLStreamConstants.CHARACTERS, null, "david"); reader.next(); verify(reader, XMLStreamConstants.END_ELEMENT, "bob", null); reader.next(); verify(reader, XMLStreamConstants.END_ELEMENT, "alice", null); reader.next(); verify(reader, XMLStreamConstants.END_DOCUMENT, null, null); reader.close(); }
@Test public void testAttributes() throws Exception { String input = "{\"alice\":{\"@charlie\":\"david\",\"$\":\"bob\"}}"; XMLStreamReader reader = new JsonXMLInputFactory().createXMLStreamReader(new StringReader(input)); verify(reader, XMLStreamConstants.START_DOCUMENT, null, null); reader.next(); verify(reader, XMLStreamConstants.START_ELEMENT, "alice", null); Assert.assertEquals(1, reader.getAttributeCount()); Assert.assertEquals("david", reader.getAttributeValue(null, "charlie")); Assert.assertEquals("david", reader.getAttributeValue(XMLConstants.NULL_NS_URI, "charlie")); reader.next(); verify(reader, XMLStreamConstants.CHARACTERS, null, "bob"); reader.next(); verify(reader, XMLStreamConstants.END_ELEMENT, "alice", null); reader.next(); verify(reader, XMLStreamConstants.END_DOCUMENT, null, null); reader.close(); }
@Test public void testNamespaces() throws Exception { String input = "{\"alice\":{\"@xmlns\":\"http: XMLStreamReader reader = new JsonXMLInputFactory().createXMLStreamReader(new StringReader(input)); verify(reader, XMLStreamConstants.START_DOCUMENT, null, null); reader.next(); verify(reader, XMLStreamConstants.START_ELEMENT, "alice", null); Assert.assertEquals("http: Assert.assertEquals(0, reader.getAttributeCount()); reader.next(); verify(reader, XMLStreamConstants.CHARACTERS, null, "bob"); reader.next(); verify(reader, XMLStreamConstants.END_ELEMENT, "alice", null); Assert.assertEquals("http: reader.next(); verify(reader, XMLStreamConstants.END_DOCUMENT, null, null); reader.close(); }
@Test public void testNamespaceMappings() throws Exception { String input = "{\"foo:alice\":\"bob\"}"; JsonXMLConfig config = new JsonXMLConfigBuilder().namespaceMapping("foo", "http: XMLStreamReader reader = new JsonXMLInputFactory(config).createXMLStreamReader(new StringReader(input)); verify(reader, XMLStreamConstants.START_DOCUMENT, null, null); reader.next(); verify(reader, XMLStreamConstants.START_ELEMENT, "alice", null); Assert.assertEquals("http: Assert.assertEquals(0, reader.getAttributeCount()); reader.next(); verify(reader, XMLStreamConstants.CHARACTERS, null, "bob"); reader.next(); verify(reader, XMLStreamConstants.END_ELEMENT, "alice", null); Assert.assertEquals("http: reader.next(); verify(reader, XMLStreamConstants.END_DOCUMENT, null, null); reader.close(); }
@Test public void testNamespacesBadgerfish() throws Exception { String input = "{\"alice\":{\"@xmlns\":{\"$\":\"http: XMLStreamReader reader = new JsonXMLInputFactory().createXMLStreamReader(new StringReader(input)); verify(reader, XMLStreamConstants.START_DOCUMENT, null, null); reader.next(); verify(reader, XMLStreamConstants.START_ELEMENT, "alice", null); Assert.assertEquals("http: Assert.assertEquals("http: Assert.assertEquals(0, reader.getAttributeCount()); reader.next(); verify(reader, XMLStreamConstants.CHARACTERS, null, "bob"); reader.next(); verify(reader, XMLStreamConstants.END_ELEMENT, "alice", null); Assert.assertEquals("http: Assert.assertEquals("http: reader.next(); verify(reader, XMLStreamConstants.END_DOCUMENT, null, null); reader.close(); }
@Test public void testRootArray() throws Exception { String input = "{\"alice\":[\"bob\",\"bob\"]}"; XMLStreamReader reader = new JsonXMLInputFactory().createXMLStreamReader(new StringReader(input)); verify(reader, XMLStreamConstants.START_DOCUMENT, null, null); reader.next(); verify(reader, XMLStreamConstants.PROCESSING_INSTRUCTION, null, null); Assert.assertEquals(JsonXMLStreamConstants.MULTIPLE_PI_TARGET, reader.getPITarget()); Assert.assertEquals("alice", reader.getPIData()); reader.next(); verify(reader, XMLStreamConstants.START_ELEMENT, "alice", null); reader.next(); verify(reader, XMLStreamConstants.CHARACTERS, null, "bob"); reader.next(); verify(reader, XMLStreamConstants.END_ELEMENT, "alice", null); reader.next(); verify(reader, XMLStreamConstants.START_ELEMENT, "alice", null); reader.next(); verify(reader, XMLStreamConstants.CHARACTERS, null, "bob"); reader.next(); verify(reader, XMLStreamConstants.END_ELEMENT, "alice", null); reader.next(); verify(reader, XMLStreamConstants.END_DOCUMENT, null, null); reader.close(); }
@Test public void testRootArrayWithVirtualRoot() throws Exception { String input = "[\"bob\",\"bob\"]"; JsonXMLInputFactory factory = new JsonXMLInputFactory(); factory.setProperty(JsonXMLInputFactory.PROP_VIRTUAL_ROOT, new QName("alice")); XMLStreamReader reader = factory.createXMLStreamReader(new StringReader(input)); verify(reader, XMLStreamConstants.START_DOCUMENT, null, null); reader.next(); verify(reader, XMLStreamConstants.PROCESSING_INSTRUCTION, null, null); Assert.assertEquals(JsonXMLStreamConstants.MULTIPLE_PI_TARGET, reader.getPITarget()); Assert.assertEquals("alice", reader.getPIData()); reader.next(); verify(reader, XMLStreamConstants.START_ELEMENT, "alice", null); reader.next(); verify(reader, XMLStreamConstants.CHARACTERS, null, "bob"); reader.next(); verify(reader, XMLStreamConstants.END_ELEMENT, "alice", null); reader.next(); verify(reader, XMLStreamConstants.START_ELEMENT, "alice", null); reader.next(); verify(reader, XMLStreamConstants.CHARACTERS, null, "bob"); reader.next(); verify(reader, XMLStreamConstants.END_ELEMENT, "alice", null); reader.next(); verify(reader, XMLStreamConstants.END_DOCUMENT, null, null); reader.close(); }
@Test public void testSimpleValueArray() throws Exception { String input = "[\"edgar\",\"david\"]"; XMLStreamReader reader = new JsonXMLInputFactory().createXMLStreamReader(new StringReader(input)); verify(reader, XMLStreamConstants.PROCESSING_INSTRUCTION, null, null); Assert.assertEquals(JsonXMLStreamConstants.MULTIPLE_PI_TARGET, reader.getPITarget()); Assert.assertNull(reader.getPIData()); reader.next(); verify(reader, XMLStreamConstants.CHARACTERS, null, "edgar"); reader.next(); verify(reader, XMLStreamConstants.CHARACTERS, null, "david"); Assert.assertFalse(reader.hasNext()); reader.close(); }
@Test public void testDocumentArray() throws Exception { String input = "[{\"alice\":\"bob\"},{\"alice\":\"bob\"}]"; XMLStreamReader reader = new JsonXMLInputFactory().createXMLStreamReader(new StringReader(input)); verify(reader, XMLStreamConstants.PROCESSING_INSTRUCTION, null, null); Assert.assertEquals(JsonXMLStreamConstants.MULTIPLE_PI_TARGET, reader.getPITarget()); Assert.assertNull(reader.getPIData()); reader.next(); verify(reader, XMLStreamConstants.START_DOCUMENT, null, null); reader.next(); verify(reader, XMLStreamConstants.START_ELEMENT, "alice", null); reader.next(); verify(reader, XMLStreamConstants.CHARACTERS, null, "bob"); reader.next(); verify(reader, XMLStreamConstants.END_ELEMENT, "alice", null); reader.next(); verify(reader, XMLStreamConstants.END_DOCUMENT, null, null); reader.next(); verify(reader, XMLStreamConstants.START_DOCUMENT, null, null); reader.next(); verify(reader, XMLStreamConstants.START_ELEMENT, "alice", null); reader.next(); verify(reader, XMLStreamConstants.CHARACTERS, null, "bob"); reader.next(); verify(reader, XMLStreamConstants.END_ELEMENT, "alice", null); reader.next(); verify(reader, XMLStreamConstants.END_DOCUMENT, null, null); Assert.assertFalse(reader.hasNext()); reader.close(); }
@Test public void testNull() throws Exception { String input = "{\"alice\" : null}"; JsonXMLStreamReader reader = new JsonXMLInputFactory().createXMLStreamReader(new StringReader(input)); verify(reader, XMLStreamConstants.START_DOCUMENT, null, null); reader.next(); verify(reader, XMLStreamConstants.START_ELEMENT, "alice", null); reader.next(); verify(reader, XMLStreamConstants.END_ELEMENT, "alice", null); reader.next(); verify(reader, XMLStreamConstants.END_DOCUMENT, null, null); reader.close(); }
@Test public void testTextProperty() throws Exception { String input = "{\"alice\":{\"@charlie\":\"david\",\"@\":\"bob\"}}"; XMLStreamReader reader = new JsonXMLInputFactory(new JsonXMLConfigBuilder().textProperty("@").build()).createXMLStreamReader(new StringReader(input)); verify(reader, XMLStreamConstants.START_DOCUMENT, null, null); reader.next(); verify(reader, XMLStreamConstants.START_ELEMENT, "alice", null); Assert.assertEquals(1, reader.getAttributeCount()); Assert.assertEquals("david", reader.getAttributeValue(null, "charlie")); Assert.assertEquals("david", reader.getAttributeValue(XMLConstants.NULL_NS_URI, "charlie")); reader.next(); verify(reader, XMLStreamConstants.CHARACTERS, null, "bob"); reader.next(); verify(reader, XMLStreamConstants.END_ELEMENT, "alice", null); reader.next(); verify(reader, XMLStreamConstants.END_DOCUMENT, null, null); reader.close(); }
@Test public void testAttributePrefix() throws Exception { String input = "{\"alice\":{\"$charlie\":\"david\",\"$\":\"bob\"}}"; XMLStreamReader reader = new JsonXMLInputFactory(new JsonXMLConfigBuilder().attributePrefix("$").build()).createXMLStreamReader(new StringReader(input)); verify(reader, XMLStreamConstants.START_DOCUMENT, null, null); reader.next(); verify(reader, XMLStreamConstants.START_ELEMENT, "alice", null); Assert.assertEquals(1, reader.getAttributeCount()); Assert.assertEquals("david", reader.getAttributeValue(null, "charlie")); Assert.assertEquals("david", reader.getAttributeValue(XMLConstants.NULL_NS_URI, "charlie")); reader.next(); verify(reader, XMLStreamConstants.CHARACTERS, null, "bob"); reader.next(); verify(reader, XMLStreamConstants.END_ELEMENT, "alice", null); reader.next(); verify(reader, XMLStreamConstants.END_DOCUMENT, null, null); reader.close(); }