Search in sources :

Example 6 with Configuration

use of org.codehaus.jettison.mapped.Configuration in project opencast by opencast.

the class Theme method valueOfJson.

/**
 * Reads the theme from the input stream.
 *
 * @param json
 *          the input stream
 * @return the deserialized theme
 * @throws JSONException
 * @throws XMLStreamException
 * @throws JAXBException
 */
public static Theme valueOfJson(InputStream json) throws IOException, JSONException, XMLStreamException, JAXBException {
    // TODO Get this to work, it is currently returning null properties for all properties.
    if (context == null) {
        createJAXBContext();
    }
    BufferedReader streamReader = new BufferedReader(new InputStreamReader(json, "UTF-8"));
    StringBuilder jsonStringBuilder = new StringBuilder();
    String inputStr;
    while ((inputStr = streamReader.readLine()) != null) jsonStringBuilder.append(inputStr);
    JSONObject obj = new JSONObject(jsonStringBuilder.toString());
    Configuration config = new Configuration();
    config.setSupressAtAttributes(true);
    Map<String, String> xmlToJsonNamespaces = new HashMap<String, String>(1);
    xmlToJsonNamespaces.put(IndexObject.INDEX_XML_NAMESPACE, "");
    config.setXmlToJsonNamespaces(xmlToJsonNamespaces);
    MappedNamespaceConvention con = new MappedNamespaceConvention(config);
    XMLStreamReader xmlStreamReader = new MappedXMLStreamReader(obj, con);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    Theme event = (Theme) unmarshaller.unmarshal(xmlStreamReader);
    return event;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) MappedXMLStreamReader(org.codehaus.jettison.mapped.MappedXMLStreamReader) InputStreamReader(java.io.InputStreamReader) Configuration(org.codehaus.jettison.mapped.Configuration) HashMap(java.util.HashMap) MappedXMLStreamReader(org.codehaus.jettison.mapped.MappedXMLStreamReader) JSONObject(org.codehaus.jettison.json.JSONObject) BufferedReader(java.io.BufferedReader) MappedNamespaceConvention(org.codehaus.jettison.mapped.MappedNamespaceConvention) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 7 with Configuration

use of org.codehaus.jettison.mapped.Configuration in project opencast by opencast.

the class Theme method toJSON.

/**
 * Serializes the theme.
 *
 * @return the serialized theme
 */
