[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 | * Deserializes a Value. Special deserializer is needed because by default
|
---|
| 13 | * jackson deserializes numbers as double and thus cause rounding errors.
|
---|
| 14 | */
|
---|
[526] | 15 | /*#PY
|
---|
| 16 | * from pyson.Deserializer import Deserializer
|
---|
| 17 | * from decimal import Decimal
|
---|
| 18 | *
|
---|
| 19 | * class ValueDeserializer (Deserializer):
|
---|
| 20 | * def deserialize(self, data:object, clas: object) -> 'Value':
|
---|
| 21 | * if isinstance(data, float) or isinstance(data, int):
|
---|
| 22 | * return Value(data)
|
---|
| 23 | * if isinstance(data,str):
|
---|
| 24 | * return Value(data)
|
---|
| 25 | * raise ValueError("Expected number or double quoted string but found " + str(data)
|
---|
| 26 | * + " of type " + str(type(data)))
|
---|
| 27 | */
|
---|
[519] | 28 | @SuppressWarnings("serial")
|
---|
| 29 | class ValueDeserializer extends StdDeserializer<Value> {
|
---|
| 30 |
|
---|
| 31 | public ValueDeserializer() {
|
---|
| 32 | this(null);
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | public ValueDeserializer(Class<?> vc) {
|
---|
| 36 | super(vc);
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | @Override
|
---|
| 40 | public Value deserialize(JsonParser jp, DeserializationContext ctxt)
|
---|
| 41 | throws IOException, JsonProcessingException {
|
---|
| 42 | String text = jp.getText();
|
---|
| 43 | switch (jp.currentToken()) {
|
---|
| 44 | case VALUE_NUMBER_FLOAT:
|
---|
| 45 | case VALUE_NUMBER_INT:
|
---|
| 46 | return new NumberValue(text);
|
---|
| 47 | case VALUE_STRING:
|
---|
| 48 | return new DiscreteValue(text);
|
---|
| 49 | default:
|
---|
| 50 | throw new IOException(
|
---|
| 51 | "Expected number of double quoted string but found " + text
|
---|
| 52 | + " of type " + jp.currentToken());
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | }
|
---|
[526] | 58 |
|
---|
| 59 | /**
|
---|
| 60 | * A possible value for an Issue. Must be immutable and thread safe. Supported
|
---|
| 61 | * values are {@link NumberValue} and {@link DiscreteValue}. All values are just
|
---|
| 62 | * strings, a custom deserializer is used to determine which type it is.
|
---|
| 63 | *
|
---|
| 64 | * Value must be de-serializable all by itself, because it can occur plain in a
|
---|
| 65 | * Bid eg Bid["fte":0.8]
|
---|
| 66 | */
|
---|
| 67 | @JsonDeserialize(using = ValueDeserializer.class)
|
---|
| 68 | public interface Value {
|
---|
| 69 | /**
|
---|
| 70 | * @return the contained value (String, BigDecimal)
|
---|
| 71 | */
|
---|
| 72 | public Object getValue();
|
---|
| 73 |
|
---|
| 74 | } |
---|