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;
}
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);
}
}
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);
}
}
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);
}
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;
}
Aggregations