尝试反序列化此 JSON 时, playSequenceDateRanges 列表为空
JSON:
{
"first_name": "bob",
"last_name": "smith",
"play_sequence_date_ranges": [
"1 - 07/30/2018 - AL",
"2 - 07/30/2018 - AZ"
],
"addresses": [
{
"line_1": "Street 1",
"city": "City"
}
],
"id": 113
}
Java类:
public class Person implements Comparable<Person > {
@JsonbProperty(value = "play_sequence_date_ranges")
private List<String> playSequenceDateRanges = new ArrayList<>();
@JsonbProperty(value = "first_name")
private String firstName;
@JsonbProperty(value = "last_name")
private String lastName;
private List<Address> addresses = new ArrayList<>();
private Integer id;
...
}
我尝试将反序列化器和适配器注释(分别为@JsonbTypeDeserializer 和@JsonbTypeAdapter)添加到 playSequenceDateRanges 以从 JSONArray 创建字符串列表,但没有乐趣;代码甚至没有被调试。
解串器:
public class JsonArrayToStringListDeserializer implements JsonbDeserializer<List<String>> {
@Override
public List<String> deserialize(final JsonParser parser, final DeserializationContext ctx, final Type rtType) {
List<String> stringList = new ArrayList<>();
JsonArray jsonArray = parser.getArray();
for (int i = 0; i < jsonArray.size(); i++) {
stringList.add(jsonArray.getString(i));
}
return stringList;
}
}
适配器
public class JsonArrayStringListAdapter implements JsonbAdapter<List<String>, JsonArray> {
@Override
public JsonArray adaptToJson(List<String> obj) throws Exception {
return null;
}
@Override
public List<String> adaptFromJson(JsonArray obj) throws Exception {
Jsonb jsonb = JsonbBuilder.create();
List<String> stringList = jsonb.fromJson((InputStream) obj, new ArrayList<String>(){}.getClass().getGenericSuperclass());
return stringList;
}
}
我正在使用 yasson 1.08。