-
-
Notifications
You must be signed in to change notification settings - Fork 234
Description
Using jackson-data-format-xml Version 2.10.2
I am using a custom serializer for an pojo to allow conversion to json or xml. It will pass null values for String and Number fields as opposed to "" and 0 into the serializer.
For json everything works as intended and the json would look like this example
{
field1: null,
field2: "value"
}
The output must also be available in XML, and that uses ToXmlGenerator for serializing the data. Where a String field is null, it will fail in woodstox BaseStreamWriter with a NullPointerException, as it assumes the String passed is never a null.
A snippet of the stacktrace shows the path it went through
java.lang.NullPointerException: null
at com.ctc.wstx.sw.BaseStreamWriter.writeCharacters(BaseStreamWriter.java:458)
at com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.writeString(ToXmlGenerator.java:622)
at com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.writeStringField(ToXmlGenerator.java:460)
at com.fis.core.json.JsonGeneratorPojoFieldVisitor.lambda$2(JsonGeneratorPojoFieldVisitor.java:170)
In ToXmlGenerator the writeString(String text)
method does not check for a null String. Looking at the other methods eg writeNumber(BigInteger value)
there is this code to check for a null
if (value == null) {
writeNull();
return;
}
I couldn't find any reason that the writeString(String)
shouldn't also check for a null value. I checked in version 3.0 and there is no changes for it. There may well be a valid explanation of why it shouldn't check for a null. For me I found a workaround for this in that I check in my serializer that if the string is null to instead call writeNullField(String fieldName)
. Some developers may not have this capability, and if it can instead call the writeNull()
it would remove the potential for NPE.
Can the check for null String be added to the method in ToXmlGenerator.writeString(String)
?