feat: implemented proper MessageDeserialization

This commit is contained in:
Yannik Bretschneider 2021-06-03 17:37:44 +02:00
parent 99e1de14bc
commit 788591268c
3 changed files with 81 additions and 12 deletions

View File

@ -0,0 +1,32 @@
package uulm.teamname.marvelous.gamelibrary.json.login;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage;
import uulm.teamname.marvelous.gamelibrary.messages.EventMessage;
import uulm.teamname.marvelous.gamelibrary.messages.MessageType;
import uulm.teamname.marvelous.gamelibrary.messages.RequestMessage;
import java.io.IOException;
public class EventMessageDeserializer extends JsonDeserializer<BasicMessage> {
@Override
public BasicMessage deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonNode node = p.readValueAsTree();
JsonNode subNode;
if ((subNode = node.get("messageType")) != null) {
if (MessageType.valueOf(subNode.textValue()) == MessageType.EVENTS) {
return p.getCodec().treeToValue(node, EventMessage.class);
} else if (MessageType.valueOf(subNode.textValue()) == MessageType.REQUESTS) {
return p.getCodec().treeToValue(node, RequestMessage.class);
}
}
throw new IllegalStateException("EventMessage is actually not an EventMessage");
}
}

View File

@ -1,10 +1,34 @@
package uulm.teamname.marvelous.gamelibrary.messages;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import uulm.teamname.marvelous.gamelibrary.json.login.MessageDeserializer;
import java.util.Objects;
/**
* The basic message from the standard, containing all possible expansion keys. In other words, one needs to check the
* messageType, as fields that aren't sent are null. Note that ingame messages are not deserialized into the {@link
* BasicMessage}, but instead into the {@link EventMessage}.
* messageType, as fields that aren't sent are null. Note that most messages are not deserialized into the {@link
* BasicMessage}, but instead into messages such as the {@link EventMessage} or {@link GameAssignmentMessage}.
*/
@JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION, defaultImpl = BasicMessage.class)
@JsonSubTypes({
@Type(HelloClientMessage.class),
@Type(HelloServerMessage.class),
@Type(ErrorMessage.class),
@Type(PlayerReadyMessage.class),
@Type(ReconnectMessage.class),
@Type(GameAssignmentMessage.class),
@Type(GeneralAssignmentMessage.class),
@Type(CharacterSelectionMessage.class),
@Type(ConfirmSelectionMessage.class),
@Type(GameStructureMessage.class),
@Type(EventMessage.class),
// @Type(RequestMessage.class), There's a custom deserializer in EventMessage for this
@Type(GoodbyeClientMessage.class)
})
public class BasicMessage {
/** The messageType describes the type of message that might be sent */
@ -13,15 +37,25 @@ public class BasicMessage {
/** I have no idea what this might be used for, even less the boolean in the answer */
public String optionals;
/**
* <b>Exists only in a HelloClient message.</b>
* Describes whether the user was in a running game before reconnecting.
*/
public Boolean runningGame;
/**
* <b>Exists only in GameAssignment and GeneralAssignment messages.</b>
* Used for notifying the player of which party they have been assigned to
*/
public String gameID;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BasicMessage that = (BasicMessage) o;
return messageType == that.messageType && Objects.equals(optionals, that.optionals);
}
@Override
public int hashCode() {
return Objects.hash(messageType, optionals);
}
@Override
public String toString() {
return "BasicMessage{" +
"messageType=" + messageType +
", optionals='" + optionals + '\'' +
'}';
}
}
// @JsonDeserialize(using = MessageDeserializer.class)

View File

@ -1,13 +1,16 @@
package uulm.teamname.marvelous.gamelibrary.messages;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.json.login.EventMessageDeserializer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Objects;
/** Represents a message sent between client and server and contains all possible data. */
@JsonDeserialize(using = EventMessageDeserializer.class)
public class EventMessage extends BasicMessage {
/** The list of {@link Event Events} sent inside the message. */