Jackson展开/环绕对象

q5iwbnjs  于 7个月前  发布在  其他
关注(0)|答案(2)|浏览(66)

我有一个Spring Boot 项目,我有一个这样的类:

@Value
public class A {

  @JsonUnwrapped
  OrderKey key;

  String description;

  B b;
  
  @Value
  public static class B {
    String description;
  }
}

@Value
public class OrderKey {

  @JsonProperty( "_key" )
  String id;
}

我有mixin,但为了简洁起见,在这个例子中添加了Annotations。当序列化为JSON时,这很好用,问题是当我试图重新序列化时,如果存在一些@JsonWrapped注解,它可能会工作。
简而言之,我试图在rest中使用ArangoDB,我可以创建/读取文档,但我需要使用自己的值对象,不幸的是,我不能将键作为String使用,它被OrderKey封装。@Value注解来自lombok项目。
有没有办法做到这一点?

uelo1irk

uelo1irk1#

我最终在mixin本身内部进行序列化/重复化,这样我就可以避免@JsonUnwrapped注解和另一个Key的mixin。
Mixin:

public class OrderMixin {

    @JsonDeserialize( using = OrderKeyDeserializer.class )
    @JsonSerialize( using = OrderKeySerializer.class )
    @JsonProperty( "_key" )
    OrderKey key;

    @JsonProperty( "description" )
    String description;

    @JsonProperty( "amount" )
    String amount;

    @JsonProperty( "operation" )
    Order.Operation operation;

    @JsonProperty( "creationDate" )
    LocalDateTime creationDate;

    public static class OrderKeySerializer extends JsonSerializer<OrderKey> {

        public OrderKeySerializer() {
            super( OrderKey.class );
        }

        @Override
        public void serialize( OrderKey value, JsonGenerator gen, SerializerProvider provider ) throws IOException {
            gen.writeString( value.getOrderId() );
        }
    }

    public static class OrderKeyDeserializer extends JsonDeserializer<OrderKey> {

        public OrderKeyDeserializer() {
            super( OrderKey.class );
        }

        @Override
        public OrderKey deserialize( JsonParser jsonParser, DeserializationContext context ) throws IOException {
            JsonNode node = jsonParser.getCodec().readTree( jsonParser );

            return OrderKey.get( node.asText() );
        }
    }

}

价值对象:

@Value
public class Order implements Serializable {

  private static final long serialVersionUID = 901109456762331944L;

  OrderKey key;

  String description;

  String amount;

  Operation operation;

  LocalDateTime creationDate;

  @Value
  public static class Operation {

    String id;

    String description;

    String status;

    LocalDateTime creationDate;
  }

}

@Value
public class OrderKey implements Serializable {

  private static final long serialVersionUID = -8102116676316181864L;

  private String orderId;

  public static OrderKey get( String orderId ) {
      return new OrderKey( orderId );
  }

}
r6l8ljro

r6l8ljro2#

您可以尝试在@JsonCreator中定义一个带@JsonCreator注解的构造函数。然后,Jackson可以使用这个构造函数创建一个A对象,并将JSON文档中期望的字段Map到A的字段。简化示例:

@Value
public class A {

    @JsonUnwrapped
    OrderKey key;

    String description;

    @JsonCreator
    public A(@JsonProperty("key") String key,
             @JsonProperty("description") String description) {
        this.key = new OrderKey(key);
        this.description = description;
    }
}

请注意,A的这个构造函数将阻止创建@Value所隐含的@AllArgsConstructor构造函数。
Java 8和一些额外的模块也可以避免构造函数注解。例如,检查this我的其他答案。

相关问题