source: java2python/src/test/java/testcode/PersonJson.java@ 370

Last change on this file since 370 was 370, checked in by wouter, 2 years ago

#110 fixed few issues. Closing this: all works now using targz files.

File size: 2.1 KB
Line 
1package testcode;
2
3import java.util.Objects;
4
5import com.fasterxml.jackson.annotation.JsonAutoDetect;
6import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
7import com.fasterxml.jackson.annotation.JsonCreator;
8import com.fasterxml.jackson.annotation.JsonProperty;
9import com.fasterxml.jackson.annotation.JsonTypeInfo;
10import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
11import com.fasterxml.jackson.core.JsonProcessingException;
12import com.fasterxml.jackson.databind.ObjectMapper;
13
14/**
15 * jackson-serializable Person, also with extra age field and equals and
16 * hashcode
17 */
18@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.WRAPPER_OBJECT)
19@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
20public class PersonJson {
21 // when private, python can't access them.
22 protected static final ObjectMapper jackson = new ObjectMapper();
23 protected static final String serialized = "{\"PersonJson\":{\"name\":\"Kees\",\"age\":25}}";
24
25 private final String name;
26 private Integer age;
27
28 @JsonCreator
29 public PersonJson(@JsonProperty("name") String name,
30 @JsonProperty("age") Integer age) {
31 this.name = name;
32 this.age = age;
33 }
34
35 @Override
36 public String toString() {
37 return "Person[" + name.toString() + "," + age.toString() + "]";
38 }
39
40 public String getName() {
41 return name;
42 }
43
44 public Integer getAge() {
45 return age;
46 }
47
48 @Override
49 public int hashCode() {
50 return Objects.hash(age, name);
51 }
52
53 @Override
54 public boolean equals(Object obj) {
55 if (this == obj)
56 return true;
57 if (obj == null)
58 return false;
59 if (getClass() != obj.getClass())
60 return false;
61 PersonJson other = (PersonJson) obj;
62 return Objects.equals(age, other.age)
63 && Objects.equals(name, other.name);
64 }
65
66 static public void main(String[] args) throws JsonProcessingException {
67 PersonJson person = new PersonJson("Kees", 25);
68 System.out.println(person);
69 System.out.println(jackson.writeValueAsString(person));
70 System.out.println(
71 person.equals(jackson.readValue(serialized, PersonJson.class)));
72 /*-
73 Person[Kees,25]
74 {"PersonJson":{"name":"Kees","age":25}}
75 true
76 */
77 }
78}
Note: See TracBrowser for help on using the repository browser.