add time series map keys of Instant type

This commit is contained in:
Jörg Prante 2020-10-05 13:24:17 +02:00
parent c07c2b82ff
commit 15adb8d9f7
3 changed files with 41 additions and 2 deletions

View file

@ -762,6 +762,13 @@ public final class XContentBuilder implements ToXContent, Flushable, Closeable {
return this; return this;
} }
public XContentBuilder timeseriesMap(Map<Instant, Object> map) throws IOException {
if (map == null) {
return nullValue();
}
writeInstantMap(map);
return this;
}
public XContentBuilder flatMap(Map<String, String> map) throws IOException { public XContentBuilder flatMap(Map<String, String> map) throws IOException {
if (map == null) { if (map == null) {
@ -827,6 +834,22 @@ public final class XContentBuilder implements ToXContent, Flushable, Closeable {
generator.writeEndObject(); generator.writeEndObject();
} }
private void writeInstantMap(Map<Instant, Object> map) throws IOException {
generator.writeStartObject();
for (Map.Entry<Instant, Object> 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<String, String> map) throws IOException { private void writeFlatMap(Map<String, String> map) throws IOException {
generator.writeStartObject(); generator.writeStartObject();
for (Map.Entry<String, String> entry : map.entrySet()) { for (Map.Entry<String, String> entry : map.entrySet()) {
@ -887,6 +910,10 @@ public final class XContentBuilder implements ToXContent, Flushable, Closeable {
generator.writeEndArray(); generator.writeEndArray();
} else if (type == byte[].class) { } else if (type == byte[].class) {
generator.writeBinary((byte[]) value); 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) { } else if (value instanceof Date) {
Date date = (Date) value; Date date = (Date) value;
Instant instant = Instant.ofEpochMilli(date.getTime()); Instant instant = Instant.ofEpochMilli(date.getTime());

View file

@ -10,6 +10,9 @@ import org.xbib.content.json.JsonXContent;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Map;
@ -149,4 +152,13 @@ public class XContentBuilderTest {
builder.field((String) null); builder.field((String) null);
}); });
} }
@Test
public void testInstantMap() throws IOException {
Instant instant = LocalDate.parse("2020-10-05").atStartOfDay().toInstant(ZoneOffset.UTC);
Map<Instant, Object> map = Map.of(instant, "Hello world");
XContentBuilder builder = contentBuilder();
builder.timeseriesMap(map);
assertEquals("{\"2020-10-05T00:00:00Z\":\"Hello world\"}", builder.string());
}
} }

View file

@ -1,6 +1,6 @@
group = org.xbib group = org.xbib
name = content name = content
version = 2.5.2 version = 2.5.3
gradle.wrapper.version = 6.6.1 gradle.wrapper.version = 6.6.1
xbib.net.version = 2.1.0 xbib.net.version = 2.1.0
@ -9,5 +9,5 @@ jackson.version = 2.11.2
jackson.databind.version = 2.11.2 jackson.databind.version = 2.11.2
woodstox.version = 6.2.1 woodstox.version = 6.2.1
snakeyaml.version = 1.27 snakeyaml.version = 1.27
mockito.version = 3.3.3 mockito.version = 3.5.13
asciidoclet.version = 1.5.6 asciidoclet.version = 1.5.6