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

Add support for ISO8601 to java.time.Instant serialization/deserialization #1417

Merged
merged 26 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
eb338ef
add queryParams variant for post(), patch() and delete()
gmckerrell Jul 1, 2020
f09a3ba
remove trailing spaces
gmckerrell Jul 1, 2020
6e5af9f
add query request parameters to POST, PATCH and DELETE controllers
gmckerrell Jul 1, 2020
41ad709
Update JsonApiEndpoint.java
gmckerrell Jul 2, 2020
7bc2ac7
Update JsonApiEndpoint to pass queryParams for POST, PATCH and DELETE
gmckerrell Jul 2, 2020
de776c4
shorten line
gmckerrell Jul 2, 2020
237bfc3
Merge branch 'master' into master
aklish Jul 2, 2020
71bf53d
Merge pull request #2 from yahoo/master
gmckerrell Jul 3, 2020
c4d04aa
Create ISO8601InstantSerdes.java (#3)
gmckerrell Jul 6, 2020
b9b2138
Update ElideSettingsBuilder.java
gmckerrell Jul 6, 2020
2a37235
Update ISO8601InstantSerdeTest.java
gmckerrell Jul 6, 2020
d6d6681
Update ISO8601InstantSerde
gmckerrell Jul 6, 2020
9a5fa03
Update ISO8601InstantSerdeTest.java
gmckerrell Jul 6, 2020
fda8bd3
remove trailing whitespace
gmckerrell Jul 6, 2020
c679f1b
remove redundant import
gmckerrell Jul 6, 2020
9812cc3
try changing class name to avoid travis failure...
gmckerrell Jul 6, 2020
881b74c
Update InstantSerdeTest.java
gmckerrell Jul 6, 2020
d37fb0b
Update ElideSettingsBuilder.java
gmckerrell Jul 6, 2020
65ae3c7
Update ElideSettingsBuilder.java
gmckerrell Jul 6, 2020
e12ba8c
change to jupiter test framework
gmckerrell Jul 6, 2020
4b23369
Rename InstantSerde to InstantSerde.java
gmckerrell Jul 6, 2020
61e0a9d
auto-register InstantSerde (#5)
gmckerrell Jul 6, 2020
5adfd35
switch to using ISO_OFFSET_DATE_TIME to avoid JDK-8 bug
gmckerrell Jul 6, 2020
305b693
fix typo
gmckerrell Jul 6, 2020
2abada9
fix serialization
gmckerrell Jul 6, 2020
16487cd
Merge branch 'master' into master
aklish Jul 7, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2020, Yahoo Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package com.yahoo.elide.utils.coerce.converters;

import java.time.Instant;
import java.time.format.DateTimeFormatter;

/**
* Convert an Instant to/from an ISO-8601 string representation.
*
* Uses the semantics of {@link java.time.format.DateTimeFormatter#ISO_INSTANT}
*/
@ElideTypeConverter(type = Instant.class, name = "Instant")
public class InstantSerde implements Serde<String, Instant> {
@Override
public Instant deserialize(final String value) {
return Instant.from(
// NB. ideally we would use ISO_INSTANT here but there is a bug in JDK-8 that
// means that parsing an ISO offset time doesn't work :-(
// https://bugs.openjdk.java.net/browse/JDK-8166138
DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(value)
);
}

@Override
public String serialize(final Instant value) {
return DateTimeFormatter.ISO_INSTANT.format(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2018, Yahoo Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package com.yahoo.elide.utils.coerce.converters;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

import java.time.Instant;

public class InstantSerdeTest {
private final Serde<String, Instant> serde = new InstantSerde();

@Test
public void canDeserializeUtcIsoString() {
final Instant instant = serde.deserialize("2019-06-01T09:42:55Z");

assertEquals(1559382175, instant.getEpochSecond());
assertEquals(0, instant.getNano());
}

@Test
public void canDeserializeOffsetIsoString() {
final Instant instant = serde.deserialize("2019-06-01T10:42:55+01:00");

assertEquals(1559382175, instant.getEpochSecond());
assertEquals(0, instant.getNano());
}

@Test
public void canDeserializeSubSecondPrecisionUtcIsoString() {
final Instant instant = serde.deserialize("2019-06-01T09:42:55.123Z");

assertEquals(1559382175, instant.getEpochSecond());
assertEquals(123000000, instant.getNano());
}

@Test
public void canSerialize() {
assertEquals(
"2019-06-01T09:42:55Z",
serde.serialize(Instant.ofEpochSecond(1559382175))
);
}

@Test
public void canSerializeSubSecondPrecision() {
assertEquals(
"2019-06-01T09:42:55.123Z",
serde.serialize(Instant.ofEpochMilli(1559382175123L))
);
}
}