[519] | 1 | package geniusweb.issuevalue;
|
---|
| 2 |
|
---|
| 3 | import java.io.IOException;
|
---|
| 4 |
|
---|
| 5 | import com.fasterxml.jackson.core.JsonParser;
|
---|
| 6 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
| 7 | import com.fasterxml.jackson.databind.DeserializationContext;
|
---|
| 8 | import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
---|
| 9 | import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
---|
| 10 |
|
---|
| 11 | /**
|
---|
| 12 | * A possible value for an Issue. Must be immutable and thread safe. Supported
|
---|
| 13 | * values are {@link NumberValue} and {@link DiscreteValue}. All values are just
|
---|
| 14 | * strings, a custom deserializer is used to determine which type it is.
|
---|
| 15 | *
|
---|
| 16 | * Value must be de-serializable all by itself, because it can occur plain in a
|
---|
| 17 | * Bid eg Bid["fte":0.8]
|
---|
| 18 | */
|
---|
| 19 | @JsonDeserialize(using = ValueDeserializer.class)
|
---|
| 20 | public interface Value {
|
---|
| 21 | /**
|
---|
| 22 | * @return the contained value (String, BigDecimal)
|
---|
| 23 | */
|
---|
| 24 | public Object getValue();
|
---|
| 25 |
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | /**
|
---|
| 29 | * Deserializes a Value. Special deserializer is needed because by default
|
---|
| 30 | * jackson deserializes numbers as double and thus cause rounding errors.
|
---|
| 31 | */
|
---|
| 32 | @SuppressWarnings("serial")
|
---|
| 33 | class ValueDeserializer extends StdDeserializer<Value> {
|
---|
| 34 |
|
---|
| 35 | public ValueDeserializer() {
|
---|
| 36 | this(null);
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | public ValueDeserializer(Class<?> vc) {
|
---|
| 40 | super(vc);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | @Override
|
---|
| 44 | public Value deserialize(JsonParser jp, DeserializationContext ctxt)
|
---|
| 45 | throws IOException, JsonProcessingException {
|
---|
| 46 | String text = jp.getText();
|
---|
| 47 | switch (jp.currentToken()) {
|
---|
| 48 | case VALUE_NUMBER_FLOAT:
|
---|
| 49 | case VALUE_NUMBER_INT:
|
---|
| 50 | return new NumberValue(text);
|
---|
| 51 | case VALUE_STRING:
|
---|
| 52 | return new DiscreteValue(text);
|
---|
| 53 | default:
|
---|
| 54 | throw new IOException(
|
---|
| 55 | "Expected number of double quoted string but found " + text
|
---|
| 56 | + " of type " + jp.currentToken());
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | }
|
---|