Skip to content

Commit 4e48a11

Browse files
committed
support null by OffsetDateTimeAdapter
1 parent efcd156 commit 4e48a11

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

src/main/java/api/myitmo/adapters/OffsetDateTimeAdapter.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,29 @@
33
import com.google.gson.TypeAdapter;
44
import com.google.gson.stream.JsonReader;
55
import com.google.gson.stream.JsonWriter;
6+
import com.google.gson.stream.JsonToken;
67

78
import java.io.IOException;
89
import java.time.OffsetDateTime;
910

1011
public class OffsetDateTimeAdapter extends TypeAdapter<OffsetDateTime> {
12+
1113
@Override
12-
public void write(JsonWriter jsonWriter, OffsetDateTime offsetDateTime) throws IOException {
13-
jsonWriter.value(offsetDateTime.toString());
14+
public void write(JsonWriter out, OffsetDateTime value) throws IOException {
15+
if (value == null) {
16+
out.nullValue();
17+
return;
18+
}
19+
out.value(value.toString());
1420
}
1521

1622
@Override
1723
public OffsetDateTime read(JsonReader in) throws IOException {
18-
if (in.peek() == com.google.gson.stream.JsonToken.NULL) {
24+
if (in.peek() == JsonToken.NULL) {
1925
in.nextNull();
2026
return null;
2127
}
2228

23-
String dateString = in.nextString();
24-
return OffsetDateTime.parse(dateString);
29+
return OffsetDateTime.parse(in.nextString());
2530
}
26-
}
31+
}

0 commit comments

Comments
 (0)