source: java2python/jackson-t/src/test/java/testcode/Value.java@ 465

Last change on this file since 465 was 465, checked in by wouter, 17 months ago

#125 Fix @JsonDeserialize annotation translator. Small fix in indentation of test code

File size: 2.6 KB
RevLine 
[418]1package testcode;
2
3import static org.junit.Assert.assertEquals;
4
5import java.io.IOException;
6import java.util.Objects;
7
8import com.fasterxml.jackson.core.JsonParser;
9import com.fasterxml.jackson.core.JsonProcessingException;
10import com.fasterxml.jackson.databind.DeserializationContext;
11import com.fasterxml.jackson.databind.ObjectMapper;
12import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
13import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
14
15@JsonDeserialize(using = ValueDeserializer.class)
16public class Value {
17 private final Object value;
18
19 public Value(Object value) {
20 this.value = value;
21 }
22
23 public Object getValue() {
24 return value;
25 }
26
27 /** The test code for Value */
28 public static void main(String[] args) throws JsonProcessingException {
29 ObjectMapper jackson = new ObjectMapper();
30 Value val = new Value((float) 12);
31 System.out.println(jackson.writeValueAsString(val));
32 assertEquals(val, jackson.readValue("12", Value.class));
33 val = new Value("hallo");
34 System.out.println(jackson.writeValueAsString(val));
35 assertEquals(val, jackson.readValue("\"hallo\"", Value.class));
36 }
37
38 @Override
39 public int hashCode() {
40 return Objects.hash(value);
41 }
42
43 @Override
44 public boolean equals(Object obj) {
45 if (this == obj)
46 return true;
47 if (obj == null)
48 return false;
49 if (getClass() != obj.getClass())
50 return false;
51 Value other = (Value) obj;
52 return Objects.equals(value, other.value);
53 }
54
55 @Override
56 public String toString() {
57 return "val:" + value;
58 }
59}
60
[465]61/*#PY
62 * class ValueDeserializer (Deserializer):
63 * def deserialize(self, data:object, clas: object) -> Value:
64 * if isinstance(data, float) or isinstance(data, int):
65 * return NumberValue(Decimal(str(data)))
66 * if isinstance(data,str):
67 * return DiscreteValue(data)
68 * raise ValueError("Expected number or double quoted string but found " + str(data)
69 * + " of type " + str(type(data)))
70 */
[418]71class ValueDeserializer extends StdDeserializer<Value> {
72 public ValueDeserializer() {
73 this(null);
74 }
75
76 public ValueDeserializer(Class<?> vc) {
77 super(vc);
78 }
79
80 @Override
81 public Value deserialize(JsonParser jp, DeserializationContext ctxt)
82 throws IOException, JsonProcessingException {
83 String text = jp.getText();
84 switch (jp.currentToken()) {
85 case VALUE_NUMBER_FLOAT:
86 case VALUE_NUMBER_INT:
87 return new Value(Float.parseFloat(text));
88 case VALUE_STRING:
89 return new Value(text);
90 default:
91 throw new IOException(
92 "Expected number of double quoted string but found " + text
93 + " of type " + jp.currentToken());
94 }
95
96 }
97
98}
Note: See TracBrowser for help on using the repository browser.