package com.sikey.wa04.common.dto.deserializer;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.sikey.wa04.common.enums.MessageType;

import java.io.IOException;

public class MessageTypeDeserializer extends StdDeserializer<MessageType> implements Deserializer {

    public MessageTypeDeserializer() {
        super(MessageType.class);
    }

    @Override
    public MessageType deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
        return MessageType.valueOfMsg(Integer.valueOf(p.getText()));
    }

    @Override
    public void registerModule(ObjectMapper objectMapper) {
        SimpleModule module = new SimpleModule();
        module.addDeserializer(MessageType.class, this);
        objectMapper.registerModule(module);
    }
}
