jackson-dataformat-xml icon indicating copy to clipboard operation
jackson-dataformat-xml copied to clipboard

Deserialization: empty String field to null value doesn't work

Open danibs opened this issue 1 year ago • 1 comments

Hi. I'm using <jackson-bom.version>2.17.2</jackson-bom.version> and already resolved my problem with Date using this configuration https://github.com/FasterXML/jackson-dataformat-xml/issues/561#issuecomment-2269479434 .

Now what I want is to convert:

<x><TESTO></TESTO></x>

to the field:

	@JsonProperty( "TESTO" )
	private String testo;

and when the text is empty what I want is to have null in testo field. What I already tried is all the combinations of:

XmlMapper mapper = new XmlMapper();

mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
mapper.coercionConfigFor( LogicalType.Textual ).setCoercion( CoercionInputShape.EmptyString, CoercionAction.AsNull );

I also tried adding:

mapper.coercionConfigDefaults().setCoercion(  CoercionInputShape.EmptyString, CoercionAction.AsNull);

Then I tried to debug but it seams that coercion is not used.

Any idea?

Thanks alot

danibs avatar Sep 02 '24 13:09 danibs

I think String is problematic special type since unlike many other types (like POJOs), it has natural value for empty character date case (empty String). Coercion should work, logically, but does not.

As a work-around, I would suggest just having setter method like so:

  @JsonProperty( "TESTO" )
  public void setTesto(String str) {
     testo = "".equals(str) ? null : str;
  }

to convert empty String to null.

cowtowncoder avatar Oct 29 '24 02:10 cowtowncoder