feat: implemented proper MessageDeserialization
This commit is contained in:
parent
99e1de14bc
commit
788591268c
@ -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");
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,34 @@
|
|||||||
package uulm.teamname.marvelous.gamelibrary.messages;
|
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
|
* 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
|
* messageType, as fields that aren't sent are null. Note that most messages are not deserialized into the {@link
|
||||||
* BasicMessage}, but instead into the {@link EventMessage}.
|
* 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 {
|
public class BasicMessage {
|
||||||
|
|
||||||
/** The messageType describes the type of message that might be sent */
|
/** 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 */
|
/** I have no idea what this might be used for, even less the boolean in the answer */
|
||||||
public String optionals;
|
public String optionals;
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* <b>Exists only in a HelloClient message.</b>
|
public boolean equals(Object o) {
|
||||||
* Describes whether the user was in a running game before reconnecting.
|
if (this == o) return true;
|
||||||
*/
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
public Boolean runningGame;
|
BasicMessage that = (BasicMessage) o;
|
||||||
|
return messageType == that.messageType && Objects.equals(optionals, that.optionals);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* <b>Exists only in GameAssignment and GeneralAssignment messages.</b>
|
public int hashCode() {
|
||||||
* Used for notifying the player of which party they have been assigned to
|
return Objects.hash(messageType, optionals);
|
||||||
*/
|
}
|
||||||
public String gameID;
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "BasicMessage{" +
|
||||||
|
"messageType=" + messageType +
|
||||||
|
", optionals='" + optionals + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// @JsonDeserialize(using = MessageDeserializer.class)
|
||||||
|
@ -1,13 +1,16 @@
|
|||||||
package uulm.teamname.marvelous.gamelibrary.messages;
|
package uulm.teamname.marvelous.gamelibrary.messages;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
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.events.Event;
|
||||||
|
import uulm.teamname.marvelous.gamelibrary.json.login.EventMessageDeserializer;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
/** Represents a message sent between client and server and contains all possible data. */
|
/** Represents a message sent between client and server and contains all possible data. */
|
||||||
|
@JsonDeserialize(using = EventMessageDeserializer.class)
|
||||||
public class EventMessage extends BasicMessage {
|
public class EventMessage extends BasicMessage {
|
||||||
|
|
||||||
/** The list of {@link Event Events} sent inside the message. */
|
/** The list of {@link Event Events} sent inside the message. */
|
||||||
|
Loading…
Reference in New Issue
Block a user