Skip to content

Commit ab5a167

Browse files
committed
support configureForJackson2
1 parent 1493f8f commit ab5a167

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-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/XmlFactoryBuilder.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import tools.jackson.core.ErrorReportConfiguration;
88
import tools.jackson.core.StreamReadConstraints;
9+
import tools.jackson.core.StreamReadFeature;
910
import tools.jackson.core.StreamWriteConstraints;
1011
import tools.jackson.core.base.DecorableTSFactory.DecorableTSFBuilder;
1112

@@ -222,6 +223,23 @@ public XmlFactoryBuilder configure(XmlWriteFeature f, boolean state) {
222223
return state ? enable(f) : disable(f);
223224
}
224225

226+
/**
227+
* The builder returned uses default settings more closely
228+
* matching the default configs used in Jackson 2.x versions.
229+
* <p>
230+
* This method is still a work in progress and may not yet fully replicate the
231+
* default settings of Jackson 2.x.
232+
* </p>
233+
*/
234+
@Override
235+
public XmlFactoryBuilder configureForJackson2() {
236+
return super.configureForJackson2()
237+
.disable(XmlWriteFeature.WRITE_NULLS_AS_XSI_NIL)
238+
.disable(XmlWriteFeature.UNWRAP_ROOT_OBJECT_NODE)
239+
.disable(XmlWriteFeature.AUTO_DETECT_XSI_TYPE)
240+
.disable(XmlWriteFeature.WRITE_XML_SCHEMA_CONFORMING_FLOATS);
241+
}
242+
225243
// // // Other config
226244

227245
public XmlFactoryBuilder nameForTextElement(String name) {

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,23 @@ 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+
//TODO call super.configureForJackson2() when available - https://github.com/FasterXML/jackson-databind/pull/5004
249+
return 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+
}
254+
238255
/*
239256
/******************************************************************
240257
/* XML specific additional config
@@ -340,6 +357,19 @@ public static XmlMapper.Builder builder(XmlFactory streamFactory) {
340357
return new XmlMapper.Builder(streamFactory);
341358
}
342359

360+
/**
361+
* The builder returned uses default settings more closely
362+
* matching the default configs used in Jackson 2.x versions.
363+
* <p>
364+
* This method is still a work in progress and may not yet fully replicate the
365+
* default settings of Jackson 2.x.
366+
* </p>
367+
*/
368+
public static XmlMapper.Builder builderWithJackson2Defaults() {
369+
return builder(XmlFactory.builderWithJackson2Defaults().build())
370+
.configureForJackson2();
371+
}
372+
343373
@SuppressWarnings("unchecked")
344374
@Override
345375
public XmlMapper.Builder rebuild() {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.ser.ToXmlGenerator;
6+
7+
import javax.xml.stream.XMLOutputFactory;
8+
import javax.xml.stream.XMLStreamWriter;
9+
10+
import java.io.ByteArrayOutputStream;
11+
12+
import static org.junit.jupiter.api.Assertions.assertFalse;
13+
14+
public class XmlMapperTest extends XmlTestUtil
15+
{
16+
17+
@Test
18+
public void testBuilderWithJackson2Defaults() throws Exception
19+
{
20+
XmlMapper mapper = XmlMapper.builderWithJackson2Defaults().build();
21+
assertFalse(mapper.isEnabled(StreamReadFeature.USE_FAST_DOUBLE_PARSER));
22+
assertFalse(mapper.isEnabled(StreamReadFeature.USE_FAST_BIG_NUMBER_PARSER));
23+
24+
XMLOutputFactory outputFactory = mapper.tokenStreamFactory().getXMLOutputFactory();
25+
try (
26+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
27+
ToXmlGenerator gen =
28+
mapper.createGenerator(
29+
outputFactory.createXMLStreamWriter(bos))
30+
) {
31+
assertFalse(gen.isEnabled(XmlWriteFeature.WRITE_NULLS_AS_XSI_NIL));
32+
assertFalse(gen.isEnabled(XmlWriteFeature.UNWRAP_ROOT_OBJECT_NODE));
33+
assertFalse(gen.isEnabled(XmlWriteFeature.AUTO_DETECT_XSI_TYPE));
34+
assertFalse(gen.isEnabled(XmlWriteFeature.WRITE_XML_SCHEMA_CONFORMING_FLOATS));
35+
// need to write something to the generator to avoid exception
36+
final Point p = new Point(1, 2);
37+
mapper.writeValue(gen, p);
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)