dev.struchkov.godfather.main.domain.jackson.TelegramEventContainerDeserializer Maven / Gradle / Ivy
package dev.struchkov.godfather.main.domain.jackson;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import dev.struchkov.godfather.main.domain.EventContainer;
import lombok.SneakyThrows;
import java.io.IOException;
public class TelegramEventContainerDeserializer extends StdDeserializer {
public TelegramEventContainerDeserializer() {
this(null);
}
public TelegramEventContainerDeserializer(Class> vc) {
super(vc);
}
@Override
@SneakyThrows
public EventContainer deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
final JsonNode node = jsonParser.getCodec().readTree(jsonParser);
final Class typeEvent = Class.forName(node.get("type").asText());
final JsonNode object = node.get("object");
final EventContainer eventContainer = new EventContainer<>();
eventContainer.setType(typeEvent);
eventContainer.setObject(jsonParser.getCodec().treeToValue(object, typeEvent));
return eventContainer;
}
}