簡體   English   中英

傑克遜解開/包裹對象

[英]Jackson Unwrap / Wrap Object

我有一個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;

}

我有mixins,但為了簡潔,在這個例子中添加了Annotations。 序列化為JSON時這很有用,問題是當我嘗試反序列化時,如果存在一些@JsonWrapped注釋,它可能會起作用。

簡而言之,我正在嘗試使用ArangoDB,我可以創建/讀取文檔,但我需要使用自己的Value Objects,不幸的是我不能將鍵用作String,它由OrderKey封裝。 @Value注釋來自@Value項目。

有沒有辦法實現這個目標?

您可以嘗試在使用@JsonCreator注釋的class A定義構造@JsonCreator 然后,Jackson可以使用此構造函數創建A對象,並將您希望在JSON文檔中的字段映射到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此構造函數將阻止創建@AllArgsConstructor隱含的@AllArgsConstructor構造@Value

也可以使用Java 8和一些額外的模塊來避免構造函數注釋。 檢查煤礦例如對方的回答。

我最終在mixin本身內進行了序列化/反序列化,這樣我可以避免使用@JsonUnwrapped注釋和另一個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 );
  }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM