diff options
author | Jim Hahn <jrh3@att.com> | 2021-02-18 17:15:07 -0500 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2021-02-18 17:24:26 -0500 |
commit | 758cd2e691950843415515ba6df2295679bd938b (patch) | |
tree | 1c3acdcf18a04f655b6e642fc80a7d8630934f24 /gson/src/main/java/org/onap/policy/common/gson/InstantTypeAdapter.java | |
parent | 00ee2d5b4f1e847d8e272c79f66230aa30cb11d6 (diff) |
Remove duplicate code from type adapters
Extracted a common superclass, StringTypeAdapter, out of the gson type
adapter classes.
Issue-ID: POLICY-2914
Change-Id: I4bbd2a6e8372e7f42c4952ba9ff03eed97473fb2
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'gson/src/main/java/org/onap/policy/common/gson/InstantTypeAdapter.java')
-rw-r--r-- | gson/src/main/java/org/onap/policy/common/gson/InstantTypeAdapter.java | 38 |
1 files changed, 7 insertions, 31 deletions
diff --git a/gson/src/main/java/org/onap/policy/common/gson/InstantTypeAdapter.java b/gson/src/main/java/org/onap/policy/common/gson/InstantTypeAdapter.java index 9ebf2baa..bad66af3 100644 --- a/gson/src/main/java/org/onap/policy/common/gson/InstantTypeAdapter.java +++ b/gson/src/main/java/org/onap/policy/common/gson/InstantTypeAdapter.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,41 +20,17 @@ package org.onap.policy.common.gson; -import com.google.gson.TypeAdapter; -import com.google.gson.stream.JsonReader; -import com.google.gson.stream.JsonToken; -import com.google.gson.stream.JsonWriter; -import java.io.IOException; import java.time.Instant; -import java.time.format.DateTimeParseException; /** * GSON Type Adapter for "Instant" fields, that uses the standard ISO_INSTANT formatter. */ -public class InstantTypeAdapter extends TypeAdapter<Instant> { +public class InstantTypeAdapter extends StringTypeAdapter<Instant> { - @Override - public void write(JsonWriter out, Instant value) throws IOException { - if (value == null) { - out.nullValue(); - } else { - out.value(value.toString()); - } - } - - @Override - public Instant read(JsonReader in) throws IOException { - try { - if (in.peek() == JsonToken.NULL) { - in.nextNull(); - return null; - } else { - String text = in.nextString(); - return Instant.parse(text); - } - - } catch (DateTimeParseException e) { - throw new IOException("invalid date", e); - } + /** + * Constructs an adapter. + */ + public InstantTypeAdapter() { + super("date", Instant::parse, Instant::toString); } } |