From 15adb8d9f731044dd06cf13c8cb14850c57cb8a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Mon, 5 Oct 2020 13:24:17 +0200 Subject: [PATCH] add time series map keys of Instant type --- .../org/xbib/content/XContentBuilder.java | 27 +++++++++++++++++++ .../org/xbib/content/XContentBuilderTest.java | 12 +++++++++ gradle.properties | 4 +-- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/content-core/src/main/java/org/xbib/content/XContentBuilder.java b/content-core/src/main/java/org/xbib/content/XContentBuilder.java index 81fa03c..883bcf7 100644 --- a/content-core/src/main/java/org/xbib/content/XContentBuilder.java +++ b/content-core/src/main/java/org/xbib/content/XContentBuilder.java @@ -762,6 +762,13 @@ public final class XContentBuilder implements ToXContent, Flushable, Closeable { return this; } + public XContentBuilder timeseriesMap(Map map) throws IOException { + if (map == null) { + return nullValue(); + } + writeInstantMap(map); + return this; + } public XContentBuilder flatMap(Map map) throws IOException { if (map == null) { @@ -827,6 +834,22 @@ public final class XContentBuilder implements ToXContent, Flushable, Closeable { generator.writeEndObject(); } + private void writeInstantMap(Map map) throws IOException { + generator.writeStartObject(); + for (Map.Entry entry : map.entrySet()) { + Instant instant = entry.getKey(); + ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()); + field(zdt.format(DateTimeFormatter.ISO_INSTANT)); + Object value = entry.getValue(); + if (value == null) { + generator.writeNull(); + } else { + writeValue(value); + } + } + generator.writeEndObject(); + } + private void writeFlatMap(Map map) throws IOException { generator.writeStartObject(); for (Map.Entry entry : map.entrySet()) { @@ -887,6 +910,10 @@ public final class XContentBuilder implements ToXContent, Flushable, Closeable { generator.writeEndArray(); } else if (type == byte[].class) { generator.writeBinary((byte[]) value); + } else if (value instanceof Instant) { + Instant instant = (Instant) value; + ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()); + generator.writeString(zdt.format(DateTimeFormatter.ISO_INSTANT)); } else if (value instanceof Date) { Date date = (Date) value; Instant instant = Instant.ofEpochMilli(date.getTime()); diff --git a/content-core/src/test/java/org/xbib/content/XContentBuilderTest.java b/content-core/src/test/java/org/xbib/content/XContentBuilderTest.java index c8f72f0..f2b7904 100644 --- a/content-core/src/test/java/org/xbib/content/XContentBuilderTest.java +++ b/content-core/src/test/java/org/xbib/content/XContentBuilderTest.java @@ -10,6 +10,9 @@ import org.xbib.content.json.JsonXContent; import java.io.IOException; import java.math.BigDecimal; import java.math.BigInteger; +import java.time.Instant; +import java.time.LocalDate; +import java.time.ZoneOffset; import java.util.Arrays; import java.util.Date; import java.util.Map; @@ -149,4 +152,13 @@ public class XContentBuilderTest { builder.field((String) null); }); } + + @Test + public void testInstantMap() throws IOException { + Instant instant = LocalDate.parse("2020-10-05").atStartOfDay().toInstant(ZoneOffset.UTC); + Map map = Map.of(instant, "Hello world"); + XContentBuilder builder = contentBuilder(); + builder.timeseriesMap(map); + assertEquals("{\"2020-10-05T00:00:00Z\":\"Hello world\"}", builder.string()); + } } diff --git a/gradle.properties b/gradle.properties index 9260731..585899d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ group = org.xbib name = content -version = 2.5.2 +version = 2.5.3 gradle.wrapper.version = 6.6.1 xbib.net.version = 2.1.0 @@ -9,5 +9,5 @@ jackson.version = 2.11.2 jackson.databind.version = 2.11.2 woodstox.version = 6.2.1 snakeyaml.version = 1.27 -mockito.version = 3.3.3 +mockito.version = 3.5.13 asciidoclet.version = 1.5.6