[346] | 1 | package testcode;
|
---|
| 2 |
|
---|
| 3 | import java.util.Objects;
|
---|
| 4 |
|
---|
| 5 | import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
---|
| 6 | import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
---|
| 7 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
| 8 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
| 9 | import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
---|
| 10 | import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
|
---|
| 11 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
| 12 | import 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)
|
---|
| 20 | public class PersonJson {
|
---|
[370] | 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}}";
|
---|
[346] | 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() {
|
---|
[369] | 37 | return "Person[" + name.toString() + "," + age.toString() + "]";
|
---|
[346] | 38 | }
|
---|
| 39 |
|
---|
[370] | 40 | public String getName() {
|
---|
| 41 | return name;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | public Integer getAge() {
|
---|
| 45 | return age;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[346] | 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);
|
---|
[370] | 68 | System.out.println(person);
|
---|
[346] | 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 | }
|
---|