[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 |
|
---|
[542] | 11 | @SuppressWarnings("serial")
|
---|
[519] | 12 | /**
|
---|
| 13 | * Deserializes a Value. Special deserializer is needed because by default
|
---|
| 14 | * jackson deserializes numbers as double and thus cause rounding errors.
|
---|
| 15 | */
|
---|
[526] | 16 | /*#PY
|
---|
| 17 | * from pyson.Deserializer import Deserializer
|
---|
| 18 | * from decimal import Decimal
|
---|
| 19 | *
|
---|
| 20 | * class ValueDeserializer (Deserializer):
|
---|
| 21 | * def deserialize(self, data:object, clas: object) -> 'Value':
|
---|
| 22 | * if isinstance(data, float) or isinstance(data, int):
|
---|
| 23 | * return Value(data)
|
---|
| 24 | * if isinstance(data,str):
|
---|
| 25 | * return Value(data)
|
---|
| 26 | * raise ValueError("Expected number or double quoted string but found " + str(data)
|
---|
| 27 | * + " of type " + str(type(data)))
|
---|
| 28 | */
|
---|
[519] | 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 | } |
---|