How do I unwrap a list of list wrapped items in Jackson?

此生再无相见时 提交于 2019-12-10 15:59:12

问题


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;
}

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"
        }
      }
    ]
  }

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.


EDIT: 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.


回答1:


If every list has a structure like below:

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

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.

Example Set deserialiser:

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());
  }
}

We can use is as below:

class Product {

  private String id;
  private String vendor;

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

  // getters, setters
}

Example app:

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);
  }
}

Above code prints:

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



回答2:


So images is not a set it is a JSONObject with edges list in it

public class Images {

   private List<Edge> edges;

    }

Each Edge contains Node object,

public class Edge {

   private Node node;

   }

Each Node has single String property originalSrc

public class Node  {

 private String originalSrc;

  }



回答3:


To unwrap Images out of "node": { "originalSrc": "https:\/\/cdn.something.com" } you can simply use @JsonRootName annotation

@JsonRootName(value = "node")
class Image {
    public String originalSrc;
}

But unwrapping images collection out of "images": { "edges": [{...}, {...}] } is bit more complex, need to use custom JsonDeserializer

class Product {
    public String id;
    public String vendor;

    @JsonDeserialize(using = ImageSetDeserializer.class)
    public Set<Image> images;
}


class ImageSetDeserializer extends JsonDeserializer<Set<Image>> {
    public Set<Image> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
        ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec();
        JsonNode node = mapper.readTree(jsonParser);
        return mapper.convertValue(node.get("edges").findValues("node"), new TypeReference<Set<Image>>() {});
    }
}

Finally a test:

public class ProductTest {

    private final String source = "{\n" +
            "  \"id\": \"gid:\\/\\/mysite\\/Product\\/1853361520730\",\n" +
            "  \"vendor\": \"gadgetdown\",\n" +
            "  \"images\": {\n" +
            "    \"edges\": [\n" +
            "      {\n" +
            "        \"node\": {\n" +
            "          \"originalSrc\": \"https:\\/\\/cdn.something.com\"\n" +
            "        }\n" +
            "      },\n" +
            "      {\n" +
            "        \"node\": {\n" +
            "          \"originalSrc\": \"https:\\/\\/cdn.something.com\"\n" +
            "        }\n" +
            "      }\n" +
            "    ]\n" +
            "  }" +
            "}";

    @Test
    public void test() throws IOException {
        ObjectMapper mapper = new ObjectMapper();

        Product product = mapper.readValue(source, Product.class);

        assertEquals(product.id, "gid://mysite/Product/1853361520730");
        assertEquals(product.vendor, "gadgetdown");
        assertNotNull(product.images);
        List<Image> images = new ArrayList<>(product.images);
        assertEquals(images.size(), 2);
        assertEquals(images.get(0).originalSrc, "https://cdn.something.com");
        assertEquals(images.get(1).originalSrc, "https://cdn.something.com");
    }
}


来源:https://stackoverflow.com/questions/57270327/how-do-i-unwrap-a-list-of-list-wrapped-items-in-jackson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!