package ch.zhaw.gartenverwaltung.json;

import ch.zhaw.gartenverwaltung.types.GrowthPhaseType;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import java.io.IOException;

public class GrowthPhaseTypeDeserializer extends StdDeserializer<GrowthPhaseType> {
    public GrowthPhaseTypeDeserializer(Class<?> vc) {
        super(vc);
    }

    public GrowthPhaseTypeDeserializer() { this(null); }

    @Override
    public GrowthPhaseType deserialize(JsonParser parser, DeserializationContext context) throws IOException {
        GrowthPhaseType result;
        String token = parser.getText();
        try {
            result = GrowthPhaseType.valueOf(token.toUpperCase());
        } catch (IllegalArgumentException e) {
            throw new IOException(String.format("Bad growth phase type \"%s\"\n", token));
        }
        return result;
    }
}
