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