mateusz58
6/26/2019 - 9:06 PM

JSON_deserialize custom

Collection<VolumeInfo> getBooks ()throws IOException {
        Resource resource = resourceLoader.getResource("classpath:json/books1.json");
        ObjectMapper mapper = new ObjectMapper();
        Collection<VolumeInfo> volumeInfo = new ArrayList<VolumeInfo>() {
        };
        JsonNode rootNode = mapper.readTree(resource.getInputStream());
        ArrayNode itemsNode = (ArrayNode) rootNode.get("items");
        Iterator<JsonNode> itemsIterator = itemsNode.elements();
        while (itemsIterator.hasNext()) {
            JsonNode volumeInfoNode = itemsIterator.next();
            VolumeInfo volumeInfoObject = mapper.readValue(volumeInfoNode.get("volumeInfo").toString(), VolumeInfo.class);
            volumeInfo.add(volumeInfoObject);
        }
        return volumeInfo;
//          JsonNode root = mapper.readValue(resource.getInputStream(), new TypeReference<List<String, Object>>() {});
    }