public static XMLStreamReader createReader( Reader reader, JSONConfiguration config, String rootName, boolean readingList) throws XMLStreamException { Reader nonEmptyReader = ensureNonEmptyReader(reader); switch (config.getNotation()) { case NATURAL: try { final JsonParser rawParser = new JsonFactory().createJsonParser(nonEmptyReader); final JsonParser nonListParser = config.isRootUnwrapping() ? JacksonRootAddingParser.createRootAddingParser(rawParser, rootName) : rawParser; if (!readingList) { return new Jackson2StaxReader(nonListParser, config); } else { return new Jackson2StaxReader( JacksonRootAddingParser.createRootAddingParser( nonListParser, "jsonArrayRootElement"), config); } } catch (Exception ex) { throw new XMLStreamException(ex); } case MAPPED: try { return new JsonXmlStreamReader(nonEmptyReader, rootName, config); } catch (IOException ex) { throw new XMLStreamException(ex); } case MAPPED_JETTISON: try { Configuration jmConfig; if (null == config.getXml2JsonNs()) { jmConfig = new Configuration(); } else { jmConfig = new Configuration(config.getXml2JsonNs()); } return new MappedXMLStreamReader( new JSONObject(new JSONTokener(ReaderWriter.readFromAsString(nonEmptyReader))), new MappedNamespaceConvention(jmConfig)); } catch (Exception ex) { throw new XMLStreamException(ex); } case BADGERFISH: try { return new BadgerFishXMLStreamReader( new JSONObject(new JSONTokener(ReaderWriter.readFromAsString(nonEmptyReader)))); } catch (Exception ex) { throw new XMLStreamException(ex); } } // This should not occur throw new IllegalArgumentException("Unknown JSON config"); }
public static XMLStreamWriter createWriter( Writer writer, JSONConfiguration config, boolean writingList) throws IOException { switch (config.getNotation()) { case NATURAL: final JsonGenerator rawGenerator = new JsonFactory().createJsonGenerator(writer); if (config.isHumanReadableFormatting()) { rawGenerator.useDefaultPrettyPrinter(); } final JsonGenerator bodyGenerator = writingList ? JacksonArrayWrapperGenerator.createArrayWrapperGenerator( rawGenerator, config.isRootUnwrapping() ? 0 : 1) : rawGenerator; if (config.isRootUnwrapping()) { return new Stax2JacksonWriter( JacksonRootStrippingGenerator.createRootStrippingGenerator( bodyGenerator, writingList ? 2 : 1), config); } else { return new Stax2JacksonWriter(bodyGenerator, config); } case MAPPED: return JsonXmlStreamWriter.createWriter(writer, config); case BADGERFISH: return new BadgerFishXMLStreamWriter(writer); case MAPPED_JETTISON: Configuration jmConfig; if (null == config.getXml2JsonNs()) { jmConfig = new Configuration(); } else { jmConfig = new Configuration(config.getXml2JsonNs()); } return new MappedXMLStreamWriter(new MappedNamespaceConvention(jmConfig), writer); default: return null; } }