Use Jackson To Stream Parse an Array of Json Objects

前端 未结 4 1623
无人共我
无人共我 2020-12-05 11:19

I have a file that contains a json array of objects:

[ { \"test1\": \"abc\" }, { \"test2\": [1, 2, 3] } ]

I wish to use use

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 11:51

    This is a late answer that builds on Ian Roberts' answer. You can also use a JsonPointer to find the start position if it is nested into a document. This avoids custom coding the slightly cumbersome streaming token approach to get to the start point. In this case, the basePath is "/", but it can be any path that JsonPointer understands.

    Path sourceFile = Paths.get("/path/to/my/file.json");
    // Point the basePath to a starting point in the file
    JsonPointer basePath = JsonPointer.compile("/");
    ObjectMapper mapper = new ObjectMapper();
    try (InputStream inputSource = Files.newInputStream(sourceFile);
         JsonParser baseParser = mapper.getFactory().createParser(inputSource);
         JsonParser filteredParser = new FilteringParserDelegate(baseParser,
                        new JsonPointerBasedFilter(basePath), false, false);) {
        // Call nextToken once to initialize the filteredParser
        JsonToken basePathToken = filteredParser.nextToken();
        if (basePathToken != JsonToken.START_ARRAY) {
            throw new IllegalStateException("Base path did not point to an array: found " 
                                           + basePathToken);
        }
        while (filteredParser.nextToken() == JsonToken.START_OBJECT) {
            // Parse each object inside of the array into a separate tree model 
            // to keep a fixed memory footprint when parsing files 
            // larger than the available memory
            JsonNode nextNode = mapper.readTree(filteredParser);
            // Consume/process the node for example:
            JsonPointer fieldRelativePath = JsonPointer.compile("/test1");
            JsonNode valueNode = nextNode.at(fieldRelativePath);
            if (!valueNode.isValueNode()) {
                throw new IllegalStateException("Did not find value at "
                        + fieldRelativePath.toString() 
                        + " after setting base to " + basePath.toString());
            }
            System.out.println(valueNode.asText());
        }
    }
    

提交回复
热议问题