33import com .google .gson .*;
44import com .google .gson .stream .JsonReader ;
55import com .google .gson .stream .JsonWriter ;
6-
76import java .io .IOException ;
87import java .lang .reflect .Type ;
98import java .time .Instant ;
1211 * A {@link JsonSerializer} that formats {@link Instant} objects into iso8601 formatted strings, and
1312 * {@link JsonDeserializer} that parses iso8601 formatted strings into {@link Instant} objects.
1413 */
15- public class ISO8601InstantAdapter extends TypeAdapter <Instant > implements JsonSerializer <Instant >, JsonDeserializer <Instant > {
16- @ Override
17- public JsonElement serialize (Instant src , Type typeOfSrc , JsonSerializationContext context ) {
18- return new JsonPrimitive (Iso8601Utils .format (src ));// ISO 8601 format
19- }
14+ public class ISO8601InstantAdapter extends TypeAdapter <Instant >
15+ implements JsonSerializer <Instant >, JsonDeserializer <Instant > {
16+ @ Override
17+ public JsonElement serialize (Instant src , Type typeOfSrc , JsonSerializationContext context ) {
18+ return new JsonPrimitive (Iso8601Utils .format (src )); // ISO 8601 format
19+ }
2020
21- @ Override
22- public Instant deserialize (JsonElement json , Type typeOfT , JsonDeserializationContext context )
23- throws JsonParseException {
24- return Instant .parse (json .getAsString ());
25- }
21+ @ Override
22+ public Instant deserialize (JsonElement json , Type typeOfT , JsonDeserializationContext context )
23+ throws JsonParseException {
24+ return Instant .parse (json .getAsString ());
25+ }
2626
27- @ Override
28- public void write (JsonWriter out , Instant value ) throws IOException {
29- out .value (value == null ? null : value .toString ());
30- }
27+ @ Override
28+ public void write (JsonWriter out , Instant value ) throws IOException {
29+ out .value (value == null ? null : value .toString ());
30+ }
3131
32- @ Override
33- public Instant read (JsonReader in ) throws IOException {
34- String str = in .nextString ();
35- return str == null ? null : Instant .parse (str );
36- }
32+ @ Override
33+ public Instant read (JsonReader in ) throws IOException {
34+ String str = in .nextString ();
35+ return str == null ? null : Instant .parse (str );
36+ }
3737}
0 commit comments