com.fasterxml.jackson.dataformat.xml.ser.XmlSerializerProvider Maven / Gradle / Ivy
package com.fasterxml.jackson.dataformat.xml.ser;
import java.io.IOException;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.PropertyName;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.ser.SerializerFactory;
import com.fasterxml.jackson.databind.ser.DefaultSerializerProvider;
import com.fasterxml.jackson.databind.util.TokenBuffer;
import com.fasterxml.jackson.dataformat.xml.util.StaxUtil;
import com.fasterxml.jackson.dataformat.xml.util.TypeUtil;
import com.fasterxml.jackson.dataformat.xml.util.XmlRootNameLookup;
/**
* We need to override some parts of
* {@link com.fasterxml.jackson.databind.SerializerProvider}
* implementation to handle oddities of XML output, like "extra" root element.
*/
public class XmlSerializerProvider extends DefaultSerializerProvider
{
// As of 2.7
private static final long serialVersionUID = 1L;
/**
* If all we get to serialize is a null, there's no way to figure out
* expected root name; so let's just default to something like "<null>"...
*/
protected final static QName ROOT_NAME_FOR_NULL = new QName("null");
protected final XmlRootNameLookup _rootNameLookup;
public XmlSerializerProvider(XmlRootNameLookup rootNames)
{
super();
_rootNameLookup = rootNames;
}
public XmlSerializerProvider(XmlSerializerProvider src,
SerializationConfig config, SerializerFactory f)
{
super(src, config, f);
_rootNameLookup = src._rootNameLookup;
}
/**
* @since 2.8.9
*/
protected XmlSerializerProvider(XmlSerializerProvider src) {
super(src);
// 21-May-2018, tatu: As per [dataformat-xml#282], should NOT really copy
// root name lookup as that may link back to diff version, configuration
_rootNameLookup = new XmlRootNameLookup();
}
/*
/**********************************************************************
/* Overridden methods
/**********************************************************************
*/
@Override
public DefaultSerializerProvider copy() {
return new XmlSerializerProvider(this);
}
@Override
public DefaultSerializerProvider createInstance(SerializationConfig config,
SerializerFactory jsf) {
return new XmlSerializerProvider(this, config, jsf);
}
@SuppressWarnings("resource")
@Override
public void serializeValue(JsonGenerator gen, Object value) throws IOException
{
if (value == null) {
_serializeXmlNull(gen);
return;
}
final Class> cls = value.getClass();
final boolean asArray;
final ToXmlGenerator xgen = _asXmlGenerator(gen);
if (xgen == null) { // called by convertValue()
asArray = false;
} else {
QName rootName = _rootNameFromConfig();
if (rootName == null) {
rootName = _rootNameLookup.findRootName(cls, _config);
}
_initWithRootName(xgen, rootName);
asArray = TypeUtil.isIndexedType(cls);
if (asArray) {
_startRootArray(xgen, rootName);
}
}
// From super-class implementation
final JsonSerializer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy