如何在杰克逊中解开列表包装物品的清单? [英] How do I unwrap a list of list wrapped items in Jackson?

查看:80
本文介绍了如何在杰克逊中解开列表包装物品的清单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于这个的豆子:

I have a bean that resembles this:

public class Product {

    public String id;
    public String vendor;
    public Set<Image> images;
}

public class Image {

    public String originalSrc;
}

我正在尝试反序列化类似于以下内容的JSON:

I'm trying to deserialize my JSON that resembles this:

{
  "id": "gid:\/\/mysite\/Product\/1853361520730",
  "vendor": "gadgetdown",
  "images": {
    "edges": [
      {
        "node": {
          "originalSrc": "https:\/\/cdn.something.com"
        }
      },
      {
        "node": {
          "originalSrc": "https:\/\/cdn.something.com"
        }
      }
    ]
  }

我无法反序列化该对象,因为每个image对象都包装在node对象中,并且一起包装在edges对象中.

I'm unable to deserialize the object as each of the image objects are wrapped in a node object and collectively in a edges object.

为清楚起见,我不想通过使用bean来完成此操作,此示例是一种简化,并且JSON有效负载中的所有数组项都包裹在此edgesnode表示形式中.

For clarity, I don't want to accomplish this via using beans and this example is a simplification and all array items in the JSON payload are wrapped in this edges and node representation.

推荐答案

如果每个列表都具有如下结构:

If every list has a structure like below:

{
  "images": {
    "edges": [
      {
        "node": {
          "entry": "entry-value"
        }
      }
    ]
  }
}

每个列表都是具有edges属性的JSON Object,并且数组中的每个元素都由具有node属性的JSON Object包装.对于这种结构,我们可以编写类似于

Each list is a JSON Object with edges property and each element in array is wrapped by JSON Object with node property. For this structure we can write generic deserializer similar to one from Jackson - deserialize inner list of objects to list of one higher level question.

示例Set反序列化器:

class InnerSetDeserializer extends JsonDeserializer<Set> implements ContextualDeserializer {

  private final JavaType propertyType;

  public InnerSetDeserializer() {
    this(null);
  }

  public InnerSetDeserializer(JavaType propertyType) {
    this.propertyType = propertyType;
  }

  @Override
  public Set deserialize(JsonParser p, DeserializationContext context) throws IOException {
    p.nextToken(); // SKIP START_OBJECT
    p.nextToken(); // SKIP any FIELD_NAME

    CollectionType collectionType = getCollectionType(context);
    List<Map<String, Object>> list = context.readValue(p, collectionType);

    p.nextToken(); // SKIP END_OBJECT

    return list.stream().map(Map::values).flatMap(Collection::stream).collect(Collectors.toSet());
  }

  private CollectionType getCollectionType(DeserializationContext context) {
    TypeFactory typeFactory = context.getTypeFactory();
    MapType mapType =
        typeFactory.constructMapType(
            Map.class, String.class, propertyType.getContentType().getRawClass());

    return typeFactory.constructCollectionType(List.class, mapType);
  }

  @Override
  public JsonDeserializer<?> createContextual(DeserializationContext context, BeanProperty property) {
    return new InnerSetDeserializer(property.getType());
  }
}

我们可以使用如下:

class Product {

  private String id;
  private String vendor;

  @JsonDeserialize(using = InnerSetDeserializer.class)
  private Set<Image> images;

  // getters, setters
}

示例应用程序:

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.MapType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class JsonApp {

  public static void main(String[] args) throws IOException {
    File jsonFile = new File("./resources/test.json");

    ObjectMapper mapper = new ObjectMapper();
    Product product = mapper.readValue(jsonFile, Product.class);
    System.out.println(product);
  }
}

上面的代码显示:

Product{id='gid://mysite/Product/1853361520730', vendor='gadgetdown', images=[Image{originalSrc='https://cdn.something.com'}, Image{originalSrc='https://cdn.something.com'}]}

这篇关于如何在杰克逊中解开列表包装物品的清单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