File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1919
2020import java .math .BigDecimal ;
2121import java .math .RoundingMode ;
22+ import java .time .Instant ;
2223import java .time .LocalDate ;
2324import java .time .LocalDateTime ;
2425import java .time .format .DateTimeFormatter ;
@@ -1395,6 +1396,23 @@ static JsonRead<LocalDate> _isoLocalDate() {
13951396 return _localDate (DateTimeFormatter .ISO_LOCAL_DATE );
13961397 }
13971398
1399+ /**
1400+ * Read a datetime at path with iso format
1401+ *
1402+ * <pre>{@code
1403+ * JsonRead<LocalDate> reader = _isoLocalDate();
1404+ * }</pre>
1405+ *
1406+ * @return the reader
1407+ */
1408+ static JsonRead <Instant > _instant () {
1409+ return JsonRead .ofRead (_string ().flatMapResult (str ->
1410+ Try .of (() -> Instant .parse (str ))
1411+ .map (JsResult ::success )
1412+ .getOrElseGet (e -> JsResult .error (JsResult .Error .error ("cannot.parse.into.instant" )))
1413+ ), JsonSchema .dateTimeSchema ());
1414+ }
1415+
13981416 /**
13991417 * Read a date at path
14001418 *
Original file line number Diff line number Diff line change 66
77import java .math .BigDecimal ;
88import java .math .RoundingMode ;
9+ import java .time .Instant ;
910import java .time .LocalDate ;
1011import java .time .LocalDateTime ;
1112import java .time .format .DateTimeFormatter ;
@@ -57,6 +58,24 @@ static <T> JsonWrite<T> auto() {
5758 return $localdate (DateTimeFormatter .ISO_LOCAL_DATE );
5859 }
5960
61+
62+ /**
63+ * Write a date to json in ISO
64+ *
65+ * @return the writer
66+ */
67+ static JsonWrite <Instant > $instant (DateTimeFormatter formatter ) {
68+ return instant -> new TextNode (formatter .format (instant ));
69+ }
70+
71+ /**
72+ * Write an instant as ISO-8601 instant format
73+ * @return the writer
74+ */
75+ static JsonWrite <Instant > $instant () {
76+ return $instant (DateTimeFormatter .ISO_INSTANT );
77+ }
78+
6079 /**
6180 * Write a date to json in ISO
6281 *
Original file line number Diff line number Diff line change 1515import org .junit .jupiter .api .Test ;
1616
1717import java .math .BigDecimal ;
18+ import java .time .Instant ;
1819import java .time .LocalDate ;
1920import java .time .LocalDateTime ;
2021import java .time .format .DateTimeFormatter ;
@@ -195,6 +196,21 @@ public void isoLocalDateTimeValid() {
195196 assertThat (localDateJsonRead .jsonSchema ()).isEqualTo (JsonSchema .dateTimeSchema ());
196197 }
197198
199+ @ Test
200+ public void instantValid () {
201+ JsonRead <Instant > read = _instant ();
202+ var instant = Instant .ofEpochSecond (345544L );
203+ JsResult <Instant > test = read .read (new TextNode (DateTimeFormatter .ISO_INSTANT .format (instant )));
204+ assertThat (test ).isEqualTo (JsResult .success (instant ));
205+ assertThat (read .jsonSchema ()).isEqualTo (JsonSchema .dateTimeSchema ());
206+ }
207+
208+ @ Test
209+ public void instantInvalid () {
210+ JsResult <Instant > test = _instant ().read (new TextNode ("c'est pas un boa ca ?" ));
211+ assertThat (test ).isEqualTo (JsResult .error (JsResult .Error .error ("cannot.parse.into.instant" )));
212+ }
213+
198214 @ Test
199215 public void dateTimeInvalid () {
200216 JsResult <LocalDateTime > test = _localDateTime (DateTimeFormatter .ISO_LOCAL_DATE ).read (new TextNode ("test" ));
Original file line number Diff line number Diff line change 88import org .junit .jupiter .api .Test ;
99
1010import java .math .BigDecimal ;
11+ import java .time .Instant ;
1112import java .time .LocalDate ;
1213import java .time .LocalDateTime ;
1314import java .time .format .DateTimeFormatter ;
@@ -95,6 +96,11 @@ public void writeNullList() {
9596 assertThat (write ).isEqualTo (Json .newArray ());
9697 }
9798
99+ @ Test
100+ public void writeInstant () {
101+ JsonNode write = $instant ().write (Instant .ofEpochMilli (3600123 ));
102+ assertThat (write ).isEqualTo (new TextNode ("1970-01-01T01:00:00.123Z" ));
103+ }
98104
99105 public enum TestEnum {
100106 test1
You can’t perform that action at this time.
0 commit comments