[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 |
|
---|
[786] | 63 | class A {
|
---|
[804] | 64 | private final @NonNull String test = "ds";
|
---|
[786] | 65 | }
|
---|
| 66 |
|
---|
[526] | 67 | /**
|
---|
| 68 | * A possible value for an Issue. Must be immutable and thread safe. Supported
|
---|
| 69 | * values are {@link NumberValue} and {@link DiscreteValue}. All values are just
|
---|
| 70 | * strings, a custom deserializer is used to determine which type it is.
|
---|
| 71 | *
|
---|
| 72 | * Value must be de-serializable all by itself, because it can occur plain in a
|
---|
| 73 | * Bid eg Bid["fte":0.8]
|
---|
| 74 | */
|
---|
| 75 | @JsonDeserialize(using = ValueDeserializer.class)
|
---|
| 76 | public interface Value {
|
---|
[786] | 77 |
|
---|
[526] | 78 | /**
|
---|
| 79 | * @return the contained value (String, BigDecimal)
|
---|
| 80 | */
|
---|
[804] | 81 | public @NonNull Object getValue();
|
---|
[526] | 82 |
|
---|
| 83 | } |
---|