1 | package geniusweb.issuevalue;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 |
|
---|
5 | import org.eclipse.jdt.annotation.NonNull;
|
---|
6 |
|
---|
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 |
|
---|
13 | @SuppressWarnings("serial")
|
---|
14 | /**
|
---|
15 | * Deserializes a Value. Special deserializer is needed because by default
|
---|
16 | * jackson deserializes numbers as double and thus cause rounding errors.
|
---|
17 | */
|
---|
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):
|
---|
25 | * from geniusweb.issuevalue.NumberValue import NumberValue
|
---|
26 | * return NumberValue(str(data))
|
---|
27 | * if isinstance(data,str):
|
---|
28 | * from geniusweb.issuevalue.DiscreteValue import DiscreteValue
|
---|
29 | * return DiscreteValue(data)
|
---|
30 | * raise ValueError("Expected number or double quoted string but found " + str(data)
|
---|
31 | * + " of type " + str(type(data)))
|
---|
32 | */
|
---|
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 | }
|
---|
62 |
|
---|
63 | @JsonDeserialize(using = ValueDeserializer.class)
|
---|
64 | /**
|
---|
65 | * A possible value for an Issue. Must be immutable and thread safe. Supported
|
---|
66 | * values are {@link NumberValue} and {@link DiscreteValue}. All values are just
|
---|
67 | * strings, a custom deserializer is used to determine which type it is.
|
---|
68 | *
|
---|
69 | * Value must be de-serializable all by itself, because it can occur plain in a
|
---|
70 | * Bid eg Bid["fte":0.8]
|
---|
71 | */
|
---|
72 | public interface Value {
|
---|
73 | @NonNull
|
---|
74 | /**
|
---|
75 | * @return the contained value (String, BigDecimal)
|
---|
76 | */
|
---|
77 | public Object getValue();
|
---|
78 |
|
---|
79 | } |
---|