add LocalDateTime to JsonBuilder

This commit is contained in:
Jörg Prante 2025-01-09 19:00:23 +01:00
parent 1f217c417f
commit eee78409ae
3 changed files with 35 additions and 27 deletions

View file

@ -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;
}

View file

@ -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());
}
}

View file

@ -1,3 +1,3 @@
group = org.xbib
name = datastructures
version = 5.2.0
version = 5.2.1