source: java2python/geniuswebtranslator/geniuswebsrc/geniusweb/issuevalue/Value.java@ 770

Last change on this file since 770 was 770, checked in by wouter, 12 months ago

#169 fix #PY code

File size: 2.3 KB
Line 
1package geniusweb.issuevalue;
2
3import java.io.IOException;
4
5import com.fasterxml.jackson.core.JsonParser;
6import com.fasterxml.jackson.core.JsonProcessingException;
7import com.fasterxml.jackson.databind.DeserializationContext;
8import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
9import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
10
11@SuppressWarnings("serial")
12/**
13 * Deserializes a Value. Special deserializer is needed because by default
14 * jackson deserializes numbers as double and thus cause rounding errors.
15 */
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 * from geniusweb.issuevalue.NumberValue import NumberValue
24 * return NumberValue(data)
25 * if isinstance(data,str):
26 * from geniusweb.issuevalue.DiscreteValue import DiscreteValue
27 * return DiscreteValue(data)
28 * raise ValueError("Expected number or double quoted string but found " + str(data)
29 * + " of type " + str(type(data)))
30 */
31class 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}
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)
70public interface Value {
71 /**
72 * @return the contained value (String, BigDecimal)
73 */
74 public Object getValue();
75
76}
Note: See TracBrowser for help on using the repository browser.