/** * Construct a JettisonMappedXmlDriver with configuration. This constructor has been added * by special request of Jettison users to support JSON generated by older Jettison * versions. if the driver is setup to ignore the XStream hints for JSON arrays, there is * neither support from XStream's side nor are there any tests to ensure this mode. * * @param config the Jettison configuration * @param useSerializeAsArray flag to use XStream's hints for collections and arrays * @since 1.4 */ public JettisonMappedXmlDriver(final Configuration config, final boolean useSerializeAsArray) { mof = new MappedXMLOutputFactory(config); mif = new MappedXMLInputFactory(config); convention = new MappedNamespaceConvention(config); this.useSerializeAsArray = useSerializeAsArray; }
public void startNode(String name, Class clazz) { XMLStreamWriter out = getXMLStreamWriter(); if (clazz != null && out instanceof AbstractXMLStreamWriter) { if (Collection.class.isAssignableFrom(clazz) || Map.class.isAssignableFrom(clazz) || clazz.isArray()) { QName qname = getQNameMap().getQName(encodeNode(name)); String prefix = qname.getPrefix(); String uri = qname.getNamespaceURI(); String key = convention.createKey(prefix, uri, qname.getLocalPart()); if (!((AbstractXMLStreamWriter)out).getSerializedAsArrays().contains(key)) { // Typo is in the API of Jettison ... ((AbstractXMLStreamWriter)out).seriliazeAsArray(key); } } } startNode(name); } }
public void writeAttribute(String prefix, String ns, String local, String value) throws XMLStreamException { String key = convention.isElement(prefix, ns, local) ? convention.createKey(prefix, ns, local) : convention.createAttributeKey(prefix, ns, local); JSONPropertyString prop = new JSONPropertyString(key, null); prop.addText(value); current = current.withProperty(prop, false); }
public static XMLStreamWriter createStreamWriter(OutputStream os, QName qname, boolean writeXsiType, Configuration config, boolean serializeAsArray, List<String> arrayKeys, boolean dropRootElement, String enc) throws Exception { MappedNamespaceConvention convention = new MappedNamespaceConvention(config); AbstractXMLStreamWriter xsw = new CDataMappedXMLStreamWriter(convention, new OutputStreamWriter(os, enc)); if (serializeAsArray) { if (arrayKeys != null) { for (String key : arrayKeys) { xsw.serializeAsArray(key); } } else if (qname != null) { String key = convention.createKey(qname.getPrefix(), qname.getNamespaceURI(), qname.getLocalPart()); xsw.serializeAsArray(key); } } XMLStreamWriter writer = !writeXsiType || dropRootElement ? new IgnoreContentJettisonWriter(xsw, writeXsiType, dropRootElement) : xsw; return writer; }
public void writeAttribute(String p, String ns, String local, String value) throws XMLStreamException { if (convention.isElement(p, ns, local)) { writeStartElement(p, local, ns); writeCharacters(value); writeEndElement(); return; } String key = convention.createAttributeKey(p, ns, local); try { if (current instanceof JSONArray) { JSONArray array = ((JSONArray) current); current = array.get(array.length() - 1); } makeCurrentJSONObject(); if (current instanceof JSONObject) { Object o = ((JSONObject) current).opt(key); if (o == null) { ((JSONObject) current).put(key, value); } } } catch (JSONException e) { throw new XMLStreamException(e); } }
@Override public boolean isElement(String p, String ns, String local) { if (p == null) p = ""; if (ns == null) ns = ""; return super.isElement(p, ns, local); } }
for (Iterator<?> itr = attributesAsElements.iterator(); itr.hasNext();) { QName q = (QName) itr.next(); jsonAttributesAsElements.add(createAttributeKey(q.getPrefix(), q.getNamespaceURI(), q.getLocalPart()));
public static XMLStreamWriter createStreamWriter(OutputStream os, QName qname, boolean writeXsiType, Configuration config, boolean serializeAsArray, List<String> arrayKeys, boolean dropRootElement) throws Exception { MappedNamespaceConvention convention = new MappedNamespaceConvention(config); AbstractXMLStreamWriter xsw = new CDataMappedXMLStreamWriter(convention, new OutputStreamWriter(os, UTF8)); if (serializeAsArray) { if (arrayKeys != null) { for (String key : arrayKeys) { xsw.serializeAsArray(key); } } else if (qname != null) { String key = convention.createKey(qname.getPrefix(), qname.getNamespaceURI(), qname.getLocalPart()); xsw.serializeAsArray(key); } } XMLStreamWriter writer = !writeXsiType || dropRootElement ? new IgnoreContentJettisonWriter(xsw, writeXsiType, dropRootElement) : xsw; return writer; }
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 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 void writeStartElement(String prefix, String local, String ns) throws XMLStreamException { String parentKey = current.getTreeKey(); stack.push(current); String key = convention.createKey(prefix, ns, local); current = new JSONPropertyString(key, parentKey); }
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"); }
private static String getKey(MappedNamespaceConvention convention, QName qname) throws Exception { return convention.createKey(qname.getPrefix(), qname.getNamespaceURI(), qname.getLocalPart()); }
public MappedXMLOutputFactory(Configuration config) { this.convention = new MappedNamespaceConvention(config); }
private static String getKey(MappedNamespaceConvention convention, QName qname) throws Exception { return convention.createKey(qname.getPrefix(), qname.getNamespaceURI(), qname.getLocalPart()); }
public MappedXMLStreamReader(JSONObject obj) throws JSONException, XMLStreamException { this(obj, new MappedNamespaceConvention()); }
private static String getKey(MappedNamespaceConvention convention, QName qname) throws Exception { return convention.createKey(qname.getPrefix(), qname.getNamespaceURI(), qname.getLocalPart()); }
public MappedXMLInputFactory(Configuration config) { this.convention = new MappedNamespaceConvention(config); }
@Override public void startNode(final String name, final Class<?> clazz) { final XMLStreamWriter out = getXMLStreamWriter(); String key = ""; if (clazz != null && out instanceof AbstractXMLStreamWriter) { if (Collection.class.isAssignableFrom(clazz) || Map.class.isAssignableFrom(clazz) || clazz.isArray()) { final QName qname = getQNameMap().getQName(encodeNode(name)); final String prefix = qname.getPrefix(); final String uri = qname.getNamespaceURI(); key = convention.createKey(prefix, uri, qname.getLocalPart()); } } stack.push(key); super.startNode(name); }
public static XMLStreamReader createReader(final Reader reader, final JSONConfiguration config, final String rootName, final Class<?> expectedType, final JAXBContext jaxbContext, final boolean readingList) throws XMLStreamException { Reader nonEmptyReader = ensureNonEmptyReader(reader); switch (config.getNotation()) { case NATURAL: case MAPPED: return JsonXmlStreamReader.create(nonEmptyReader, config, rootName, expectedType, jaxbContext, readingList); 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"); }