[519] | 1 | package geniusweb.issuevalue;
|
---|
| 2 |
|
---|
| 3 | import java.io.IOException;
|
---|
| 4 |
|
---|
[804] | 5 | import org.eclipse.jdt.annotation.NonNull;
|
---|
[786] | 6 |
|
---|
[519] | 7 | import com.fasterxml.jackson.core.JsonParser;
|
---|
| 8 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
| 9 | import com.fasterxml.jackson.databind.DeserializationContext;
|
---|
| 10 | import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
---|
| 11 | import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
---|
| 12 |
|
---|
[542] | 13 | @SuppressWarnings("serial")
|
---|
[519] | 14 | /**
|
---|
| 15 | * Deserializes a Value. Special deserializer is needed because by default
|
---|
| 16 | * jackson deserializes numbers as double and thus cause rounding errors.
|
---|
| 17 | */
|
---|
[526] | 18 | /*#PY
|
---|
| 19 | * from pyson.Deserializer import Deserializer
|
---|
| 20 | * from decimal import Decimal
|
---|
| 21 | *
|
---|
| 22 | * class ValueDeserializer (Deserializer):
|
---|
| 23 | * def deserialize(self, data:object, clas: object) -> 'Value':
|
---|
| 24 | * if isinstance(data, float) or isinstance(data, int):
|
---|
[770] | 25 | * from geniusweb.issuevalue.NumberValue import NumberValue
|
---|
| 26 | * return NumberValue(data)
|
---|
[526] | 27 | * if isinstance(data,str):
|
---|
[770] | 28 | * from geniusweb.issuevalue.DiscreteValue import DiscreteValue
|
---|
| 29 | * return DiscreteValue(data)
|
---|
[526] | 30 | * raise ValueError("Expected number or double quoted string but found " + str(data)
|
---|
| 31 | * + " of type " + str(type(data)))
|
---|
| 32 | */
|
---|
[519] | 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 | }
|
---|
[526] | 62 |
|
---|
| 63 | /**
|
---|
| 64 | * A possible value for an Issue. Must be immutable and thread safe. Supported
|
---|
| 65 | * values are {@link NumberValue} and {@link DiscreteValue}. All values are just
|
---|
| 66 | * strings, a custom deserializer is used to determine which type it is.
|
---|
| 67 | *
|
---|
| 68 | * Value must be de-serializable all by itself, because it can occur plain in a
|
---|
| 69 | * Bid eg Bid["fte":0.8]
|
---|
| 70 | */
|
---|
| 71 | @JsonDeserialize(using = ValueDeserializer.class)
|
---|
| 72 | public interface Value {
|
---|
[786] | 73 |
|
---|
[526] | 74 | /**
|
---|
| 75 | * @return the contained value (String, BigDecimal)
|
---|
| 76 | */
|
---|
[804] | 77 | public @NonNull Object getValue();
|
---|
[526] | 78 |
|
---|
| 79 | } |
---|