001package org.gbif.dwc.terms.jackson;
002
003import org.gbif.dwc.terms.Term;
004import org.gbif.dwc.terms.TermFactory;
005
006import java.io.IOException;
007
008import org.codehaus.jackson.JsonParser;
009import org.codehaus.jackson.JsonProcessingException;
010import org.codehaus.jackson.JsonToken;
011import org.codehaus.jackson.map.DeserializationContext;
012import org.codehaus.jackson.map.JsonDeserializer;
013
014/**
015 * A json deserializer that turns full qualified term names into dwc terms instances.
016 */
017public class TermDeserializer extends JsonDeserializer<Term> {
018  private TermFactory factory = TermFactory.instance();
019
020  @Override
021  public Term deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
022    if (jp.getCurrentToken() == JsonToken.VALUE_STRING) {
023      return factory.findTerm(jp.getText());
024    }
025    throw ctxt.mappingException("Expected JSON String");
026  }
027}