Java Jackson自定义反序列化器映射
我需要反序列化一些json,这些json可以包含对象数组[{}、{}]或单个对象{}。看我的。以下是我想做的:Java Jackson自定义反序列化器映射,java,jackson,deserialization,Java,Jackson,Deserialization,我需要反序列化一些json,这些json可以包含对象数组[{}、{}]或单个对象{}。看我的。以下是我想做的: public class LocationDeserializer extends JsonDeserializer<List<Location>>{ @Override public List<Location> deserialize(JsonParser jp, DeserializationConte
public class LocationDeserializer extends JsonDeserializer<List<Location>>{
@Override
public List<Location> deserialize(JsonParser jp,
DeserializationContext ctxt) throws IOException
{
List<Location> list = new ArrayList<Location>();
if(!jp.isExpectedStartArrayToken()){
list.add(...);
}else{
//Populate the list
}
return list;
}
或
我不知道您的JSON是什么样子,但我认为在这种情况下,使用JSON要比使用
JsonDeserializer
容易得多。大概是这样的:
ObjectNode root = mapper.readTree("location.json");
if (root.getNodeType() == JsonNodeType.ARRAY) {
//Use a get and the JsonNode API to traverse the tree to generate List<Location>
}
else {
//Use a get and the JsonNode API to traverse the tree to generate single Location or a one-element List<Location>
}
ObjectNode root=mapper.readTree(“location.json”);
if(root.getNodeType()==JsonNodeType.ARRAY){
//使用get和JSONNodeAPI遍历树以生成列表
}
否则{
//使用get和JsonNode API遍历树以生成单个位置或一个元素列表
}
您可以告诉Jackson将此反序列化器与注释一起使用
在反序列化方法中,可以使用以下方法:
@Override
public List<Location> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
List<Location> list = new ArrayList<Location>();
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(jp);
if(root.get("location").isArray()){
// handle the array
}else{
// handle the single object
}
return list;
}
@覆盖
公共列表反序列化(JsonParser jp,反序列化上下文ctxt)引发IOException{
列表=新的ArrayList();
ObjectMapper mapper=新的ObjectMapper();
JsonNode root=mapper.readTree(jp);
if(root.get(“location”).isArray()){
//处理数组
}否则{
//处理单个对象
}
退货清单;
}
Ok这可能是一个更好的开始。我在问题中添加了一些json示例。现在,在这两种情况下,您将如何填充列表或位置对象?还有,如何告诉jackson对我父对象的location属性使用这个解析器?首先,这取决于您。您可以在else
案例中生成单个位置
,或者生成一个元素集合。后者可能更整洁。其次,它不是最优雅的,但我会将REST端点配置为将请求作为纯文本,然后使用simlar代码手动反序列化请求体。使用JsonDeserializer
,您通常会对适当的POJO进行注释,但我不知道它如何应用于(可能的)集合。我尝试了这个方法,得到了一个ClientProtocolException:无法提取响应:没有为响应类型[com.app.model.PositionResponse]和内容类型[application/json]找到合适的HttpMessageConverter. 这是我设置的注释:@jsondeseligate(using=LocationDeserializer.class)public void setLocation(final List List){this.location=List;}好吧,这不属于这个问题,这是代码的另一部分中的一个错误,我想,在那里您通过ApacheHttpClient接收JSON。但这部分应该可以工作,我认为它属于它,因为当我在setter上添加注释时,会引发异常。因此,这是“如何告诉Jackson对属性“location”使用此反序列化程序”的一部分。我应该用更多的代码更新我的问题吗?注释是针对Location类的,因此您可以编写@jsondeseserialize(使用=LocationDeserializer.class)公共类Location…
,而不是反序列化方法。
ObjectNode root = mapper.readTree("location.json");
if (root.getNodeType() == JsonNodeType.ARRAY) {
//Use a get and the JsonNode API to traverse the tree to generate List<Location>
}
else {
//Use a get and the JsonNode API to traverse the tree to generate single Location or a one-element List<Location>
}
@Override
public List<Location> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
List<Location> list = new ArrayList<Location>();
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(jp);
if(root.get("location").isArray()){
// handle the array
}else{
// handle the single object
}
return list;
}