@Override
public String toJSON() {
    try {
        if (context == null) {
            createJAXBContext();
        }
        Marshaller marshaller = Theme.context.createMarshaller();
        Configuration config = new Configuration();
        config.setSupressAtAttributes(true);
        MappedNamespaceConvention con = new MappedNamespaceConvention(config);
        StringWriter writer = new StringWriter();
        XMLStreamWriter xmlStreamWriter = new MappedXMLStreamWriter(con, writer) {

            @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 {
            }
        };
        marshaller.marshal(this, xmlStreamWriter);
        return writer.toString();
    } catch (JAXBException e) {
        throw new IllegalStateException(e.getLinkedException() != null ? e.getLinkedException() : e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) Configuration(org.codehaus.jettison.mapped.Configuration) StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) MappedXMLStreamWriter(org.codehaus.jettison.mapped.MappedXMLStreamWriter) JAXBException(javax.xml.bind.JAXBException) MappedNamespaceConvention(org.codehaus.jettison.mapped.MappedNamespaceConvention) MappedXMLStreamWriter(org.codehaus.jettison.mapped.MappedXMLStreamWriter)

Example 8 with Configuration

use of org.codehaus.jettison.mapped.Configuration in project opencast by opencast.

the class MediaPackageParser method getAsJSON.

/**
 * Serializes the media package to a JSON string.
 *
 * @param mediaPackage
 *          the media package
 * @return the serialized media package
 */
public static String getAsJSON(MediaPackage mediaPackage) {
    if (mediaPackage == null) {
        throw new IllegalArgumentException("Mediapackage must not be null");
    }
    try {
        Marshaller marshaller = MediaPackageImpl.context.createMarshaller();
        Configuration config = new Configuration();
        config.setSupressAtAttributes(true);
        MappedNamespaceConvention con = new MappedNamespaceConvention(config);
        StringWriter writer = new StringWriter();
        XMLStreamWriter xmlStreamWriter = new MappedXMLStreamWriter(con, writer) {

            @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 {
            }
        };
        marshaller.marshal(mediaPackage, xmlStreamWriter);
        return writer.toString();
    } catch (JAXBException e) {
        throw new IllegalStateException(e.getLinkedException() != null ? e.getLinkedException() : e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) Configuration(org.codehaus.jettison.mapped.Configuration) StringWriter(java.io.StringWriter) MappedXMLStreamWriter(org.codehaus.jettison.mapped.MappedXMLStreamWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) JAXBException(javax.xml.bind.JAXBException) MappedNamespaceConvention(org.codehaus.jettison.mapped.MappedNamespaceConvention) MappedXMLStreamWriter(org.codehaus.jettison.mapped.MappedXMLStreamWriter)

Example 9 with Configuration

use of org.codehaus.jettison.mapped.Configuration in project cxf by apache.

the class JSONProvider method createWriter.

protected XMLStreamWriter createWriter(Object actualObject, Class<?> actualClass, Type genericType, String enc, OutputStream os, boolean isCollection) throws Exception {
    if (BADGER_FISH_CONVENTION.equals(convention)) {
        return JSONUtils.createBadgerFishWriter(os, enc);
    }
    boolean dropElementsInXmlStreamProp = getBooleanJsonProperty(DROP_ELEMENT_IN_XML_PROPERTY, dropElementsInXmlStream);
    boolean dropRootNeeded = getBooleanJsonProperty(DROP_ROOT_CONTEXT_PROPERTY, dropRootElement);
    boolean dropRootInXmlNeeded = dropRootNeeded && dropElementsInXmlStreamProp;
    QName qname = actualClass == Document.class ? org.apache.cxf.helpers.DOMUtils.getElementQName(((Document) actualObject).getDocumentElement()) : getQName(actualClass, genericType, actualObject);
    if (qname != null && ignoreNamespaces && (isCollection || dropRootInXmlNeeded)) {
        qname = new QName(qname.getLocalPart());
    }
    Configuration config = JSONUtils.createConfiguration(namespaceMap, writeXsiType && !ignoreNamespaces, attributesToElements, typeConverter);
    if (namespaceSeparator != null) {
        config.setJsonNamespaceSeparator(namespaceSeparator);
    }
    if (!dropElementsInXmlStreamProp && super.outDropElements != null) {
        config.setIgnoredElements(outDropElements);
    }
    if (!writeNullAsString) {
        config.setWriteNullAsString(writeNullAsString);
    }
    boolean ignoreEmpty = getBooleanJsonProperty(IGNORE_EMPTY_JSON_ARRAY_VALUES_PROPERTY, ignoreEmptyArrayValues);
    if (ignoreEmpty) {
        config.setIgnoreEmptyArrayValues(ignoreEmpty);
    }
    if (escapeForwardSlashesAlways) {
        config.setEscapeForwardSlashAlways(escapeForwardSlashesAlways);
    }
    boolean dropRootInJsonStream = dropRootNeeded && !dropElementsInXmlStreamProp;
    if (dropRootInJsonStream) {
        config.setDropRootElement(true);
    }
    List<String> theArrayKeys = getArrayKeys();
    boolean rootIsArray = isRootArray(theArrayKeys);
    if (ignoreNamespaces && rootIsArray && (theArrayKeys == null || dropRootInJsonStream)) {
        if (theArrayKeys == null) {
            theArrayKeys = new LinkedList<String>();
        } else if (dropRootInJsonStream) {
            theArrayKeys = new LinkedList<String>(theArrayKeys);
        }
        if (qname != null) {
            theArrayKeys.add(qname.getLocalPart());
        }
    }
    XMLStreamWriter writer = JSONUtils.createStreamWriter(os, qname, writeXsiType && !ignoreNamespaces, config, rootIsArray, theArrayKeys, isCollection || dropRootInXmlNeeded, enc);
    writer = JSONUtils.createIgnoreMixedContentWriterIfNeeded(writer, ignoreMixedContent);
    writer = JSONUtils.createIgnoreNsWriterIfNeeded(writer, ignoreNamespaces, !writeXsiType);
    return createTransformWriterIfNeeded(writer, os, dropElementsInXmlStreamProp);
}
Also used : Configuration(org.codehaus.jettison.mapped.Configuration) QName(javax.xml.namespace.QName) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Document(org.w3c.dom.Document) LinkedList(java.util.LinkedList)

Example 10 with Configuration

use of org.codehaus.jettison.mapped.Configuration in project cxf by apache.

the class JSONUtils method createConfiguration.

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;
}
Also used : Configuration(org.codehaus.jettison.mapped.Configuration)

Aggregations

Configuration (org.codehaus.jettison.mapped.Configuration)14 MappedNamespaceConvention (org.codehaus.jettison.mapped.MappedNamespaceConvention)10 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)7 MappedXMLStreamWriter (org.codehaus.jettison.mapped.MappedXMLStreamWriter)6 StringWriter (java.io.StringWriter)5 JAXBException (javax.xml.bind.JAXBException)5 Marshaller (javax.xml.bind.Marshaller)5 BufferedReader (java.io.BufferedReader)4 InputStreamReader (java.io.InputStreamReader)4 HashMap (java.util.HashMap)4 Unmarshaller (javax.xml.bind.Unmarshaller)4 XMLStreamReader (javax.xml.stream.XMLStreamReader)4 JSONObject (org.codehaus.jettison.json.JSONObject)4 MappedXMLStreamReader (org.codehaus.jettison.mapped.MappedXMLStreamReader)4 LinkedList (java.util.LinkedList)2 QName (javax.xml.namespace.QName)2 Document (org.w3c.dom.Document)2 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 BadgerFishXMLInputFactory (org.codehaus.jettison.badgerfish.BadgerFishXMLInputFactory)1 BadgerFishXMLStreamWriter (org.codehaus.jettison.badgerfish.BadgerFishXMLStreamWriter)1