package ch.zhaw.gartenverwaltung.json;

import ch.zhaw.gartenverwaltung.types.HardinessZone;
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 HardinessZoneDeserializer extends StdDeserializer<HardinessZone> {
    public HardinessZoneDeserializer(Class<?> vc) {
        super(vc);
    }
    public HardinessZoneDeserializer() {
        this(null);
    }

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