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 timestamps fields to null value doesn't work #561

Open
silvestr85 opened this issue Dec 12, 2022 · 4 comments
Open

Comments

@silvestr85
Copy link

Hi.
From version 2.12.0 deserialization timestamps works in other way.
I have field in model:

    @JacksonXmlProperty(isAttribute = true,localName = "timestampDate")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Timestamp timestampDate;

In response i receive something like this:

<PositionsCurrent>
  <PositionsCurrent timestampDate="" />
</PositionsCurrent>

Earlier this assertion worked fine:
Assert.assertNull(response.getData());

On the newest version 2.14 deserialization returns something like this:
Actual :1970-01-01 01:00:00.0

Is it possible to return null again when I try deserialize empty field to Timestamp?

@cowtowncoder
Copy link
Member

@silvestr85 Try enabling FromXmlParser.EMPTY_ELEMENT_AS_NULL.

Handling of empty String to "empty" Timestamp is indeed unfortunate and shouldn't occur.

If nothing else works you could alternatively add/override setTimestamp(Timestamp ts) method and look if underlying epoch value (long) is 0L and if so translate instance into null.

ps. which Timestamp class are we talking about? java.sql.Timestamp?

@silvestr85
Copy link
Author

I was trying this one FromXmlParser.EMPTY_ELEMENT_AS_NULL but it doesn't work for Timestamp.

Yes, I'm using java.sql.Timestamp

For now I have workaround as my own converter:

public class TimestampConverter extends StdConverter<String, Timestamp> {

    @Override
    public Timestamp convert(final String value) {
        if (value == null || value.equals("")) {
            return null;
        }
        else {
            return Timestamp.valueOf(value);
        }
    }
}

@cowtowncoder
Copy link
Member

FWTW, I think this is unfortunately something that happens, as jackson-databind has TimestampDeserializer that indicates that so-called "empty" value is one with instance constructed with timestamp of 0L -- which means January 1st, 1970, UTC (epoch).

So I think work-around indicated makes sense.

@danibs
Copy link

danibs commented Aug 5, 2024

I found the solution 🥳

You must set the behaviour of coercionConfigFor:

XmlMapper mapper = new XmlMapper();
mapper.coercionConfigFor( LogicalType.DateTime ).setCoercion( CoercionInputShape.EmptyString, CoercionAction.AsNull );

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

3 participants