Skip to content

Commit 8a04feb

Browse files
authored
Support configureForJackson2() with XmlMapper (#731)
1 parent 2aa0d1c commit 8a04feb

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

src/main/java/tools/jackson/dataformat/xml/XmlFactory.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,18 @@ public static XmlFactoryBuilder builder() {
167167
return new XmlFactoryBuilder();
168168
}
169169

170+
/**
171+
* The builder returned uses default settings more closely
172+
* matching the default configs used in Jackson 2.x versions.
173+
* <p>
174+
* This method is still a work in progress and may not yet fully replicate the
175+
* default settings of Jackson 2.x.
176+
* </p>
177+
*/
178+
public static XmlFactoryBuilder builderWithJackson2Defaults() {
179+
return builder().configureForJackson2();
180+
}
181+
170182
/**
171183
* Note: compared to base implementation by {@link TokenStreamFactory},
172184
* here the copy will actually share underlying XML input and

src/main/java/tools/jackson/dataformat/xml/XmlMapper.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,24 @@ public Builder configure(XmlWriteFeature feature, boolean state)
235235
return this;
236236
}
237237

238+
/**
239+
* The builder returned uses default settings more closely
240+
* matching the default configs used in Jackson 2.x versions.
241+
* <p>
242+
* This method is still a work in progress and may not yet fully replicate the
243+
* default settings of Jackson 2.x.
244+
* </p>
245+
*/
246+
@Override
247+
public Builder configureForJackson2() {
248+
return super.configureForJackson2()
249+
.disable(XmlWriteFeature.WRITE_NULLS_AS_XSI_NIL)
250+
.disable(XmlWriteFeature.UNWRAP_ROOT_OBJECT_NODE)
251+
.disable(XmlWriteFeature.AUTO_DETECT_XSI_TYPE)
252+
.disable(XmlWriteFeature.WRITE_XML_SCHEMA_CONFORMING_FLOATS)
253+
.disable(XmlReadFeature.AUTO_DETECT_XSI_TYPE);
254+
}
255+
238256
/*
239257
/******************************************************************
240258
/* XML specific additional config
@@ -340,6 +358,19 @@ public static XmlMapper.Builder builder(XmlFactory streamFactory) {
340358
return new XmlMapper.Builder(streamFactory);
341359
}
342360

361+
/**
362+
* The builder returned uses default settings more closely
363+
* matching the default configs used in Jackson 2.x versions.
364+
* <p>
365+
* This method is still a work in progress and may not yet fully replicate the
366+
* default settings of Jackson 2.x.
367+
* </p>
368+
*/
369+
public static XmlMapper.Builder builderWithJackson2Defaults() {
370+
return builder(XmlFactory.builderWithJackson2Defaults().build())
371+
.configureForJackson2();
372+
}
373+
343374
@SuppressWarnings("unchecked")
344375
@Override
345376
public XmlMapper.Builder rebuild() {

src/main/java/tools/jackson/dataformat/xml/deser/FromXmlParser.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import tools.jackson.core.io.NumberInput;
2020
import tools.jackson.core.util.ByteArrayBuilder;
2121
import tools.jackson.core.util.JacksonFeatureSet;
22+
import tools.jackson.dataformat.xml.XmlWriteFeature;
2223
import tools.jackson.dataformat.xml.util.CaseInsensitiveNameSet;
2324
import tools.jackson.dataformat.xml.util.StaxUtil;
2425

@@ -283,6 +284,10 @@ public void addVirtualWrapping(Set<String> namesToWrap0, boolean caseInsensitive
283284
_streamReadContext.setNamesToWrap(namesToWrap);
284285
}
285286

287+
public final boolean isEnabled(XmlReadFeature f) {
288+
return (_formatFeatures & f.getMask()) != 0;
289+
}
290+
286291
/*
287292
/**********************************************************************
288293
/* JsonParser impl, closing etc
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package tools.jackson.dataformat.xml;
2+
3+
import org.junit.jupiter.api.Test;
4+
import tools.jackson.core.StreamReadFeature;
5+
import tools.jackson.dataformat.xml.deser.FromXmlParser;
6+
import tools.jackson.dataformat.xml.ser.ToXmlGenerator;
7+
8+
import javax.xml.stream.XMLInputFactory;
9+
import javax.xml.stream.XMLOutputFactory;
10+
11+
import java.io.ByteArrayInputStream;
12+
import java.io.ByteArrayOutputStream;
13+
import java.nio.charset.StandardCharsets;
14+
15+
import static org.junit.jupiter.api.Assertions.assertFalse;
16+
17+
public class XmlMapperTest extends XmlTestUtil
18+
{
19+
20+
@Test
21+
public void testBuilderWithJackson2Defaults() throws Exception
22+
{
23+
XmlMapper mapper = XmlMapper.builderWithJackson2Defaults().build();
24+
assertFalse(mapper.isEnabled(StreamReadFeature.USE_FAST_DOUBLE_PARSER));
25+
assertFalse(mapper.isEnabled(StreamReadFeature.USE_FAST_BIG_NUMBER_PARSER));
26+
27+
XMLOutputFactory outputFactory = mapper.tokenStreamFactory().getXMLOutputFactory();
28+
try (
29+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
30+
ToXmlGenerator gen =
31+
mapper.createGenerator(
32+
outputFactory.createXMLStreamWriter(bos))
33+
) {
34+
assertFalse(gen.isEnabled(XmlWriteFeature.WRITE_NULLS_AS_XSI_NIL));
35+
assertFalse(gen.isEnabled(XmlWriteFeature.UNWRAP_ROOT_OBJECT_NODE));
36+
assertFalse(gen.isEnabled(XmlWriteFeature.AUTO_DETECT_XSI_TYPE));
37+
assertFalse(gen.isEnabled(XmlWriteFeature.WRITE_XML_SCHEMA_CONFORMING_FLOATS));
38+
// need to write something to the generator to avoid exception
39+
final Point p = new Point(1, 2);
40+
mapper.writeValue(gen, p);
41+
}
42+
43+
final byte[] xml = "<root/>".getBytes(StandardCharsets.UTF_8);
44+
XMLInputFactory inputFactory = mapper.tokenStreamFactory().getXMLInputFactory();
45+
try (
46+
ByteArrayInputStream bis = new ByteArrayInputStream(xml);
47+
FromXmlParser parser =
48+
mapper.createParser(
49+
inputFactory.createXMLStreamReader(bis))
50+
) {
51+
assertFalse(parser.isEnabled(XmlReadFeature.AUTO_DETECT_XSI_TYPE));
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)