Skip to content

Commit bbef73e

Browse files
authored
fix(opt-null-zoned-datetime): added serializers for zoned datetime in OptionalNullable type (#83)
1 parent bea429e commit bbef73e

11 files changed

Lines changed: 963 additions & 177 deletions

File tree

src/main/java/io/apimatic/core/types/OptionalNullable.java

Lines changed: 170 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.time.LocalDate;
55
import java.time.LocalDateTime;
6+
import java.time.ZonedDateTime;
67
import java.util.List;
78
import java.util.Map;
89
import java.util.Objects;
@@ -12,6 +13,7 @@
1213
import com.fasterxml.jackson.databind.SerializerProvider;
1314

1415
import io.apimatic.core.utilities.LocalDateTimeHelper;
16+
import io.apimatic.core.utilities.ZonedDateTimeHelper;
1517

1618
/**
1719
* Class to encapsulate fields which are Optional as well as Nullable. It also provides helper.
@@ -81,97 +83,209 @@ public boolean equals(Object obj) {
8183
* {@link OptionalNullable} as its encapsulated object.
8284
*/
8385
public static class Serializer extends JsonSerializer<OptionalNullable<Object>> {
86+
87+
/**
88+
* Override this method to extract the required data from given data instance
89+
* @param data Input data instance
90+
* @return Extracted/Converted data instance with required functionality
91+
*/
92+
protected Object extractData(Object data) {
93+
return data;
94+
}
95+
8496
@Override
8597
public void serialize(
8698
OptionalNullable<Object> object, JsonGenerator jgen, SerializerProvider provider)
8799
throws IOException {
88-
jgen.writeObject(object.value);
100+
jgen.writeObject(extractData(object.value));
89101
}
90102
}
91103

92104
/**
93-
* A class to handle serialization of Unix Timestamps to DateTime objects.
105+
* A class to handle serialization of all Date formats.
106+
* @param <M> Type of the extracted data.
94107
*/
95-
public static class UnixTimestampSerializer extends JsonSerializer<OptionalNullable<Object>> {
108+
private abstract static class DateSerializer<M> extends Serializer {
109+
96110
@SuppressWarnings("unchecked")
97111
@Override
98-
public void serialize(
99-
OptionalNullable<Object> object, JsonGenerator jgen, SerializerProvider provider)
100-
throws IOException {
101-
if (object.value instanceof List<?>) {
102-
jgen.writeObject(LocalDateTimeHelper
103-
.toUnixTimestampLong((List<LocalDateTime>) object.value));
104-
} else if (object.value instanceof Map<?, ?>) {
105-
jgen.writeObject(LocalDateTimeHelper
106-
.toUnixTimestampLong((Map<String, LocalDateTime>) object.value));
107-
} else {
108-
jgen.writeObject(
109-
LocalDateTimeHelper.toUnixTimestampLong((LocalDateTime) object.value));
112+
protected Object extractData(Object data) {
113+
if (data instanceof List<?>) {
114+
return extractListData((List<Object>) data);
110115
}
116+
117+
if (data instanceof Map<?, ?>) {
118+
return extractMapData((Map<String, Object>) data);
119+
}
120+
121+
return extractSimpleData(data);
122+
}
123+
124+
public abstract M extractSimpleData(Object data);
125+
126+
public abstract List<M> extractListData(Object data);
127+
128+
public abstract Map<String, M> extractMapData(Object data);
129+
}
130+
131+
/**
132+
* A class to handle serialization of LocalDate objects to date strings.
133+
*/
134+
public static class SimpleDateSerializer extends DateSerializer<String> {
135+
136+
@Override
137+
public String extractSimpleData(Object data) {
138+
return LocalDateTimeHelper.toSimpleDate((LocalDate) data);
139+
}
140+
141+
@Override
142+
@SuppressWarnings("unchecked")
143+
public List<String> extractListData(Object data) {
144+
return LocalDateTimeHelper.toSimpleDate((List<LocalDate>) data);
145+
}
146+
147+
@Override
148+
@SuppressWarnings("unchecked")
149+
public Map<String, String> extractMapData(Object data) {
150+
return LocalDateTimeHelper.toSimpleDate((Map<String, LocalDate>) data);
151+
}
152+
}
153+
154+
/**
155+
* A class to handle serialization of Unix Timestamps to DateTime objects.
156+
*/
157+
public static class UnixTimestampSerializer extends DateSerializer<Long> {
158+
159+
@Override
160+
public Long extractSimpleData(Object data) {
161+
return LocalDateTimeHelper.toUnixTimestampLong((LocalDateTime) data);
162+
}
163+
164+
@Override
165+
@SuppressWarnings("unchecked")
166+
public List<Long> extractListData(Object data) {
167+
return LocalDateTimeHelper.toUnixTimestampLong((List<LocalDateTime>) data);
168+
}
169+
170+
@Override
171+
@SuppressWarnings("unchecked")
172+
public Map<String, Long> extractMapData(Object data) {
173+
return LocalDateTimeHelper.toUnixTimestampLong((Map<String, LocalDateTime>) data);
111174
}
112175
}
113176

114177
/**
115178
* A class to handle serialization of Rfc1123 format strings to DateTime objects.
116179
*/
117-
public static class Rfc1123DateTimeSerializer extends JsonSerializer<OptionalNullable<Object>> {
180+
public static class Rfc1123DateTimeSerializer extends DateSerializer<String> {
181+
182+
@Override
183+
public String extractSimpleData(Object data) {
184+
return LocalDateTimeHelper.toRfc1123DateTime((LocalDateTime) data);
185+
}
186+
118187
@SuppressWarnings("unchecked")
119188
@Override
120-
public void serialize(
121-
OptionalNullable<Object> object, JsonGenerator jgen, SerializerProvider provider)
122-
throws IOException {
123-
if (object.value instanceof List<?>) {
124-
jgen.writeObject(
125-
LocalDateTimeHelper.toRfc1123DateTime((List<LocalDateTime>) object.value));
126-
} else if (object.value instanceof Map<?, ?>) {
127-
jgen.writeObject(LocalDateTimeHelper
128-
.toRfc1123DateTime((Map<String, LocalDateTime>) object.value));
129-
} else {
130-
jgen.writeString(
131-
LocalDateTimeHelper.toRfc1123DateTime((LocalDateTime) object.value));
132-
}
189+
public List<String> extractListData(Object data) {
190+
return LocalDateTimeHelper.toRfc1123DateTime((List<LocalDateTime>) data);
191+
}
192+
193+
@SuppressWarnings("unchecked")
194+
@Override
195+
public Map<String, String> extractMapData(Object data) {
196+
return LocalDateTimeHelper.toRfc1123DateTime((Map<String, LocalDateTime>) data);
133197
}
134198
}
135199

136200
/**
137201
* A class to handle serialization of Rfc8601(Rfc3339) format strings to DateTime objects.
138202
*/
139-
public static class Rfc8601DateTimeSerializer extends JsonSerializer<OptionalNullable<Object>> {
203+
public static class Rfc8601DateTimeSerializer extends DateSerializer<String> {
204+
205+
@Override
206+
public String extractSimpleData(Object data) {
207+
return LocalDateTimeHelper.toRfc8601DateTime((LocalDateTime) data);
208+
}
209+
140210
@SuppressWarnings("unchecked")
141211
@Override
142-
public void serialize(
143-
OptionalNullable<Object> object, JsonGenerator jgen, SerializerProvider provider)
144-
throws IOException {
145-
if (object.value instanceof List<?>) {
146-
jgen.writeObject(
147-
LocalDateTimeHelper.toRfc8601DateTime((List<LocalDateTime>) object.value));
148-
} else if (object.value instanceof Map<?, ?>) {
149-
jgen.writeObject(LocalDateTimeHelper
150-
.toRfc8601DateTime((Map<String, LocalDateTime>) object.value));
151-
} else {
152-
jgen.writeString(
153-
LocalDateTimeHelper.toRfc8601DateTime((LocalDateTime) object.value));
154-
}
212+
public List<String> extractListData(Object data) {
213+
return LocalDateTimeHelper.toRfc8601DateTime((List<LocalDateTime>) data);
214+
}
215+
216+
@SuppressWarnings("unchecked")
217+
@Override
218+
public Map<String, String> extractMapData(Object data) {
219+
return LocalDateTimeHelper.toRfc8601DateTime((Map<String, LocalDateTime>) data);
155220
}
156221
}
157222

158223
/**
159-
* A class to handle serialization of LocalDate objects to date strings.
224+
* A class to handle serialization of Unix Timestamps to DateTime objects.
160225
*/
161-
public static class SimpleDateSerializer extends JsonSerializer<OptionalNullable<Object>> {
226+
public static class ZonedUnixTimestampSerializer extends DateSerializer<Long> {
227+
228+
@Override
229+
public Long extractSimpleData(Object data) {
230+
return ZonedDateTimeHelper.toUnixTimestampLong((ZonedDateTime) data);
231+
}
232+
162233
@SuppressWarnings("unchecked")
163234
@Override
164-
public void serialize(
165-
OptionalNullable<Object> object, JsonGenerator jgen, SerializerProvider provider)
166-
throws IOException {
167-
if (object.value instanceof List<?>) {
168-
jgen.writeObject(LocalDateTimeHelper.toSimpleDate((List<LocalDate>) object.value));
169-
} else if (object.value instanceof Map<?, ?>) {
170-
jgen.writeObject(
171-
LocalDateTimeHelper.toSimpleDate((Map<String, LocalDate>) object.value));
172-
} else {
173-
jgen.writeString(LocalDateTimeHelper.toSimpleDate((LocalDate) object.value));
174-
}
235+
public List<Long> extractListData(Object data) {
236+
return ZonedDateTimeHelper.toUnixTimestampLong((List<ZonedDateTime>) data);
237+
}
238+
239+
@SuppressWarnings("unchecked")
240+
@Override
241+
public Map<String, Long> extractMapData(Object data) {
242+
return ZonedDateTimeHelper.toUnixTimestampLong((Map<String, ZonedDateTime>) data);
243+
}
244+
}
245+
246+
/**
247+
* A class to handle serialization of Rfc1123 format strings to DateTime objects.
248+
*/
249+
public static class ZonedRfc1123DateTimeSerializer extends DateSerializer<String> {
250+
251+
@Override
252+
public String extractSimpleData(Object data) {
253+
return ZonedDateTimeHelper.toRfc1123DateTime((ZonedDateTime) data);
254+
}
255+
256+
@SuppressWarnings("unchecked")
257+
@Override
258+
public List<String> extractListData(Object data) {
259+
return ZonedDateTimeHelper.toRfc1123DateTime((List<ZonedDateTime>) data);
260+
}
261+
262+
@SuppressWarnings("unchecked")
263+
@Override
264+
public Map<String, String> extractMapData(Object data) {
265+
return ZonedDateTimeHelper.toRfc1123DateTime((Map<String, ZonedDateTime>) data);
266+
}
267+
}
268+
269+
/**
270+
* A class to handle serialization of Rfc8601(Rfc3339) format strings to DateTime objects.
271+
*/
272+
public static class ZonedRfc8601DateTimeSerializer extends DateSerializer<String> {
273+
274+
@Override
275+
public String extractSimpleData(Object data) {
276+
return ZonedDateTimeHelper.toRfc8601DateTime((ZonedDateTime) data);
277+
}
278+
279+
@SuppressWarnings("unchecked")
280+
@Override
281+
public List<String> extractListData(Object data) {
282+
return ZonedDateTimeHelper.toRfc8601DateTime((List<ZonedDateTime>) data);
283+
}
284+
285+
@SuppressWarnings("unchecked")
286+
@Override
287+
public Map<String, String> extractMapData(Object data) {
288+
return ZonedDateTimeHelper.toRfc8601DateTime((Map<String, ZonedDateTime>) data);
175289
}
176290
}
177291
}

0 commit comments

Comments
 (0)