使用Jackson自定义反序列化操作(Custom Deserialization in Jackson)

Myth丶恋晨 2024-03-23 17:49 209阅读 0赞

目录

  • Maven依赖
  • Standard Deserialization
  • Custom Deserializer on ObjectMapper
  • Custom Deserializer on the Class
  • Custom Deserializer for a Generic Type

Maven依赖

  1. <dependencies>
  2. <!-- yaml -->
  3. <dependency>
  4. <groupId>com.fasterxml.jackson.core</groupId>
  5. <artifactId>jackson-databind</artifactId>
  6. <version>2.14.2</version>
  7. <exclusions>
  8. <exclusion>
  9. <artifactId>jackson-annotations</artifactId>
  10. <groupId>com.fasterxml.jackson.core</groupId>
  11. </exclusion>
  12. <exclusion>
  13. <artifactId>jackson-core</artifactId>
  14. <groupId>com.fasterxml.jackson.core</groupId>
  15. </exclusion>
  16. </exclusions>
  17. </dependency>
  18. <dependency>
  19. <groupId>com.fasterxml.jackson.dataformat</groupId>
  20. <artifactId>jackson-dataformat-yaml</artifactId>
  21. <version>2.14.2</version>
  22. <exclusions>
  23. <exclusion>
  24. <artifactId>jackson-core</artifactId>
  25. <groupId>com.fasterxml.jackson.core</groupId>
  26. </exclusion>
  27. <exclusion>
  28. <artifactId>jackson-databind</artifactId>
  29. <groupId>com.fasterxml.jackson.core</groupId>
  30. </exclusion>
  31. <exclusion>
  32. <artifactId>snakeyaml</artifactId>
  33. <groupId>org.yaml</groupId>
  34. </exclusion>
  35. </exclusions>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.yaml</groupId>
  39. <artifactId>snakeyaml</artifactId>
  40. <version>2.0</version>
  41. </dependency>
  42. <!--Json相关-->
  43. <dependency>
  44. <groupId>com.fasterxml.jackson.core</groupId>
  45. <artifactId>jackson-core</artifactId>
  46. <version>2.14.2</version>
  47. </dependency>
  48. <dependency>
  49. <groupId>com.fasterxml.jackson.core</groupId>
  50. <artifactId>jackson-annotations</artifactId>
  51. <version>2.15.1</version>
  52. </dependency>

Standard Deserialization

  1. @Data
  2. @NoArgsConstructor
  3. @AllArgsConstructor
  4. public class Item {
  5. public int id;
  6. public String itemName;
  7. public User owner;
  8. }
  9. String json = "{\n" +
  10. " \"id\": 1,\n" +
  11. " \"itemName\": \"theItem\",\n" +
  12. " \"owner\": {\n" +
  13. " \"id\": 2,\n" +
  14. " \"name\": \"theUser\"\n" +
  15. " }\n" +
  16. "}";
  17. Item itemWithOwner = new ObjectMapper().readValue(json, Item.class);
  18. System.out.println(itemWithOwner);
  19. 结果
  20. Item(id=1, itemName=theItem, owner=User(id=2, name=theUser))

Custom Deserializer on ObjectMapper

  1. @Data
  2. @NoArgsConstructor
  3. @AllArgsConstructor
  4. public class User {
  5. public int id;
  6. public String name;
  7. }
  8. public class ItemDeserializer extends StdDeserializer<Item> {
  9. public ItemDeserializer() {
  10. this(null);
  11. }
  12. public ItemDeserializer(Class<?> vc) {
  13. super(vc);
  14. }
  15. @Override
  16. public Item deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
  17. JsonNode jsonNode = jsonParser.getCodec().readTree(jsonParser);
  18. int id = (Integer) ((IntNode) jsonNode.get("id")).numberValue();
  19. String itemName = jsonNode.get("itemName").asText();
  20. int userId = (Integer) ((IntNode) jsonNode.get("createdBy")).numberValue();
  21. return new Item(id, itemName, new User(userId, null));
  22. }
  23. }
  24. String json = "{\n" +
  25. " \"id\": 1,\n" +
  26. " \"itemName\": \"theItem\",\n" +
  27. " \"createdBy\": 2\n" +
  28. "}";
  29. ObjectMapper mapper = new ObjectMapper();
  30. SimpleModule module = new SimpleModule();
  31. module.addDeserializer(Item.class, new ItemDeserializer());
  32. mapper.registerModule(module);
  33. Item readValue = mapper.readValue(json, Item.class);
  34. System.out.println(readValue);
  35. 结果
  36. Item(id=1, itemName=theItem, owner=User(id=2, name=null))

Custom Deserializer on the Class

在上文ItemDeserializer的基础上

  1. @Data
  2. @NoArgsConstructor
  3. @AllArgsConstructor
  4. @JsonDeserialize(using = ItemDeserializer.class)
  5. public class Item {
  6. public int id;
  7. public String itemName;
  8. public User owner;
  9. }
  10. String json = "{\n" +
  11. " \"id\": 1,\n" +
  12. " \"itemName\": \"theItem\",\n" +
  13. " \"createdBy\": 2\n" +
  14. "}";
  15. Item itemWithOwner = new ObjectMapper().readValue(json, Item.class);
  16. System.out.println(itemWithOwner);
  17. // Item(id=1, itemName=theItem, owner=User(id=2, name=null))

Custom Deserializer for a Generic Type

  1. @Data
  2. @NoArgsConstructor
  3. @AllArgsConstructor
  4. public class Item {
  5. public int id;
  6. public String itemName;
  7. public Wrapper<User> owner;
  8. }
  9. @Data
  10. @NoArgsConstructor
  11. @AllArgsConstructor
  12. public class User {
  13. public int id;
  14. public String name;
  15. }
  16. @Data
  17. public class Wrapper<T> {
  18. T value;
  19. public T getValue() {
  20. return value;
  21. }
  22. public void setValue(T value) {
  23. this.value = value;
  24. }
  25. }
  26. public class WrapperDeserializer extends JsonDeserializer<Wrapper<?>> implements ContextualDeserializer {
  27. private JavaType type;
  28. @Override
  29. public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) {
  30. this.type = property.getType().containedType(0);
  31. return this;
  32. }
  33. @Override
  34. public Wrapper<?> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, IOException {
  35. Wrapper<?> wrapper = new Wrapper<>();
  36. wrapper.setValue(deserializationContext.readValue(jsonParser, type));
  37. return wrapper;
  38. }
  39. }
  40. ObjectMapper mapper = new ObjectMapper();
  41. SimpleModule module = new SimpleModule();
  42. module.addDeserializer(Wrapper.class, new WrapperDeserializer());
  43. mapper.registerModule(module);
  44. String json = "{\n" +
  45. "\"id\": 1,\n" +
  46. "\"itemName\": \"theItem\",\n" +
  47. "\"owner\": {\n" +
  48. " \"id\": 2,\n" +
  49. " \"name\": \"theUser\"\n" +
  50. "}\n" +
  51. "}";
  52. Item readValue = mapper.readValue(json, Item.class);
  53. System.out.println(readValue);
  54. Item(id=1, itemName=theItem, owner=Wrapper(value=User(id=2, name=theUser)))

-——————————————————————————————————————读书笔记摘自 文章:
Getting Started with Custom Deserialization in Jackson

发表评论

表情:
评论列表 (有 0 条评论,209人围观)

还没有评论,来说两句吧...

相关阅读