blob: 6f34b0760a1ebb671830510cded76515870db227 (
plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
package moe.ymc.acron.c2s;
import com.google.gson.*;
import com.google.gson.annotations.SerializedName;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.reflect.Type;
public record ReqCmd(@SerializedName("id") int id,
@SerializedName("cmd") @NotNull String cmd,
@SerializedName("config") @Nullable ReqSetConfig config)
implements Request {
@Override
public void validate() {
if (cmd == null) {
throw new IllegalArgumentException("Property 'cmd' cannot be null.");
}
}
@Override
public int getId() {
return id;
}
public static class ReqCmdDeserializer implements JsonDeserializer<ReqCmd> {
@Override
public ReqCmd deserialize(JsonElement json,
Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
final JsonObject object = json.getAsJsonObject();
final int id = object.has("id") ?
object.get("id").getAsInt() :
-1;
final String cmd = object.has("cmd") ?
object.get("cmd").getAsString() :
null;
// We cannot use context#deserialize here
// because RuntimeTypeAdapterFactory keeps kicking in
// and asking for the 'type' property
// which is obviously redundant for an inner field.
// Thus, I pass it directly to the deserializer
// to bypass the RuntimeTypeAdapterFactory.
final ReqSetConfig reqSetConfig = object.has("config") ?
new ReqSetConfig.ReqSetConfigDeserializer()
.deserialize(object.get("config"), ReqSetConfig.class, context) :
null;
return new ReqCmd(id, cmd, reqSetConfig);
}
}
}
|