add LocalDateTime to JsonBuilder
Some checks are pending
CodeQL / Analyze (push) Waiting to run

This commit is contained in:
Jörg Prante 2025-01-09 19:00:23 +01:00
parent 4960f8cdb4
commit abd3c5d459
3 changed files with 35 additions and 29 deletions

View file

@ -6,6 +6,8 @@ import org.xbib.datastructures.api.TimeValue;
import java.io.IOException; import java.io.IOException;
import java.io.UncheckedIOException; import java.io.UncheckedIOException;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -119,32 +121,21 @@ public class JsonBuilder implements Builder {
if (state.structure == Structure.COLLECTION) { if (state.structure == Structure.COLLECTION) {
beginArrayValue(); beginArrayValue();
} }
if (object == null) { switch (object) {
buildNull(); case null -> buildNull();
} else if (object instanceof CharSequence) { case CharSequence charSequence -> buildString(charSequence, true);
buildString((CharSequence) object, true); case Boolean b -> buildBoolean(b);
} else if (object instanceof Boolean) { case Byte b -> buildNumber(b);
buildBoolean((Boolean) object); case Integer i -> buildNumber(i);
} else if (object instanceof Byte) { case Long l -> buildNumber(l);
buildNumber((byte) object); case Float v -> buildNumber(v);
} else if (object instanceof Integer) { case Double v -> buildNumber(v);
buildNumber((int) object); case Number number -> buildNumber(number);
} else if (object instanceof Long) { case Instant instant -> buildInstant(instant);
buildNumber((long) object); case LocalDateTime localDateTime -> buildInstant(localDateTime.toInstant(OffsetDateTime.now().getOffset()));
} else if (object instanceof Float) { case ByteSizeValue byteSizeValue -> buildString(byteSizeValue.toString(), false);
buildNumber((float) object); case TimeValue timeValue -> buildString(timeValue.toString(), false);
} else if (object instanceof Double) { default -> throw new IllegalArgumentException("unable to write object class " + object.getClass());
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());
} }
return this; return this;
} }

View file

@ -8,6 +8,9 @@ import org.xbib.datastructures.json.tiny.StringParser;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -271,4 +274,18 @@ public class BuilderTest {
.endMap(); .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()); 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());
}
} }

View file

@ -1,5 +1,3 @@
group = org.xbib group = org.xbib
name = datastructures name = datastructures
version = 5.2.0 version = 5.2.1
org.gradle.warning.mode = all