package geniusweb.issuevalue; import java.io.IOException; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.deser.std.StdDeserializer; /** * A possible value for an Issue. Must be immutable and thread safe. Supported * values are {@link NumberValue} and {@link DiscreteValue}. All values are just * strings, a custom deserializer is used to determine which type it is. * * Value must be de-serializable all by itself, because it can occur plain in a * Bid eg Bid["fte":0.8] */ @JsonDeserialize(using = ValueDeserializer.class) public interface Value { /** * @return the contained value (String, BigDecimal) */ public Object getValue(); } /** * Deserializes a Value. Special deserializer is needed because by default * jackson deserializes numbers as double and thus cause rounding errors. */ @SuppressWarnings("serial") class ValueDeserializer extends StdDeserializer { public ValueDeserializer() { this(null); } public ValueDeserializer(Class vc) { super(vc); } @Override public Value deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { String text = jp.getText(); switch (jp.currentToken()) { case VALUE_NUMBER_FLOAT: case VALUE_NUMBER_INT: return new NumberValue(text); case VALUE_STRING: return new DiscreteValue(text); default: throw new IOException( "Expected number of double quoted string but found " + text + " of type " + jp.currentToken()); } } }