/
AlternativeInstantDeserializer.java
36 lines (27 loc) · 1.22 KB
/
AlternativeInstantDeserializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.github.twitch4j.common.util;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
import java.time.Instant;
public class AlternativeInstantDeserializer extends JsonDeserializer<Instant> {
@Override
public Instant deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
String str = p.getValueAsString();
if (str == null)
return null;
str = str.trim();
// Accounts for https://github.com/twitchdev/issues/issues/644
if (str.startsWith("seconds:")) {
int startIndex = "seconds:".length();
int endIndex = str.indexOf(' ', startIndex + 1);
String seconds = str.substring(startIndex, endIndex > 0 ? endIndex : str.length());
return Instant.ofEpochSecond(Integer.parseInt(seconds));
}
// Accounts for https://github.com/twitchdev/issues/issues/347
if (str.endsWith(" +0000 UTC"))
str = str.substring(0, str.length() - " +0000 UTC".length()) + "Z";
str = str.replace(' ', 'T');
return Instant.parse(str);
}
}