json date handling related changes

This commit is contained in:
Grahame Grieve 2023-01-05 08:43:57 +11:00
parent 45cf907204
commit 19f1171962
2 changed files with 68 additions and 3 deletions

View File

@ -1,8 +1,12 @@
package org.hl7.fhir.utilities.json.model;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -49,7 +53,7 @@ public class JsonObject extends JsonElement {
public JsonObject add(String name, String value) throws JsonException {
check(name != null, "Name is null");
return add(name, new JsonString(value));
return add(name, value == null ? new JsonNull() : new JsonString(value));
}
public JsonObject add(String name, boolean value) throws JsonException {
@ -74,14 +78,20 @@ public class JsonObject extends JsonElement {
}
}
public JsonObject set(String name, Instant value) throws JsonException {
String v = value == null ? null : value.toString();
return set(name, v);
}
public JsonObject set(String name, String value) throws JsonException {
check(name != null, "Name is null");
JsonProperty p = propMap.get(name);
if (p != null) {
p.setValue(new JsonString(value));
p.setValue(value == null ? new JsonNull() : new JsonString(value));
return this;
} else {
return add(name, new JsonString(value));
return add(name, value == null ? new JsonNull() : new JsonString(value));
}
}
@ -256,6 +266,19 @@ public class JsonObject extends JsonElement {
}
}
public Instant asInstant(String name) throws ParseException {
String source = asString(name);
if (Utilities.noString(source) || "null".equals(source)) {
return null;
} else if (source.length() <= 10) {
Date d = new SimpleDateFormat("yyyy-mm-dd").parse(source);
return d.toInstant();
} else {
OffsetDateTime odt = OffsetDateTime.parse(source);
return odt.toInstant();
}
}
public JsonObject forceObject(String name) throws JsonException {
if (has(name) && !hasObject(name)) {
remove(name);

View File

@ -0,0 +1,42 @@
package org.hl7.fhir.utilities.json;
import java.text.ParseException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import org.hl7.fhir.utilities.json.model.JsonObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class JsonObjectTests {
@Test
void test1() throws ParseException {
JsonObject json = new JsonObject();
json.set("date", "2022-10-12");
System.out.println(json.asString("date"));
Assertions.assertNotNull(json.asInstant("date"));
}
@Test
void test2() throws ParseException {
Instant instant = Instant.now();
JsonObject json = new JsonObject();
json.set("date", instant);
System.out.println(json.asString("date"));
Assertions.assertEquals(instant, json.asInstant("date"));
}
@Test
void test3() {
Instant instant = Instant.now();
DateTimeFormatter df = DateTimeFormatter.ofPattern("MMM-yyyy").withLocale(Locale.getDefault()).withZone(ZoneId.systemDefault());
System.out.println(df.format(instant));
df = DateTimeFormatter.ofPattern("yyyy-MM-dd").withLocale(Locale.getDefault()).withZone(ZoneId.systemDefault());
System.out.println(df.format(instant));
Assertions.assertNotEquals(instant, Instant.now());
}
}