From eee78409ae412f8a3d97922ae8f936f847c7bea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Thu, 9 Jan 2025 19:00:23 +0100 Subject: [PATCH] add LocalDateTime to JsonBuilder --- .../datastructures/json/tiny/JsonBuilder.java | 43 ++++++++----------- .../json/tiny/test/BuilderTest.java | 17 ++++++++ gradle.properties | 2 +- 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/datastructures-json-tiny/src/main/java/org/xbib/datastructures/json/tiny/JsonBuilder.java b/datastructures-json-tiny/src/main/java/org/xbib/datastructures/json/tiny/JsonBuilder.java index b4b7cda..de0333c 100644 --- a/datastructures-json-tiny/src/main/java/org/xbib/datastructures/json/tiny/JsonBuilder.java +++ b/datastructures-json-tiny/src/main/java/org/xbib/datastructures/json/tiny/JsonBuilder.java @@ -6,6 +6,8 @@ import org.xbib.datastructures.api.TimeValue; import java.io.IOException; import java.io.UncheckedIOException; import java.time.Instant; +import java.time.LocalDateTime; +import java.time.OffsetDateTime; import java.util.Collection; import java.util.Map; import java.util.Objects; @@ -119,32 +121,21 @@ public class JsonBuilder implements Builder { if (state.structure == Structure.COLLECTION) { beginArrayValue(); } - if (object == null) { - buildNull(); - } else if (object instanceof CharSequence) { - buildString((CharSequence) object, true); - } else if (object instanceof Boolean) { - buildBoolean((Boolean) object); - } else if (object instanceof Byte) { - buildNumber((byte) object); - } else if (object instanceof Integer) { - buildNumber((int) object); - } else if (object instanceof Long) { - buildNumber((long) object); - } else if (object instanceof Float) { - buildNumber((float) object); - } else if (object instanceof Double) { - buildNumber((double) object); - } else if (object instanceof Number) { - buildNumber((Number) object); - } else if (object instanceof Instant) { - buildInstant((Instant) object); - } else if (object instanceof ByteSizeValue) { - buildString(object.toString(), false); - } else if (object instanceof TimeValue) { - buildString(object.toString(), false); - } else { - throw new IllegalArgumentException("unable to write object class " + object.getClass()); + switch (object) { + case null -> buildNull(); + case CharSequence charSequence -> buildString(charSequence, true); + case Boolean b -> buildBoolean(b); + case Byte b -> buildNumber(b); + case Integer i -> buildNumber(i); + case Long l -> buildNumber(l); + case Float v -> buildNumber(v); + case Double v -> buildNumber(v); + case Number number -> buildNumber(number); + case Instant instant -> buildInstant(instant); + case LocalDateTime localDateTime -> buildInstant(localDateTime.toInstant(OffsetDateTime.now().getOffset())); + case ByteSizeValue byteSizeValue -> buildString(byteSizeValue.toString(), false); + case TimeValue timeValue -> buildString(timeValue.toString(), false); + default -> throw new IllegalArgumentException("unable to write object class " + object.getClass()); } return this; } diff --git a/datastructures-json-tiny/src/test/java/org/xbib/datastructures/json/tiny/test/BuilderTest.java b/datastructures-json-tiny/src/test/java/org/xbib/datastructures/json/tiny/test/BuilderTest.java index 1d256cf..4b495ef 100644 --- a/datastructures-json-tiny/src/test/java/org/xbib/datastructures/json/tiny/test/BuilderTest.java +++ b/datastructures-json-tiny/src/test/java/org/xbib/datastructures/json/tiny/test/BuilderTest.java @@ -8,6 +8,9 @@ import org.xbib.datastructures.json.tiny.StringParser; import java.io.IOException; import java.io.StringReader; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -271,4 +274,18 @@ public class BuilderTest { .endMap(); assertEquals("{\"bool\":{\"should\":[{\"simple_query_string\":{\"query\":\"Jörg\",\"fields\":[\"name\"],\"default_operator\":\"and\"}},{\"simple_query_string\":{\"query\":\"\\\"Jörg\\\"\",\"fields\":[\"name^2\"],\"default_operator\":\"and\"}}],\"minimum_should_match\":\"1\"}}", builder.build()); } + + @Test + public void testInstant() throws IOException { + Instant instant = Instant.now(); + LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); + JsonBuilder builder = JsonBuilder.builder(); + builder.beginMap() + .beginMap("test") + .field("instant", instant) + .field("localdatetime", localDateTime) + .endMap() + .endMap(); + assertEquals("{\"test\":{\"instant\":\"" + instant + "\",\"localdatetime\":\"" + instant + "\"}}", builder.build()); + } } diff --git a/gradle.properties b/gradle.properties index 9290ea4..34f8b0b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ group = org.xbib name = datastructures -version = 5.2.0 +version = 5.2.1