All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.struchkov.godfather.main.domain.jackson.TelegramEventContainerDeserializer Maven / Gradle / Ivy

There is a newer version: 1.3.1
Show newest version
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;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy