Tôi có một lớp đơn giản với một nhà xây dựng tư nhân và một nhà máy tĩnh. Tôi muốn các lớp để serialize như một số, vì vậy tôi đã chú thích getter cho lĩnh vực này với @JsonValue
. Tuy nhiên, Jackson dường như thích nhà xây dựng tư nhân hơn nhà máy tĩnh, ngay cả khi tôi chú thích nhà máy tĩnh với @JsonCreator
. Nó hoạt động nếu tôi chú thích hàm tạo riêng với @JsonIgnore
, nhưng điều đó có vẻ hơi lệch.Jackson thích xây dựng tư nhân trên @JsonCreator khi deserializing một lớp học với @JsonValue
Tôi đã xem một số bài đăng cho rằng @JsonCreator
chỉ hoạt động nếu các tham số được chú thích với @JsonProperty
; tuy nhiên, điều đó có vẻ là trường hợp cho các đối tượng được tuần tự hóa thành các đối tượng JSON. Đối tượng này đang được tuần tự hóa dưới dạng một số và do đó không có thuộc tính nào để cung cấp cho chú thích.
Có điều gì tôi thiếu không?
dụ lớp:
package com.example;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.google.common.base.Preconditions;
public class NonNegative {
private final double n;
private NonNegative(double n) {
this.n = n;
}
@JsonCreator
public static NonNegative checked(double n) {
Preconditions.checkArgument(n >= 0.0);
return new NonNegative(n);
}
@JsonValue
public double getValue() {
return n;
}
@Override
public int hashCode() {
return Objects.hash(n);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof NonNegative) {
NonNegative that = (NonNegative) obj;
return Objects.equals(n, that.n);
}
return false;
}
}
dụ kiểm tra:
package com.example;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class NonNegativeTest {
private static final ObjectMapper MAPPER = new ObjectMapper();
@Test
public void itSerializesAndDeserializes() throws Exception {
NonNegative nonNegative = NonNegative.checked(0.5);
assertThat(MAPPER.readValue(MAPPER.writeValueAsString(nonNegative), NonNegative.class)).isEqualTo(nonNegative);
}
/* This test fails. */
@Test(expected = JsonMappingException.class)
public void itDoesNotDeserializeANegativeNumber() throws Exception {
MAPPER.readValue(MAPPER.writeValueAsString(-0.5), NonNegative.class);
}
}
bạn đã thử chú thích đối số của phương pháp đã chọn với @JsonProperty chưa? – wastl
Tôi có. Nó không giúp được gì. – NickAldwin
[Có thể có liên quan] (https://github.com/FasterXML/jackson-module-paranamer/issues/11). –