Skip to content

Commit 4dedfd7

Browse files
committed
Update release notes wrt #360; test clean up
1 parent 9148d9e commit 4dedfd7

File tree

5 files changed

+78
-36
lines changed

5 files changed

+78
-36
lines changed

release-notes/CREDITS-2.x

+6
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ Jochen Schalanda (joschi@github)
115115
* Reported #318: XMLMapper fails to deserialize null (POJO reference) from blank tag
116116
(2.12.0)
117117

118+
Migwel@github
119+
120+
* Contributed #360: Add a feature to support writing `xsi:nil` attribute for
121+
`null` values
122+
(2.12.0)
123+
118124
Ingo Wiarda (dewarim@github)
119125

120126
* Reported #374: Deserialization fails with `XmlMapper` and

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Project: jackson-dataformat-xml
3535
(reported by Jochen S)
3636
#319: Empty root tag into `List` deserialization bug
3737
(reported by Seatec13@github)
38+
#360: Add a feature to support writing `xsi:nil` attribute for `null` values
39+
(contributed by Migwel@github)
3840
#374: Deserialization fails with `XmlMapper` and `DeserializationFeature.UNWRAP_ROOT_VALUE`
3941
(reported by Ingo W)
4042
#377: `ToXmlGenerator` ignores `Base64Variant` while serializing `byte[]`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fasterxml.jackson.dataformat.xml.ser;
2+
3+
import java.io.IOException;
4+
5+
import com.fasterxml.jackson.core.JsonGenerator;
6+
import com.fasterxml.jackson.databind.SerializerProvider;
7+
import com.fasterxml.jackson.databind.module.SimpleModule;
8+
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
9+
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
10+
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
11+
12+
public class CustomSerializerTest extends XmlTestBase
13+
{
14+
@SuppressWarnings("serial")
15+
static class CustomSerializer extends StdScalarSerializer<String>
16+
{
17+
public CustomSerializer() { super(String.class); }
18+
19+
@Override
20+
public void serialize(String value, JsonGenerator jgen,
21+
SerializerProvider provider) throws IOException {
22+
jgen.writeString("custom:"+value);
23+
}
24+
}
25+
26+
// for [dataformat-xml#41]
27+
public void testCustomSerializer() throws Exception
28+
{
29+
SimpleModule module = new SimpleModule();
30+
module.addSerializer(String.class, new CustomSerializer());
31+
final XmlMapper mapper = XmlMapper.builder()
32+
.addModule(module)
33+
.build();
34+
assertEquals("<String>custom:foo</String>", mapper.writeValueAsString("foo"));
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fasterxml.jackson.dataformat.xml.ser;
2+
3+
import java.io.IOException;
4+
5+
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
6+
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
7+
8+
public class NullSerializationTest extends XmlTestBase
9+
{
10+
static class WrapperBean<T>
11+
{
12+
public T value;
13+
14+
public WrapperBean() { }
15+
public WrapperBean(T v) { value = v; }
16+
}
17+
18+
// [dataformat-xml#360]
19+
public void testNil() throws IOException
20+
{
21+
final XmlMapper mapper = XmlMapper.builder()
22+
.configure(ToXmlGenerator.Feature.WRITE_NULLS_AS_XSI_NIL, true)
23+
.build();
24+
25+
// First, map in a general wrapper
26+
assertEquals("<WrapperBean><value xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"/></WrapperBean>",
27+
mapper.writeValueAsString(new WrapperBean<>(null)));
28+
29+
// and then as root -- not sure what it should exactly look like but...
30+
String xml = mapper.writeValueAsString(null);
31+
assertEquals("<null xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"/>", xml);
32+
}
33+
}

src/test/java/com/fasterxml/jackson/dataformat/xml/ser/TestSerialization.java

+1-36
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
import java.io.*;
44
import java.util.*;
55

6-
import com.fasterxml.jackson.core.JsonGenerator;
7-
import com.fasterxml.jackson.databind.SerializerProvider;
8-
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
9-
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule;
106
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
117
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
128
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlCData;
@@ -68,17 +64,6 @@ static class CDataStringArrayBean
6864
public String[] value = {"<some<data\"", "abc"};
6965
}
7066

71-
static class CustomSerializer extends StdScalarSerializer<String>
72-
{
73-
public CustomSerializer() { super(String.class); }
74-
75-
@Override
76-
public void serialize(String value, JsonGenerator jgen,
77-
SerializerProvider provider) throws IOException {
78-
jgen.writeString("custom:"+value);
79-
}
80-
}
81-
8267
static class CustomMap extends LinkedHashMap<String, Integer> { }
8368

8469
/*
@@ -87,7 +72,7 @@ static class CustomMap extends LinkedHashMap<String, Integer> { }
8772
/**********************************************************
8873
*/
8974

90-
protected XmlMapper _xmlMapper = new XmlMapper();
75+
private final XmlMapper _xmlMapper = new XmlMapper();
9176

9277
public void testSimpleAttribute() throws IOException
9378
{
@@ -111,17 +96,6 @@ public void testSimpleAttrAndElem() throws IOException
11196
assertEquals("<AttrAndElem id=\"42\"><elem>whatever</elem></AttrAndElem>", xml);
11297
}
11398

114-
public void testNil() throws IOException
115-
{
116-
XmlMapper mapper = new XmlMapper();
117-
mapper.configure(ToXmlGenerator.Feature.WRITE_NULLS_AS_XSI_NIL, true);
118-
WrapperBean<String> bean = new WrapperBean<>(null);
119-
// First, map in a general wrapper
120-
String xml = mapper.writeValueAsString(bean);
121-
assertEquals("<WrapperBean><value xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"/></WrapperBean>", xml);
122-
}
123-
124-
@SuppressWarnings("boxing")
12599
public void testMap() throws IOException
126100
{
127101
// First, map in a general wrapper
@@ -176,15 +150,6 @@ public void testCDataStringArray() throws IOException
176150
xml = removeSjsxpNamespace(xml);
177151
assertEquals("<CDataStringArrayBean><value><value><![CDATA[<some<data\"]]></value><value><![CDATA[abc]]></value></value></CDataStringArrayBean>", xml);
178152
}
179-
180-
// for [dataformat-xml#41]
181-
public void testCustomSerializer() throws Exception
182-
{
183-
JacksonXmlModule module = new JacksonXmlModule();
184-
module.addSerializer(String.class, new CustomSerializer());
185-
XmlMapper xml = new XmlMapper(module);
186-
assertEquals("<String>custom:foo</String>", xml.writeValueAsString("foo"));
187-
}
188153

189154
// manual 'test' to see "what would JAXB do?"
190155
/*

0 commit comments

Comments
 (0)