public void writeEndDocument() throws XMLStreamException { if (!stack.isEmpty()) throw new XMLStreamException("Missing some closing tags."); // We know the root is a JSONPropertyObject so this cast is safe writeJSONObject((JSONObject)current.getValue()); try { writer.flush(); } catch (IOException e) { throw new XMLStreamException(e); } }