Search in sources :

Example 11 with Configuration

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

the class Event method valueOfJson.

/**
 * Reads the recording event from the input stream.
 *
 * @param json
 *          the input stream
 * @return the deserialized recording event
 * @throws JSONException
 * @throws XMLStreamException
 * @throws JAXBException
 */
public static Event 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();
    Event event = (Event) 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 12 with Configuration

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

the class Event method toJSON.

/**
 * Serializes the recording event.
 *
 * @return the serialized recording event
 */
@Override
public String toJSON() {
    try {
        if (context == null) {
            createJAXBContext();
        }
        Marshaller marshaller = Event.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 13 with Configuration

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

the class Group method valueOfJson.

/**
 * Reads the group from the input stream.
 *
 * @param json
 *          the input stream
 * @return the deserialized group
 * @throws JSONException
 * @throws XMLStreamException
 * @throws JAXBException
 */
public static Group 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(INDEX_XML_NAMESPACE, "");
    config.setXmlToJsonNamespaces(xmlToJsonNamespaces);
    MappedNamespaceConvention con = new MappedNamespaceConvention(config);
    XMLStreamReader xmlStreamReader = new MappedXMLStreamReader(obj, con);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    Group event = (Group) unmarshaller.unmarshal(xmlStreamReader);
    return event;
}
Also used : JaxbGroup(org.opencastproject.security.api.JaxbGroup) 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 14 with Configuration

use of org.codehaus.jettison.mapped.Configuration in project entando-core by entando.

the class JSONProvider method createWriter.

@Override
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);
    }
    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()) : this.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 = ApsJSONUtils.createStreamWriter(os, qname, writeXsiType && !ignoreNamespaces, config, rootIsArray, theArrayKeys, isCollection || dropRootInXmlNeeded, enc);
    writer = ApsJSONUtils.createIgnoreMixedContentWriterIfNeeded(writer, ignoreMixedContent);
    writer = ApsJSONUtils.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)

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