杰克逊的@JsonPropertyOrder 不适用于@JsonUnwrapped
Jackson's @JsonPropertyOrder doesn't work with @JsonUnwrapped
我有一个包含另一个对象属性的对象,如下所示:
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
@JsonPropertyOrder({"fA1","b","fA2"})
@Data
public class A {
private String fA1;
private String fA2;
@JsonUnwrapped
private B b = new B();
@Data
class B {
private String fB1;
private String fB2;
}
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
A a = new A ();
System.out.println(objectMapper.writeValueAsString(a));
}
}
我想要的是生成遵守此顺序的 json :
{
"fA1":"",
"fB1":"",
"fA2":"",
"fB2":""
}
有什么办法吗?
根据 this issue in the jackson-databind repository on GitHub, the @JsonPropertyOrder
annotation doesn't work with @JsonUnwrapped
注释。请参阅下面的引用:
True, unwrapped properties are not included in ordering, since they are not really known by enclosing serializer as regular properties. And specifically, as things are, will be output as a block of properties per contained, so even if known they can not be reordered separately.
Perhaps something could be done with Jackson 3.x once we get there.
但您可以考虑 解决方法:因为您似乎正在使用 Lombok,您可以使用 @Delegate
and @JsonIgnore
:
注释 b
@Data
@JsonPropertyOrder({"fa1", "fb1", "fa2", "fb2"})
public class A {
private String fA1;
private String fA2;
@Delegate
@JsonIgnore
private B b = new B();
}
@JsonIgnore
注释将确保 b
属性 不被 Jackson 序列化。
并且 @Delegate
注释将告诉 Lombok 生成将调用转发给 B
方法的委托方法。这样,A
class 将具有委托给 fB1
和 fB2
字段的 getter 和 setter 的 getter 和 setter。
我有一个包含另一个对象属性的对象,如下所示:
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
@JsonPropertyOrder({"fA1","b","fA2"})
@Data
public class A {
private String fA1;
private String fA2;
@JsonUnwrapped
private B b = new B();
@Data
class B {
private String fB1;
private String fB2;
}
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
A a = new A ();
System.out.println(objectMapper.writeValueAsString(a));
}
}
我想要的是生成遵守此顺序的 json :
{
"fA1":"",
"fB1":"",
"fA2":"",
"fB2":""
}
有什么办法吗?
根据 this issue in the jackson-databind repository on GitHub, the @JsonPropertyOrder
annotation doesn't work with @JsonUnwrapped
注释。请参阅下面的引用:
True, unwrapped properties are not included in ordering, since they are not really known by enclosing serializer as regular properties. And specifically, as things are, will be output as a block of properties per contained, so even if known they can not be reordered separately.
Perhaps something could be done with Jackson 3.x once we get there.
但您可以考虑 解决方法:因为您似乎正在使用 Lombok,您可以使用 @Delegate
and @JsonIgnore
:
b
@Data
@JsonPropertyOrder({"fa1", "fb1", "fa2", "fb2"})
public class A {
private String fA1;
private String fA2;
@Delegate
@JsonIgnore
private B b = new B();
}
@JsonIgnore
注释将确保 b
属性 不被 Jackson 序列化。
并且 @Delegate
注释将告诉 Lombok 生成将调用转发给 B
方法的委托方法。这样,A
class 将具有委托给 fB1
和 fB2
字段的 getter 和 setter 的 getter 和 setter。