diff options
author | Morten Tokle <mortent@verizonmedia.com> | 2019-11-12 09:14:32 +0100 |
---|---|---|
committer | Morten Tokle <mortent@verizonmedia.com> | 2019-11-12 09:14:32 +0100 |
commit | 53b7c8f8fecfdeed2f598cb0c59a33a082ce2d65 (patch) | |
tree | c121590bb22be92a24c476a03355e558f66f872f /athenz-identity-provider-service | |
parent | e0abfbad493274bda6dac079bc78fc47d400100b (diff) |
Deserialize instant using jackson
Diffstat (limited to 'athenz-identity-provider-service')
2 files changed, 14 insertions, 10 deletions
diff --git a/athenz-identity-provider-service/src/main/java/com/yahoo/vespa/hosted/ca/restapi/InstanceSerializer.java b/athenz-identity-provider-service/src/main/java/com/yahoo/vespa/hosted/ca/restapi/InstanceSerializer.java index c989f85b167..d164555dc3e 100644 --- a/athenz-identity-provider-service/src/main/java/com/yahoo/vespa/hosted/ca/restapi/InstanceSerializer.java +++ b/athenz-identity-provider-service/src/main/java/com/yahoo/vespa/hosted/ca/restapi/InstanceSerializer.java @@ -1,6 +1,8 @@ // Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.hosted.ca.restapi; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.yahoo.security.Pkcs10CsrUtils; import com.yahoo.security.X509CertificateUtils; import com.yahoo.slime.ArrayTraverser; @@ -16,9 +18,8 @@ import com.yahoo.vespa.hosted.ca.instance.InstanceIdentity; import com.yahoo.vespa.hosted.ca.instance.InstanceRefresh; import com.yahoo.vespa.hosted.ca.instance.InstanceRegistration; -import java.math.BigDecimal; +import java.io.IOException; import java.time.Instant; -import java.time.temporal.ChronoUnit; import java.util.HashSet; import java.util.Set; @@ -47,6 +48,11 @@ public class InstanceSerializer { private static final String IDD_IPADDRESSES_FIELD = "ip-addresses"; private static final String IDD_IDENTITY_TYPE_FIELD = "identity-type"; + private static final ObjectMapper objectMapper = new ObjectMapper(); + static { + objectMapper.registerModule(new JavaTimeModule()); + } + private InstanceSerializer() {} public static InstanceRegistration registrationFromSlime(Slime slime) { @@ -96,10 +102,11 @@ public class InstanceSerializer { } private static Instant getJsr310Instant(double v) { - var val = new BigDecimal(v); - var seconds = val.longValue(); - var nanos = val.subtract(new BigDecimal(seconds)).scaleByPowerOfTen(9).longValue(); - return Instant.ofEpochSecond(seconds, nanos).truncatedTo(ChronoUnit.MILLIS); + try { + return objectMapper.readValue(Double.toString(v), Instant.class); + } catch (IOException e) { + throw new RuntimeException(e); + } } private static Cursor requireField(String fieldName, Cursor root) { diff --git a/athenz-identity-provider-service/src/test/java/com/yahoo/vespa/hosted/ca/restapi/InstanceSerializerTest.java b/athenz-identity-provider-service/src/test/java/com/yahoo/vespa/hosted/ca/restapi/InstanceSerializerTest.java index edc8bec2370..f04e155aa2c 100644 --- a/athenz-identity-provider-service/src/test/java/com/yahoo/vespa/hosted/ca/restapi/InstanceSerializerTest.java +++ b/athenz-identity-provider-service/src/test/java/com/yahoo/vespa/hosted/ca/restapi/InstanceSerializerTest.java @@ -21,7 +21,6 @@ import java.io.IOException; import java.io.UncheckedIOException; import java.nio.charset.StandardCharsets; import java.time.Instant; -import java.time.temporal.ChronoUnit; import java.util.Collections; import java.util.Optional; @@ -44,8 +43,7 @@ public class InstanceSerializerTest { 0, "configserverhostname", "instancehostname", - Instant.now().truncatedTo(ChronoUnit.MILLIS), - // Instant.ofEpochSecond(1572000079), + Instant.now(), Collections.emptySet(), IdentityType.NODE); @@ -95,5 +93,4 @@ public class InstanceSerializerTest { throw new UncheckedIOException(e); } } - } |