|
3 | 3 | import java.io.IOException; |
4 | 4 | import java.time.LocalDate; |
5 | 5 | import java.time.LocalDateTime; |
| 6 | +import java.time.ZonedDateTime; |
6 | 7 | import java.util.List; |
7 | 8 | import java.util.Map; |
8 | 9 | import java.util.Objects; |
|
12 | 13 | import com.fasterxml.jackson.databind.SerializerProvider; |
13 | 14 |
|
14 | 15 | import io.apimatic.core.utilities.LocalDateTimeHelper; |
| 16 | +import io.apimatic.core.utilities.ZonedDateTimeHelper; |
15 | 17 |
|
16 | 18 | /** |
17 | 19 | * Class to encapsulate fields which are Optional as well as Nullable. It also provides helper. |
@@ -81,97 +83,209 @@ public boolean equals(Object obj) { |
81 | 83 | * {@link OptionalNullable} as its encapsulated object. |
82 | 84 | */ |
83 | 85 | 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 | + |
84 | 96 | @Override |
85 | 97 | public void serialize( |
86 | 98 | OptionalNullable<Object> object, JsonGenerator jgen, SerializerProvider provider) |
87 | 99 | throws IOException { |
88 | | - jgen.writeObject(object.value); |
| 100 | + jgen.writeObject(extractData(object.value)); |
89 | 101 | } |
90 | 102 | } |
91 | 103 |
|
92 | 104 | /** |
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. |
94 | 107 | */ |
95 | | - public static class UnixTimestampSerializer extends JsonSerializer<OptionalNullable<Object>> { |
| 108 | + private abstract static class DateSerializer<M> extends Serializer { |
| 109 | + |
96 | 110 | @SuppressWarnings("unchecked") |
97 | 111 | @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); |
110 | 115 | } |
| 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); |
111 | 174 | } |
112 | 175 | } |
113 | 176 |
|
114 | 177 | /** |
115 | 178 | * A class to handle serialization of Rfc1123 format strings to DateTime objects. |
116 | 179 | */ |
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 | + |
118 | 187 | @SuppressWarnings("unchecked") |
119 | 188 | @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); |
133 | 197 | } |
134 | 198 | } |
135 | 199 |
|
136 | 200 | /** |
137 | 201 | * A class to handle serialization of Rfc8601(Rfc3339) format strings to DateTime objects. |
138 | 202 | */ |
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 | + |
140 | 210 | @SuppressWarnings("unchecked") |
141 | 211 | @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); |
155 | 220 | } |
156 | 221 | } |
157 | 222 |
|
158 | 223 | /** |
159 | | - * A class to handle serialization of LocalDate objects to date strings. |
| 224 | + * A class to handle serialization of Unix Timestamps to DateTime objects. |
160 | 225 | */ |
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 | + |
162 | 233 | @SuppressWarnings("unchecked") |
163 | 234 | @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); |
175 | 289 | } |
176 | 290 | } |
177 | 291 | } |
0 commit comments