Jackson中用于具有一个字段的类/记录/值对象的平面JSON

0kjbasz6  于 2022-11-08  发布在  其他
关注(0)|答案(1)|浏览(180)

我有一个Java记录,其中只有一个字段:

  1. public record AggregateId(UUID id) {}

和一个具有AggregateId字段的类(为了可读性,删除了其他字段)

  1. public class Aggregate {
  2. public final AggregateId aggregateId;
  3. @JsonCreator
  4. public Aggregate(
  5. @JsonProperty("aggregateId") AggregateId aggregateId
  6. ) {
  7. this.aggregateId = aggregateId;
  8. }
  9. }

上面的实现使用给定的示例对JSON进行序列化和反序列化:

  1. ObjectMapper objectMapper = new ObjectMapper();
  2. String content = """
  3. {
  4. "aggregateId": {
  5. "id": "3f61aede-83dd-4049-a6ff-337887b6b807"
  6. }
  7. }
  8. """;
  9. Aggregate aggregate = objectMapper.readValue(content, Aggregate.class);
  10. System.out.println(objectMapper.writeValueAsString(aggregate));

我如何更改Jackson配置以替换JSON:

  1. {
  2. "aggregateId": "3f61aede-83dd-4049-a6ff-337887b6b807"
  3. }

而不放弃AggregateId的单独类和通过字段访问,不使用getter?
我尝试了@JsonUnwrapper注解,但这导致了抛出

  1. Exception in thread "X" com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
  2. Invalid type definition for type `X`:
  3. Cannot define Creator parameter as `@JsonUnwrapped`: combination not yet supported at [Source: (String)"{
  4. "aggregateId": "3f61aede-83dd-4049-a6ff-337887b6b807"
  5. }"

  1. Exception in thread "X" com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
  2. Cannot define Creator property "aggregateId" as `@JsonUnwrapped`:
  3. combination not yet supported at [Source: (String)"{
  4. "aggregateId": "3f61aede-83dd-4049-a6ff-337887b6b807"
  5. }"
  • Jackson版本:第2.13.1节 *
  1. dependencies {
  2. compile "com.fasterxml.jackson.core:jackson-annotations:2.13.1"
  3. compile "com.fasterxml.jackson.core:jackson-databind:2.13.1"
  4. }

当然,使用自定义的序列化程序/反序列化程序也是可以的,但我正在寻找一种更简单的解决方案,因为我有许多不同的类都有类似的问题。

q9yhzks0

q9yhzks01#

尚不支持@JsonUnwrapped@JsonCreator的组合,因此我们可以生成如下解决方案:

  1. import com.fasterxml.jackson.annotation.JsonCreator;
  2. import com.fasterxml.jackson.annotation.JsonProperty;
  3. import com.fasterxml.jackson.annotation.JsonUnwrapped;
  4. import com.fasterxml.jackson.core.JsonProcessingException;
  5. import com.fasterxml.jackson.databind.ObjectMapper;
  6. import com.fasterxml.jackson.databind.SerializationFeature;
  7. import java.util.UUID;
  8. public class AggregateTest {
  9. static record AggregateId(@JsonProperty("aggregateId") UUID id) {}
  10. static class Aggregate {
  11. @JsonUnwrapped
  12. @JsonProperty(access = JsonProperty.Access.READ_ONLY)
  13. public final AggregateId _aggregateId;
  14. public final String otherField;
  15. @JsonCreator
  16. public Aggregate(@JsonProperty("aggregateId") UUID aggregateId,
  17. @JsonProperty("otherField") String otherField) {
  18. this._aggregateId = new AggregateId(aggregateId);
  19. this.otherField = otherField;
  20. }
  21. }
  22. public static void main(String[] args) throws JsonProcessingException {
  23. String rawJson =
  24. "{\"aggregateId\": \"1f61aede-83dd-4049-a6ff-337887b6b807\"," +
  25. "\"otherField\": \"İsmail Y.\"}";
  26. ObjectMapper objectMapper = new ObjectMapper();
  27. objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
  28. Aggregate aggregate = objectMapper
  29. .readValue(rawJson, Aggregate.class);
  30. System.out.println(objectMapper
  31. .writeValueAsString(aggregate));
  32. }
  33. }

这里我们暂时去掉@JsonUnwrapped字段。
我们获取名为aggregateIdUUID,并创建一条**AggregateId记录**。
关于它的详细说明:

展开查看全部

相关问题