Hallo Community,
ich möchte meinen JSON-String in ein Message.class Objekt konvertieren lassen, dabei wird aber mein Enum OperationType.class nicht gemappet. Ich habe einen JsonDeserialize...geschrieben, jedoch funktioniert das damit auch nicht. :-( habt ihr einen Tipp?
ich möchte meinen JSON-String in ein Message.class Objekt konvertieren lassen, dabei wird aber mein Enum OperationType.class nicht gemappet. Ich habe einen JsonDeserialize...geschrieben, jedoch funktioniert das damit auch nicht. :-( habt ihr einen Tipp?
Code:
public class Message
{
private HashMap<String, String> parameters = new HashMap<String, String>();
private OperationType operationType = null;
private String result = null;
public Message(OperationType operationType,
HashMap<String, String> parameters)
{
this.operationType = operationType;
this.parameters = parameters;
}
public void setParameters(HashMap<String, String> parameters)
{
this.parameters = parameters;
}
public HashMap<String, String> getParameters() {
return this.parameters;
}
public OperationType getOperationType() {
return this.operationType;
}
public String getResult() {
return this.result;
}
public void setResult(String s) {
this.result = s;
}
}
Code:
public enum OperationType
{
ENTITY_REGISTER,
ENTITY_POSITIONS,
ENTITY_SHOOT,
ENTITY_MOVE,
ENTITY_DELETE;
}
Code:
package com.george.server.parser;
import java.lang.reflect.Type;
import java.util.HashMap;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
public class JSONMessageParser extends AbstractMessageParser {
private GsonBuilder gsonBuilder = new GsonBuilder();
private Gson gson = null;
public JSONMessageParser() {
JsonDeserializer<?> jd = new JsonDeserializer<OperationType>() {
@Override
public OperationType deserialize(JsonElement arg0, Type arg1,
JsonDeserializationContext arg2) throws JsonParseException {
String value = arg0.getAsString();
OperationType operationType = null;
System.out.println("VALUE: " + value);
switch(value)
{
case "EntityRegister":
operationType = OperationType.ENTITY_REGISTER;
break;
case "EntityPositions":
operationType = OperationType.ENTITY_POSITIONS;
break;
case "EntityShoot":
operationType = OperationType.ENTITY_SHOOT;
break;
case "EntityMove":
operationType = OperationType.ENTITY_MOVE;
break;
case "EntityDelete":
operationType = OperationType.ENTITY_DELETE;
break;
}
return operationType;
}
};
this.gson = this.gsonBuilder.registerTypeAdapter(OperationType.class, jd).create();
}
@Override
public boolean isMessageParserSupported(String message) {
if (message.contains("{") || message.contains("}")
|| message.contains("[") || message.contains("]")) {
return true;
}
return false;
}
@Override
public Message parseIncomingMessage(String message) {
System.out.println(message);
Message m = gson.fromJson(message, Message.class);
return null;
}
@Override
public String parseOutgoingMessage(Message message) {
return null;
}
public static void main(String[] args) {
JSONMessageParser j = new JSONMessageParser();
j.parseIncomingMessage("{\"operation\": \"" + OperationType.ENTITY_DELETE + "\", \"parameters\": {\"id\": \"1\"}}");
}
}