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

Instant converter working; LocalDate(Time) not #20

Open
patricklucas opened this issue Jun 8, 2017 · 4 comments
Open

Instant converter working; LocalDate(Time) not #20

patricklucas opened this issue Jun 8, 2017 · 4 comments

Comments

@patricklucas
Copy link

The Instant converter is working great for me, but I'm having trouble with the LocalDate and LocalDateTime converters.

private Instant date;
private LocalDateTime date2;
private LocalDate date3;
private Instant date4;

private static Gson gson = Converters.registerAll(new GsonBuilder()).create();

public MyClass(final Instant date) {
    this.date = date;
    this.date2 = LocalDateTime.ofInstant(date, ZoneId.of("UTC"));
    this.date3 = date2.toLocalDate();
    this.date4 = date3.atStartOfDay().toInstant(ZoneOffset.UTC);
}

public String toJSON() {
    return gson.toJson(this);
}

When I call toJSON, the two Instants (date, date4) are serialized properly to ISO timestamps, but date2 and date3 serialize to {"date": {"year", "month", "day"}, "time": {...}} and {"year", "month", "day"} respectively.

Given the relatively simplicity of the implementation of this package, I'm not sure where the error might lie, but I just wanted to inquire here in case you have any ideas.

@gkopff
Copy link
Owner

gkopff commented Jun 8, 2017

I will take a look and see what I can find.

gkopff added a commit that referenced this issue Jun 8, 2017
@gkopff
Copy link
Owner

gkopff commented Jun 8, 2017

@patricklucas Take a look at 8e5b022 - it adds your test class and this test case:

    final Instant instant = Instant.parse("2017-06-08T22:11:28.566Z");

    final Issue20 issue = new Issue20(instant);
    final String expected =
        "{\"date\":\"2017-06-08T22:11:28.566Z\"," +
        "\"date2\":\"2017-06-08T22:11:28.566\"," +
        "\"date3\":\"2017-06-08\"," +
        "\"date4\":\"2017-06-08T00:00:00Z\"}";

    assertThat(issue.toJSON(), is(expected));

The test passes, so I am unable to reproduce the issue you're having.

@gkopff
Copy link
Owner

gkopff commented Jun 8, 2017

Perhaps in your real code you're accidentally using a Gson instance that hasn't had the converters registered.

@patricklucas
Copy link
Author

Thanks. I suspected the same, but double checked the reference and there didn't seem to be anything amiss.

I'll take another look next Wednesday when I again have access to the actual code.

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