beckchr/staxon

Runtime Exception thrown when writing JSON from JSON Reader that throws XMLStreamException

Closed this issue · 1 comments

------------- Standard Output ---------------
Cannot determine next state


------------- Standard Error -----------------
java.lang.RuntimeException: Cannot determine next state
at de.odysseus.staxon.event.SimpleXMLEventReader.hasNext( at de.odysseus.staxon.event.SimpleXMLEventReader.hasNext(SimpleXMLEventReader.java:128)
SimpleXMLEventReader.java:128)
at de.odysseus.staxon.event.SimpleXMLEventWriter.add(SimpleXMLEventWriter.java:118)

...

Caused by: javax.xml.stream.XMLStreamException: java.io.IOException: Unexpected symbol: COLON
at de.odysseus.staxon.base.AbstractXMLStreamReader.hasNext(AbstractXMLStreamReader.java:362)
at de.odysseus.staxon.event.SimpleXMLEventReader.peek(SimpleXMLEventReader.java:215)
at de.odysseus.staxon.event.SimpleXMLEventReader.hasNext(SimpleXMLEventReader.java:126)

The following unit tests can be added to JsonXMLStreamWriterTest to demonstrate:

@Test
public void testJsonToJsonFormatter() throws Exception {

    String test= "{\"order\" : {\"list\" : {\"item\" : [\"Level1\", \"Level2\", \"Level3\"]}}}";
    String expected = "{\"order\":{\"list\":{\"item\":[\"Level1\",\"Level2\",\"Level3\"]}}}";

    StringReader sr = null;
    StringWriter sw = new StringWriter();

    JsonXMLConfig jsonReaderConfig = new JsonXMLConfigBuilder().multiplePI(false).build();
    JsonXMLConfig jsonWriterConfig = new JsonXMLConfigBuilder().autoArray(true).prettyPrint(false).build();

    try {
        sr = new StringReader(test);
        XMLEventReader jsonReader = new JsonXMLInputFactory(jsonReaderConfig).createXMLEventReader(sr);
        /* Create writer (JSON). */
        XMLEventWriter jsonWriter =  new JsonXMLOutputFactory(jsonWriterConfig).createXMLEventWriter(sw);
        jsonWriter.add(jsonReader);
        jsonWriter.flush();
        String result = sw.toString();
        //System.out.println("result= ");
        //System.out.println(sw.toString());
        jsonReader.close();
        jsonWriter.close();
        Assert.assertEquals(expected, result);
    }
    finally {
        if(sr != null){
            sr.close();
        }
    }
}

// Currently throws a Runtime Exception, but should throw XMLStreamException
@Test//(expected = XMLStreamException.class)
public void testInvalidJson_JsonToJsonFormatter() throws Exception {

    String test= "{\"order\" : {\"list\" : {\"item\" : [\"Level1\", \"Level2\", \"Level3\"]}:}}";
    StringReader sr = null;
    StringWriter sw = new StringWriter();
    JsonXMLConfig jsonReaderConfig = new JsonXMLConfigBuilder().multiplePI(false).build();
    JsonXMLConfig jsonWriterConfig = new JsonXMLConfigBuilder().autoArray(true).prettyPrint(false).build();
    try {
        sr = new StringReader(test);
        XMLEventReader jsonReader = new JsonXMLInputFactory(jsonReaderConfig).createXMLEventReader(sr);
        XMLEventWriter jsonWriter =  new JsonXMLOutputFactory(jsonWriterConfig).createXMLEventWriter(sw);
        jsonWriter.add(jsonReader);
        jsonWriter.flush();
        System.out.println("result= ");
        System.out.println(sw.toString());
        jsonReader.close();
        jsonWriter.close();
    }
    catch(RuntimeException rte){
        System.out.println(rte.getMessage());
        rte.printStackTrace();
        Assert.fail(rte.toString());
    }
    finally {
        if(sr != null){
            sr.close();
        }
    }
}

Fixed in trunk. The problem was that SimpleXMLEventWriter.add(XMLEventReader reader) called reader.hasNext() (from java.util.Iterator) which wraps an exception into RuntimeException. Now using reader.peek() != null.