|
1 | 1 | package io.github.collagid.core.api.dtos;
|
2 | 2 |
|
3 |
| -public class RecordDTO { |
4 |
| - private String id; |
| 3 | +import com.fasterxml.jackson.core.JsonParser; |
| 4 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 5 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 6 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
| 7 | +import com.fasterxml.jackson.databind.JsonNode; |
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 10 | +import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 11 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.Serializable; |
| 15 | + |
| 16 | +@JsonDeserialize(using = RecordDTO.RecordDTODeserializer.class) |
| 17 | +public class RecordDTO extends ObjectNode implements Serializable { |
| 18 | + public RecordDTO() { |
| 19 | + super(JsonNodeFactory.instance); |
| 20 | + } |
5 | 21 |
|
6 | 22 | public void setId(String id) {
|
7 |
| - this.id = id; |
| 23 | + this.put("id", id); |
| 24 | + } |
| 25 | + |
| 26 | + public String getId() { |
| 27 | + return this.path("id").asText(); |
| 28 | + } |
| 29 | + |
| 30 | + static class RecordDTODeserializer extends JsonDeserializer<RecordDTO> { |
| 31 | + |
| 32 | + @Override |
| 33 | + public RecordDTO deserialize(JsonParser p, DeserializationContext ctxt) |
| 34 | + throws IOException, JsonProcessingException { |
| 35 | + System.out.println("xxx"); |
| 36 | + ObjectMapper mapper = (ObjectMapper) p.getCodec(); |
| 37 | + JsonNode node = mapper.readTree(p); |
| 38 | + RecordDTO recordDTO = new RecordDTO(); |
| 39 | + if (node.isObject()) { |
| 40 | + ObjectNode objectNode = (ObjectNode) node; |
| 41 | + recordDTO.setAll(objectNode); |
| 42 | + } |
| 43 | + return recordDTO; |
| 44 | + } |
8 | 45 | }
|
9 | 46 | }
|
0 commit comments