Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
danibs opened this issue Sep 2, 2024 · 1 comment
Open

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

danibs opened this issue Sep 2, 2024 · 1 comment

Comments

@danibs
Copy link

danibs commented Sep 2, 2024

Hi.
I'm using <jackson-bom.version>2.17.2</jackson-bom.version> and already resolved my problem with Date using this configuration #561 (comment) .

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 danibs changed the title Deserialization empty String fields to null value doesn't work Deserialization: empty String field to null value doesn't work Sep 2, 2024
@cowtowncoder
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants