I have jsons that contain a message of any type and the json contains a String that says which type the message has.
I want to deserialize them and get 1. an Instance of the messag type representing the message and 2. an instance of Topic where T is the message type.
As examples:
input1
{
"messageType":"String",
"message": "a string"
}
I expect a result after the deserialization to be the same as this done by hand:
Topic<String> t = new Topic<String>(String.class);
String message = "a string";
input2
{
"messageType":"Integer",
"message": 1
}
I expect a result after the deserialization to be the same as this done by hand:
Topic<Integer> t = new Topic<Integer>(Integer.class);
Integer message = 1;
input3
{
"messageType":"MyClass",
"message": {"a": "something", "b": 1}
}
I expect a result after the deserialization to be the same as this done by hand:
Topic<MyClass> t = new Topic<MyClass>(MyClass.class);
MyClass message = new MyClass("something", 1);
input4
... same with other types ...
I think you got the point. But now I need to do this somehow in a generic/abstract way. I tried this, but this will not work:
private enum MessageType {
STRING(String.class), INTEGER(Integer.class), BOOLEAN(Boolean.class), MYCLASS(MyClass.class);
private Class<?> clazz;
MessageType(Class<?> clazz) {
this.clazz = clazz;
}
}
private static class MyJson {
String topicId;
String messageType;
Object message;
}
MyJson<?> myJson = gson.fromJson(input, MyJson.class);
MessageType type = MessageType.valueOf(myJson.messageType);
Class<?> clazz = type.getClass();
??? message = clazz.newInstance(message);
Topic<???> t = new ?????
I don't know what to do?! I need Topic and Message typed, but how??? The following seems so bad:
@SuppressWarnings("unchecked")
private <T> Topic<T> createTopic(Class<T> typeClass) {
try {
return Topic.class.getConstructor(typeClass).newInstance(typeClass);
}
catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Fail");
}
}
That works for me now:
class Topic<MessageType> {
// ...
public Class<MessageType> getMessageTypeClass() // ...
// ...
}
parse result data struct:
public class TopicLine {
public Topic<?> topic;
public Object message;
}
deserializer:
public class MessageDeserializer implements JsonDeserializer<TopicLine> {
private static class InternalParseLine {
String messageType;
JsonElement message;
}
private Map<String, Topic<?>> messageTypes = new HashMap<String, Topic<?>>();
public MessageDeserializer() {
messageTypes.put("Integer", new Topic<Integer>(Integer.class));
// other topics
}
private static class InternalParseLine {
String topicId;
JsonElement message;
}
@Override
public TopicLine deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
InternalParseLine line = context.deserialize(json, InternalParseLine.class);
TopicLine topicLine = new TopicLine();
topicLine.topic = topics.get(line.messageType);
topicLine.message = context.deserialize(line.message, topicLine.topic.getMessageTypeClass());
return topicLine;
}
}
usage:
json = new GsonBuilder()
.registerTypeAdapter(TopicLine.class, new TopicLineDeserializer())
.create();
TopicLine t = json.fromJson(line, TopicLine.class);
In fact, to make it more dynamic I use a topics-manager which I pass to the deserializer in its constructor instead of a static map. So I can register and unregister topic/message-types on the fly during runtime.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.