package geniusweb.issuevalue; import java.io.IOException; import org.eclipse.jdt.annotation.NonNull; 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; @SuppressWarnings("serial") /** * Deserializes a Value. Special deserializer is needed because by default * jackson deserializes numbers as double and thus cause rounding errors. */ /*#PY * from pyson.Deserializer import Deserializer * from decimal import Decimal * * class ValueDeserializer (Deserializer): * def deserialize(self, data:object, clas: object) -> 'Value': * if isinstance(data, float) or isinstance(data, int): * from geniusweb.issuevalue.NumberValue import NumberValue * return NumberValue(data) * if isinstance(data,str): * from geniusweb.issuevalue.DiscreteValue import DiscreteValue * return DiscreteValue(data) * raise ValueError("Expected number or double quoted string but found " + str(data) * + " of type " + str(type(data))) */ 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()); } } } @JsonDeserialize(using = ValueDeserializer.class) /** * 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] */ public interface Value { @NonNull /** * @return the contained value (String, BigDecimal) */ public Object getValue(); }