3
3
import com .google .gson .*;
4
4
import com .google .gson .stream .JsonReader ;
5
5
import com .google .gson .stream .JsonWriter ;
6
-
7
6
import java .io .IOException ;
8
7
import java .lang .reflect .Type ;
9
8
import java .time .Instant ;
12
11
* A {@link JsonSerializer} that formats {@link Instant} objects into iso8601 formatted strings, and
13
12
* {@link JsonDeserializer} that parses iso8601 formatted strings into {@link Instant} objects.
14
13
*/
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
+ }
20
20
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
+ }
26
26
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
+ }
31
31
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
+ }
37
37
}
0 commit comments