Jackson ObjectMapper – 将值写入流中

huangapple 未分类评论44阅读模式
英文:

Jackson ObjectMapper - writing value to stream

问题

以下是您提供的代码段的翻译部分:

@Override
public StreamingResponseBody fetchAndWriteOrderAllocationsToFile(LocalDate date) {
    int orderCount = orderDao.getOrderCount(date);
    // 分页处理
    return outputStream -> {
        try (ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(outputStream))) {
            zipOut.putNextEntry(new ZipEntry("report.txt"));
            int startIndex = 0, count = orderCount;
            do {
                List<String> orderSerialNos = orderDao.getOrderSerialNos(date, startIndex, PAGESIZE);
                orderSerialNos.parallelStream().forEach(orderSerialNo -> {
                    try {
                        writeToStream(zipOut, allocationsService.getAllocationsFromOrderItems(orderSerialNo), objectMapper);
                    } catch (Exception e) {
                        writeToStream(zipOut, Allocations.builder()
                            .orderSerialNo(orderSerialNo)
                            .build(), objectMapper);
                    }
                });
                count -= PAGESIZE;
                startIndex += PAGESIZE;
            } while (count > 0);
        }
    };
}

private static void writeToStream(OutputStream outputStream,
                                  Object result,
                                  ObjectMapper objectMapper) {
    try {
        objectMapper.writeValue(outputStream, result);
    } catch (IOException e) {
        log.error("在写入流时发生错误", e);
    }
}

希望这对您有所帮助。如果您需要更多的翻译或其他帮助,请随时提问。

英文:

I have a requirement to fetch and write thousands of records from DB, convert to json and write to zip file. I am able to write with below implementation as well.

    @Override
    public StreamingResponseBody fetchAndWriteOrderAllocationsToFile(LocalDate date) {
        int orderCount = orderDao.getOrderCount(date);
        // Pagination
        return outputStream -&gt; {
            try (ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(outputStream))) {
                zipOut.putNextEntry(new ZipEntry(&quot;report.txt&quot;));
                int startIndex = 0, count = orderCount;
                do {
                    List&lt;String&gt; orderSerialNos = orderDao.getOrderSerialNos(date, startIndex, PAGESIZE);
                    orderSerialNos.parallelStream().forEach(orderSerialNo -&gt; {
                        try {
                            writeToStream(zipOut, allocationsService.getAllocationsFromOrderItems(orderSerialNo), objectMapper);
                        } catch (Exception e) {
                            writeToStream(zipOut, Allocations.builder()
                                .orderSerialNo(orderSerialNo)
                                .build(), objectMapper);
                        }
                    });
                    count -= PAGESIZE;
                    startIndex += PAGESIZE;
                } while (count &gt; 0);
            }
        };
    }

    private static void writeToStream(OutputStream outputStream,
                                      Object result,
                                      ObjectMapper objectMapper) {
        try {
            objectMapper.writeValue(outputStream, result);
        } catch (IOException e) {
            log.error(&quot;Error writing results to stream&quot;, e);
        }
    }

However I would like to introduce a new line character(or comma) after every json being written to file.
The closest I got was overriding PrettyPrinter.writeEndObject method to something like below and use the overridden PrettyPrinter class. This obviously adds new line char to all the sub objects of json as well as every new json. The expectation is to have the new line character only after each json.
Is there any way to accomplish this?


        @Override
        public void writeEndObject(JsonGenerator g, int nrOfEntries) throws IOException {
            g.writeRaw(&quot;}\n&quot;);
        }

Above code gives:

{&quot;orderSerialNo&quot;:&quot;1234-ABCD&quot;,&quot;orderId&quot;:1,&quot;shippingAllocations&quot;:[{&quot;recipientId&quot;:25,&quot;itemId&quot;:3893814,&quot;itemSku&quot;:&quot;ABC&quot;,&quot;quantity&quot;:1,&quot;shippingItemId&quot;:3893815,&quot;shippingSku&quot;:&quot;DEF&quot;,&quot;shipperId&quot;:66,&quot;allocation&quot;:0}
],&quot;sdAllocations&quot;:[],&quot;idAllocations&quot;:[]}
{&quot;orderSerialNo&quot;:&quot;6789-EFGH&quot;,&quot;orderId&quot;:2,&quot;shippingAllocations&quot;:[{&quot;recipientId&quot;:45,&quot;itemId&quot;:88,&quot;itemSku&quot;:&quot;BLAH&quot;,&quot;quantity&quot;:1,&quot;shippingItemId&quot;:78,&quot;shippingSku&quot;:&quot;HELP&quot;,&quot;shipperId&quot;:99,&quot;allocation&quot;:7.95}
],&quot;sdAllocations&quot;:[],&quot;idAllocations&quot;:[]}

The expectation is:

{&quot;orderSerialNo&quot;:&quot;1234-ABCD&quot;,&quot;orderId&quot;:1,&quot;shippingAllocations&quot;[{&quot;recipientId&quot;:25,&quot;itemId&quot;:3893814,&quot;itemSku&quot;:&quot;ABC&quot;,&quot;quantity&quot;:1,&quot;shippingItemId&quot;:3893815,&quot;shippingSku&quot;:&quot;DEF&quot;,&quot;shipperId&quot;:66,&quot;allocation&quot;:0}],&quot;sdAllocations&quot;:[],&quot;idAllocations&quot;:[]}
{&quot;orderSerialNo&quot;:&quot;6789-EFGH&quot;,&quot;orderId&quot;:2,&quot;shippingAllocations&quot;:[{&quot;recipientId&quot;:45,&quot;itemId&quot;:88,&quot;itemSku&quot;:&quot;BLAH&quot;,&quot;quantity&quot;:1,&quot;shippingItemId&quot;:78,&quot;shippingSku&quot;:&quot;HELP&quot;,&quot;shipperId&quot;:99,&quot;allocation&quot;:7.95}],&quot;sdAllocations&quot;:[],&quot;idAllocations&quot;:[]}

huangapple
  • 本文由 发表于 2020年4月7日 07:28:01
  • 转载请务必保留本文链接:https://java.coder-hub.com/61070503.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定