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

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

#161 PyProgram.fromDirectory introduced to clean up test code. Moved test programs to separate folders each.

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