/** * Construct a JettisonMappedXmlDriver. */ public JettisonMappedXmlDriver() { this(new Configuration()); }
public MappedNamespaceConvention(Configuration config) { super(); this.xnsToJns = config.getXmlToJsonNamespaces(); this.attributesAsElements = config.getAttributesAsElements(); this.supressAtAttributes = config.isSupressAtAttributes(); this.ignoreNamespaces = config.isIgnoreNamespaces(); this.dropRootElement = config.isDropRootElement(); this.attributeKey = config.getAttributeKey(); this.primitiveArrayKeys = config.getPrimitiveArrayKeys(); this.ignoredElements = config.getIgnoredElements(); this.ignoreEmptyArrayValues = config.isIgnoreEmptyArrayValues(); this.escapeForwardSlashAlways = config.isEscapeForwardSlashAlways(); this.jsonNamespaceSeparator = config.getJsonNamespaceSeparator(); for (Iterator<Map.Entry<Object, Object>> itr = xnsToJns.entrySet().iterator(); itr.hasNext();) { Map.Entry<?, ?> entry = itr.next(); this.readNullAsString = config.isReadNullAsString(); this.writeNullAsString = config.isWriteNullAsString(); typeConverter = config.getTypeConverter(); if (!writeNullAsString && typeConverter != null) { typeConverter = new NullStringConverter(typeConverter);
public static Configuration createConfiguration(ConcurrentHashMap<String, String> namespaceMap, boolean writeXsiType, boolean attributesAsElements, TypeConverter converter) { if (writeXsiType) { namespaceMap.putIfAbsent(XSI_URI, XSI_PREFIX); } Configuration c = new Configuration(namespaceMap); c.setSupressAtAttributes(attributesAsElements); if (converter != null) { c.setTypeConverter(converter); } return c; }
public static XMLStreamReader createStreamReader(InputStream is, boolean readXsiType, ConcurrentHashMap<String, String> namespaceMap, String namespaceSeparator, List<String> primitiveArrayKeys, DocumentDepthProperties depthProps, String enc) throws Exception { if (readXsiType) { namespaceMap.putIfAbsent(XSI_URI, XSI_PREFIX); } Configuration conf = new Configuration(namespaceMap); if (namespaceSeparator != null) { conf.setJsonNamespaceSeparator(namespaceSeparator); } if (primitiveArrayKeys != null) { conf.setPrimitiveArrayKeys( new HashSet<>(primitiveArrayKeys)); } XMLInputFactory factory = depthProps != null ? new JettisonMappedReaderFactory(conf, depthProps) : new MappedXMLInputFactory(conf); return new JettisonReader(namespaceMap, factory.createXMLStreamReader(is, enc)); }
/** * {@inheritDoc} */ @Override protected XMLStreamWriter createWriter(Object actualObject, Class<?> actualClass, Type genericType, String enc, OutputStream os, boolean isCollection) throws Exception { Configuration c = new Configuration(NAMESPACE_MAP); c.setSupressAtAttributes(true); MappedNamespaceConvention convention = new MappedNamespaceConvention(c); return new MappedXMLStreamWriter(convention, new OutputStreamWriter(os, UTF8)) { @Override public void writeStartElement(String prefix, String local, String uri) throws XMLStreamException { super.writeStartElement("", local, ""); } @Override public void writeStartElement(String uri, String local) throws XMLStreamException { super.writeStartElement("", local, ""); } @Override public void setPrefix(String pfx, String uri) throws XMLStreamException { } @Override public void setDefaultNamespace(String uri) throws XMLStreamException { } }; } }
Configuration configuration = new Configuration(); configuration.setIgnoreNamespaces(true); xmlInputFactory = new MappedXMLInputFactory(configuration);
typeConverter); if (namespaceSeparator != null) { config.setJsonNamespaceSeparator(namespaceSeparator); config.setIgnoredElements(outDropElements); config.setWriteNullAsString(writeNullAsString); config.setIgnoreEmptyArrayValues(ignoreEmpty); config.setEscapeForwardSlashAlways(escapeForwardSlashesAlways); config.setDropRootElement(true);
@SuppressWarnings(value={"unchecked"}) private String convertXmlToJson(String xmlToConvert) throws ParseException { if (xmlToConvert == null) { throw new ParseException("Unable to convert XML to JSON", 0); } try { String xmlWithoutBreaks = removeLineBreaks(xmlToConvert); byte[] xmlByteArray = xmlWithoutBreaks.getBytes(); Configuration config = new Configuration(); if (null != namespaces && !namespaces.isEmpty()) { config.getXmlToJsonNamespaces().putAll(namespaces); } ByteArrayInputStream source = new ByteArrayInputStream(xmlByteArray); AbstractXMLOutputFactory writerFactory = new MappedXMLOutputFactory(config); ByteArrayOutputStream output = serialise(source, writerFactory); return new String(output.toByteArray()); } catch (IOException e) { ParseException ex = new ParseException("Unable to convert XML to JSON", 0); ex.setStackTrace(e.getStackTrace()); throw ex; } }
private static String convertToJSON(Element rootElement) throws Exception { Configuration config = new Configuration(); config.setTypeConverter(new SimpleConverter()); MappedNamespaceConvention con = new MappedNamespaceConvention(config); StringWriter strWriter = new StringWriter(); AbstractXMLStreamWriter w = new MappedXMLStreamWriter(con, strWriter); w.writeStartDocument(); converToJSON(rootElement, w); w.writeEndDocument(); w.close(); strWriter.close(); String jsonString = strWriter.toString(); StringWriter wr = new StringWriter(); StringEscapeUtils.unescapeJavaScript(wr, jsonString); String result = wr.toString(); return result; }
public MappedNamespaceConvention(Configuration config) { super(); this.xnsToJns = config.getXmlToJsonNamespaces(); this.attributesAsElements = config.getAttributesAsElements(); for (Iterator itr = xnsToJns.entrySet().iterator(); itr.hasNext();) { Map.Entry entry = (Map.Entry) itr.next(); jnsToXns.put(entry.getValue(), entry.getKey()); } jsonAttributesAsElements = new ArrayList(); if (attributesAsElements != null) { for (Iterator itr = attributesAsElements.iterator(); itr.hasNext();) { QName q = (QName) itr.next(); jsonAttributesAsElements.add(createAttributeKey(q.getPrefix(), q.getNamespaceURI(), q.getLocalPart())); } } }
public static Configuration createConfiguration(ConcurrentHashMap<String, String> namespaceMap, boolean writeXsiType, boolean attributesAsElements, TypeConverter converter) { if (writeXsiType) { namespaceMap.putIfAbsent(XSI_URI, XSI_PREFIX); } Configuration c = new Configuration(namespaceMap); c.setSupressAtAttributes(attributesAsElements); if (converter != null) { c.setTypeConverter(converter); } return c; }
public static XMLStreamReader createStreamReader(InputStream is, boolean readXsiType, ConcurrentHashMap<String, String> namespaceMap, String namespaceSeparator, List<String> primitiveArrayKeys, DocumentDepthProperties depthProps, String enc) throws Exception { if (readXsiType) { namespaceMap.putIfAbsent(XSI_URI, XSI_PREFIX); } Configuration conf = new Configuration(namespaceMap); if (namespaceSeparator != null) { conf.setJsonNamespaceSeparator(namespaceSeparator); } if (primitiveArrayKeys != null) { conf.setPrimitiveArrayKeys( new HashSet<>(primitiveArrayKeys)); } XMLInputFactory factory = depthProps != null ? new JettisonMappedReaderFactory(conf, depthProps) : new MappedXMLInputFactory(conf); return new JettisonReader(namespaceMap, factory.createXMLStreamReader(is, enc)); }
Marshaller marshaller = MediaPackageImpl.context.createMarshaller(); Configuration config = new Configuration(); config.setSupressAtAttributes(true); MappedNamespaceConvention con = new MappedNamespaceConvention(config); StringWriter writer = new StringWriter();
Configuration configuration = new Configuration(); configuration.setIgnoreNamespaces(true); xmlOutputFactory = new MappedXMLOutputFactory(configuration);
typeConverter); if (namespaceSeparator != null) { config.setJsonNamespaceSeparator(namespaceSeparator); config.setIgnoredElements(outDropElements); config.setWriteNullAsString(writeNullAsString); config.setIgnoreEmptyArrayValues(ignoreEmpty); config.setEscapeForwardSlashAlways(escapeForwardSlashesAlways); config.setDropRootElement(true);
public static XMLStreamWriter createWriter(final Writer writer, final JettisonConfig config) throws IOException { switch (config.getNotation()) { case BADGERFISH: return new BadgerFishXMLStreamWriter(writer); case MAPPED_JETTISON: Configuration jmConfig; if (null == config.getXml2JsonNs()) { jmConfig = new Configuration(); } else { jmConfig = new Configuration(config.getXml2JsonNs()); } final MappedXMLStreamWriter result = new MappedXMLStreamWriter(new MappedNamespaceConvention(jmConfig), writer); for (String array : config.getArrayElements()) { result.serializeAsArray(array); } return result; default: return null; } }
public static Configuration createConfiguration(ConcurrentHashMap<String, String> namespaceMap, boolean writeXsiType, boolean attributesAsElements, TypeConverter converter) { if (writeXsiType) { namespaceMap.putIfAbsent(XSI_URI, XSI_PREFIX); } Configuration c = new Configuration(namespaceMap); c.setSupressAtAttributes(attributesAsElements); if (converter != null) { c.setTypeConverter(converter); } return c; }
public static XMLStreamReader createStreamReader(InputStream is, boolean readXsiType, ConcurrentHashMap<String, String> namespaceMap, String namespaceSeparator, List<String> primitiveArrayKeys, DocumentDepthProperties depthProps) throws Exception { if (readXsiType) { namespaceMap.putIfAbsent(XSI_URI, XSI_PREFIX); } Configuration conf = new Configuration(namespaceMap); if (namespaceSeparator != null) { conf.setJsonNamespaceSeparator(namespaceSeparator); } if (primitiveArrayKeys != null) { conf.setPrimitiveArrayKeys( new HashSet<String>(primitiveArrayKeys)); } XMLInputFactory factory = depthProps != null ? new JettisonMappedReaderFactory(conf, depthProps) : new MappedXMLInputFactory(conf); return new JettisonReader(namespaceMap, factory.createXMLStreamReader(is)); }
typeConverter); if (namespaceSeparator != null) { config.setJsonNamespaceSeparator(namespaceSeparator); config.setIgnoredElements(outDropElements); config.setWriteNullAsString(writeNullAsString); config.setIgnoreEmptyArrayValues(ignoreEmpty); config.setEscapeForwardSlashAlways(escapeForwardSlashesAlways); config.setDropRootElement(true);
public static XMLStreamReader createReader(final Reader reader, final JettisonConfig config) throws XMLStreamException { Reader nonEmptyReader = ensureNonEmptyReader(reader); switch (config.getNotation()) { 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"); }