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

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

#125 worked around Value - ValueDeserializer circular reference issue by moving the ValueDeserializer code up and using 'Value' in the forward reference of the manual translation code.

File size: 2.9 KB
Line 
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/*
16 * This must be defined first, because in python the ValueDeserializer
17 * must be able to refer to this.
18 * This deserializer also needs to refer to the Value class,
19 * which is not yet defined. We therefore use 'Value'.
20 */
21/*#PY
22 * from pyson.Deserializer import Deserializer
23 * from decimal import Decimal
24 *
25 * class ValueDeserializer (Deserializer):
26 * def deserialize(self, data:object, clas: object) -> 'Value':
27 * if isinstance(data, float) or isinstance(data, int):
28 * return Value(Decimal(str(data)))
29 * if isinstance(data,str):
30 * return Value(data)
31 * raise ValueError("Expected number or double quoted string but found " + str(data)
32 * + " of type " + str(type(data)))
33 */
34class ValueDeserializer extends StdDeserializer<Value> {
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 Value(Float.parseFloat(text));
51 case VALUE_STRING:
52 return new Value(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)
64public class Value {
65 private final Object value;
66
67 public Value(Object value) {
68 this.value = value;
69 }
70
71 public Object getValue() {
72 return value;
73 }
74
75 /** The test code for Value */
76 public static void main(String[] args) throws JsonProcessingException {
77 ObjectMapper jackson = new ObjectMapper();
78 Value val = new Value((float) 12);
79 System.out.println(jackson.writeValueAsString(val));
80 assertEquals(val, jackson.readValue("12", Value.class));
81 val = new Value("hallo");
82 System.out.println(jackson.writeValueAsString(val));
83 assertEquals(val, jackson.readValue("\"hallo\"", Value.class));
84 }
85
86 @Override
87 public int hashCode() {
88 return Objects.hash(value);
89 }
90
91 @Override
92 public boolean equals(Object obj) {
93 if (this == obj)
94 return true;
95 if (obj == null)
96 return false;
97 if (getClass() != obj.getClass())
98 return false;
99 Value other = (Value) obj;
100 return Objects.equals(value, other.value);
101 }
102
103 @Override
104 public String toString() {
105 return "val:" + value;
106 }
107}
Note: See TracBrowser for help on using the repository browser.