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

Q on Serializing java.time dates w/o nano parts #312

Open
mebigfatguy opened this issue Jun 6, 2024 · 2 comments
Open

Q on Serializing java.time dates w/o nano parts #312

mebigfatguy opened this issue Jun 6, 2024 · 2 comments

Comments

@mebigfatguy
Copy link

Hi folks, i'm trying to get interop between joda and java.time while i try to switch over to java.time. So currently we have a bajillion pojos that hold joda DateTimes that get serialized to ISO strings to the browser.

I'd like to start incrementally switching that over to LocalDateTime for some of them. When i add the JavaTimeModule,

    m_mapper.registerModule(new JavaTimeModule());

i see that the nano part of the date is written to the date string as well, which is not what our current Joda serializer does, so

I'm asking to see how to configure the JavaTimeModule to not write the nano time, from what i can tell it's just picks a static one, but perhaps i missed something??

    return DateTimeFormatter.ISO_LOCAL_DATE_TIME;
@mebigfatguy
Copy link
Author

ok, i futz around some, and came up with this. Is this rational?

public class Tester {

    public static final DateTimeFormatter ISO_OPC_LOCAL_DATE_TIME;
    static {
        // @formatter:off
        ISO_OPC_LOCAL_DATE_TIME = new DateTimeFormatterBuilder()
                .parseCaseInsensitive()
                .append(DateTimeFormatter.ISO_LOCAL_DATE)
                .appendLiteral('T')
                .appendValue(HOUR_OF_DAY, 2)
                .appendLiteral(':')
                .appendValue(MINUTE_OF_HOUR, 2)
                .appendLiteral(':')
                .appendValue(SECOND_OF_MINUTE, 2)
                .toFormatter();
        // @formatter:on
    }

    @Test
    public void test() throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JavaTimeModule().addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(ISO_OPC_LOCAL_DATE_TIME)));
        String now = mapper.writeValueAsString(LocalDateTime.now());
        System.out.println(now);

    }
}

@cowtowncoder
Copy link
Member

That should work.

One thing is that a recommended approach would be to add "config override" for type LocalDateTime.class instead of constructing deserializer explicitly.
From tests, it's something like:

        ObjectMapper m = mapperBuilder().withConfigOverride(LocalDateTime.class,
                cfg -> cfg.setFormat(JsonFormat.Value.forPattern("yyyy-MM-dd'X'HH:mm")))
            .build();

and is basically more robust way, in case LocalDateTimeSerializer implementation changed.

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