提交 f0ae8bc3 编写于 作者: S Sebastien Deleuze

Support custom XmlFactory in Jackson2ObjectMapperBuilder

Closes gh-22428
上级 ca24fd50
......@@ -614,8 +614,8 @@ public class Jackson2ObjectMapperBuilder {
ObjectMapper mapper;
if (this.createXmlMapper) {
mapper = (this.defaultUseWrapper != null ?
new XmlObjectMapperInitializer().create(this.defaultUseWrapper) :
new XmlObjectMapperInitializer().create());
new XmlObjectMapperInitializer().create(this.defaultUseWrapper, this.factory) :
new XmlObjectMapperInitializer().create(this.factory));
}
else {
mapper = (this.factory != null ? new ObjectMapper(this.factory) : new ObjectMapper());
......@@ -839,14 +839,24 @@ public class Jackson2ObjectMapperBuilder {
private static class XmlObjectMapperInitializer {
public ObjectMapper create() {
return new XmlMapper(StaxUtils.createDefensiveInputFactory());
public ObjectMapper create(@Nullable JsonFactory factory) {
if (factory != null) {
return new XmlMapper((XmlFactory) factory);
}
else {
return new XmlMapper(StaxUtils.createDefensiveInputFactory());
}
}
public ObjectMapper create(boolean defaultUseWrapper) {
public ObjectMapper create(boolean defaultUseWrapper, @Nullable JsonFactory factory) {
JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(defaultUseWrapper);
return new XmlMapper(new XmlFactory(StaxUtils.createDefensiveInputFactory()), module);
if (factory != null) {
return new XmlMapper((XmlFactory) factory, module);
}
else {
return new XmlMapper(new XmlFactory(StaxUtils.createDefensiveInputFactory()), module);
}
}
}
......
......@@ -71,6 +71,7 @@ import com.fasterxml.jackson.databind.ser.std.NumberSerializer;
import com.fasterxml.jackson.databind.type.SimpleType;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import com.fasterxml.jackson.dataformat.smile.SmileFactory;
import com.fasterxml.jackson.dataformat.xml.XmlFactory;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import kotlin.ranges.IntRange;
......@@ -480,6 +481,14 @@ public class Jackson2ObjectMapperBuilderTests {
assertEquals(XmlMapper.class, objectMapper.getClass());
}
@Test // gh-22428
public void xmlMapperAndCustomFactory() {
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.xml().factory(new MyXmlFactory()).build();
assertNotNull(objectMapper);
assertEquals(XmlMapper.class, objectMapper.getClass());
assertEquals(MyXmlFactory.class, objectMapper.getFactory().getClass());
}
@Test
public void createXmlMapper() {
Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json().indentOutput(true);
......@@ -667,4 +676,7 @@ public class Jackson2ObjectMapperBuilderTests {
}
public static class MyXmlFactory extends XmlFactory {
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册