如何将字符串列表反序列化为单个 arg 值 class?
How to deserialize a List of String to a single arg value class?
基本上我总是想将我的 Id class 解包到父对象,但是如果是 List<> 我不能使用 jackson 库中的 JsonUnwrapped Annotation。
@lombok.Value
public class Response {
List<MyId> ids;
// ... other fields
}
@lombok.Value
public class MyId {
String id;
}
{
"ids": ["id1", "id2"]
"otherField": {}
}
Working solution with jackson-databind 2.11
@lombok.Value
public class MyId {
@JsonValue
String id;
public MyId(final String id) {
this.id = id;
}
}
您可以使用 @JsonValue
。来自 docs:
Marker annotation that indicates that the value of annotated accessor (either field or "getter" method [a method with non-void return type, no args]) is to be used as the single value to serialize for the instance, instead of the usual method of collecting properties of value. Usually value will be of a simple scalar type (String or Number), but it can be any serializable type (Collection, Map or Bean).
用法:
@Value
public class MyId {
@JsonValue
String id;
}
完整代码:
public class JacksonExample {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
List<MyId> myIds = new ArrayList<>();
MyId id1 = new MyId("one");
MyId id2 = new MyId("two");
myIds.add(id1);
myIds.add(id2);
Response response = new Response(myIds, "some other field value");
System.out.println(objectMapper.writeValueAsString(response));
}
}
@Value
class Response {
List<MyId> ids;
String otherField;
}
@Value
class MyId {
@JsonValue
String id;
}
输出:
{
"ids": [
"one",
"two"
],
"otherField": "some other field value"
}
基本上我总是想将我的 Id class 解包到父对象,但是如果是 List<> 我不能使用 jackson 库中的 JsonUnwrapped Annotation。
@lombok.Value
public class Response {
List<MyId> ids;
// ... other fields
}
@lombok.Value
public class MyId {
String id;
}
{
"ids": ["id1", "id2"]
"otherField": {}
}
Working solution with jackson-databind 2.11
@lombok.Value public class MyId { @JsonValue String id; public MyId(final String id) { this.id = id; } }
您可以使用 @JsonValue
。来自 docs:
Marker annotation that indicates that the value of annotated accessor (either field or "getter" method [a method with non-void return type, no args]) is to be used as the single value to serialize for the instance, instead of the usual method of collecting properties of value. Usually value will be of a simple scalar type (String or Number), but it can be any serializable type (Collection, Map or Bean).
用法:
@Value
public class MyId {
@JsonValue
String id;
}
完整代码:
public class JacksonExample {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
List<MyId> myIds = new ArrayList<>();
MyId id1 = new MyId("one");
MyId id2 = new MyId("two");
myIds.add(id1);
myIds.add(id2);
Response response = new Response(myIds, "some other field value");
System.out.println(objectMapper.writeValueAsString(response));
}
}
@Value
class Response {
List<MyId> ids;
String otherField;
}
@Value
class MyId {
@JsonValue
String id;
}
输出:
{
"ids": [
"one",
"two"
],
"otherField": "some other field value"
}