Skip to content

Commit

Permalink
Update release notes wrt #360; test clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Nov 11, 2020
1 parent 9148d9e commit 4dedfd7
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 36 deletions.
6 changes: 6 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ Jochen Schalanda (joschi@github)
* Reported #318: XMLMapper fails to deserialize null (POJO reference) from blank tag
(2.12.0)

Migwel@github

* Contributed #360: Add a feature to support writing `xsi:nil` attribute for
`null` values
(2.12.0)

Ingo Wiarda (dewarim@github)

* Reported #374: Deserialization fails with `XmlMapper` and
Expand Down
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Project: jackson-dataformat-xml
(reported by Jochen S)
#319: Empty root tag into `List` deserialization bug
(reported by Seatec13@github)
#360: Add a feature to support writing `xsi:nil` attribute for `null` values
(contributed by Migwel@github)
#374: Deserialization fails with `XmlMapper` and `DeserializationFeature.UNWRAP_ROOT_VALUE`
(reported by Ingo W)
#377: `ToXmlGenerator` ignores `Base64Variant` while serializing `byte[]`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.fasterxml.jackson.dataformat.xml.ser;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;

public class CustomSerializerTest extends XmlTestBase
{
@SuppressWarnings("serial")
static class CustomSerializer extends StdScalarSerializer<String>
{
public CustomSerializer() { super(String.class); }

@Override
public void serialize(String value, JsonGenerator jgen,
SerializerProvider provider) throws IOException {
jgen.writeString("custom:"+value);
}
}

// for [dataformat-xml#41]
public void testCustomSerializer() throws Exception
{
SimpleModule module = new SimpleModule();
module.addSerializer(String.class, new CustomSerializer());
final XmlMapper mapper = XmlMapper.builder()
.addModule(module)
.build();
assertEquals("<String>custom:foo</String>", mapper.writeValueAsString("foo"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.fasterxml.jackson.dataformat.xml.ser;

import java.io.IOException;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;

public class NullSerializationTest extends XmlTestBase
{
static class WrapperBean<T>
{
public T value;

public WrapperBean() { }
public WrapperBean(T v) { value = v; }
}

// [dataformat-xml#360]
public void testNil() throws IOException
{
final XmlMapper mapper = XmlMapper.builder()
.configure(ToXmlGenerator.Feature.WRITE_NULLS_AS_XSI_NIL, true)
.build();

// First, map in a general wrapper
assertEquals("<WrapperBean><value xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"/></WrapperBean>",
mapper.writeValueAsString(new WrapperBean<>(null)));

// and then as root -- not sure what it should exactly look like but...
String xml = mapper.writeValueAsString(null);
assertEquals("<null xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"/>", xml);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
import java.io.*;
import java.util.*;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlCData;
Expand Down Expand Up @@ -68,17 +64,6 @@ static class CDataStringArrayBean
public String[] value = {"<some<data\"", "abc"};
}

static class CustomSerializer extends StdScalarSerializer<String>
{
public CustomSerializer() { super(String.class); }

@Override
public void serialize(String value, JsonGenerator jgen,
SerializerProvider provider) throws IOException {
jgen.writeString("custom:"+value);
}
}

static class CustomMap extends LinkedHashMap<String, Integer> { }

/*
Expand All @@ -87,7 +72,7 @@ static class CustomMap extends LinkedHashMap<String, Integer> { }
/**********************************************************
*/

protected XmlMapper _xmlMapper = new XmlMapper();
private final XmlMapper _xmlMapper = new XmlMapper();

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

public void testNil() throws IOException
{
XmlMapper mapper = new XmlMapper();
mapper.configure(ToXmlGenerator.Feature.WRITE_NULLS_AS_XSI_NIL, true);
WrapperBean<String> bean = new WrapperBean<>(null);
// First, map in a general wrapper
String xml = mapper.writeValueAsString(bean);
assertEquals("<WrapperBean><value xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"/></WrapperBean>", xml);
}

@SuppressWarnings("boxing")
public void testMap() throws IOException
{
// First, map in a general wrapper
Expand Down Expand Up @@ -176,15 +150,6 @@ public void testCDataStringArray() throws IOException
xml = removeSjsxpNamespace(xml);
assertEquals("<CDataStringArrayBean><value><value><![CDATA[<some<data\"]]></value><value><![CDATA[abc]]></value></value></CDataStringArrayBean>", xml);
}

// for [dataformat-xml#41]
public void testCustomSerializer() throws Exception
{
JacksonXmlModule module = new JacksonXmlModule();
module.addSerializer(String.class, new CustomSerializer());
XmlMapper xml = new XmlMapper(module);
assertEquals("<String>custom:foo</String>", xml.writeValueAsString("foo"));
}

// manual 'test' to see "what would JAXB do?"
/*
Expand Down

0 comments on commit 4dedfd7

Please sign in to comment.