英文:
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 -> {
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("Error writing results to stream", 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("}\n");
}
Above code gives:
{"orderSerialNo":"1234-ABCD","orderId":1,"shippingAllocations":[{"recipientId":25,"itemId":3893814,"itemSku":"ABC","quantity":1,"shippingItemId":3893815,"shippingSku":"DEF","shipperId":66,"allocation":0}
],"sdAllocations":[],"idAllocations":[]}
{"orderSerialNo":"6789-EFGH","orderId":2,"shippingAllocations":[{"recipientId":45,"itemId":88,"itemSku":"BLAH","quantity":1,"shippingItemId":78,"shippingSku":"HELP","shipperId":99,"allocation":7.95}
],"sdAllocations":[],"idAllocations":[]}
The expectation is:
{"orderSerialNo":"1234-ABCD","orderId":1,"shippingAllocations"[{"recipientId":25,"itemId":3893814,"itemSku":"ABC","quantity":1,"shippingItemId":3893815,"shippingSku":"DEF","shipperId":66,"allocation":0}],"sdAllocations":[],"idAllocations":[]}
{"orderSerialNo":"6789-EFGH","orderId":2,"shippingAllocations":[{"recipientId":45,"itemId":88,"itemSku":"BLAH","quantity":1,"shippingItemId":78,"shippingSku":"HELP","shipperId":99,"allocation":7.95}],"sdAllocations":[],"idAllocations":[]}
专注分享java语言的经验与见解,让所有开发者获益!
评论