我想从存储在MongoDB中的几个较小的文档中将一个大型文档编译为JSON。我已经编写了一个Java函数来编译文档,现在我希望我的应用程序可以访问JSON,以便将其返回给客户端或对其进行进一步处理。
我的问题是,实例化JSON字符串会占用大量内存,因此,我开始遇到OutOfMemoryErrors。我已经从MongoDB库中实现了自己的toJSON方法版本,如下所示:
/**
* Borrowed from the MongoDB toJSON method for Documents, except we dont instantiate the json string and return the writer instead.
*
* @return a buffer containing the JSON representation of the given document
* @throws org.bson.codecs.configuration.CodecConfigurationException if the registry does not contain a codec for the document values.
*/
private Writer toJson(Document document) {
JsonWriter writer = new JsonWriter(new StringWriter(), new JsonWriterSettings(true));
new DocumentCodec().encode(writer, document, EncoderContext.builder().isEncodingCollectibleDocument(true).build());
return writer.getWriter();
}
此方法将返回缓冲了JSON字符串的writer,而不是返回字符串。现在,我想在我的应用程序中而不使用它调用toString()方法,就像在许多在线示例中看到的那样。我找到的最接近的示例是解决方案at the bottom of this page。
try (BufferedWriter bw = new BufferedWriter(new FileWriter("TempFile1mod"))) {
final int aLength = aSB.length();
final int aChunk = 1024;// 1 kb buffer to read data from
final char[] aChars = new char[aChunk];
for (int aPosStart = 0; aPosStart < aLength; aPosStart += aChunk) {
final int aPosEnd = Math.min(aPosStart + aChunk, aLength);
aSB.getChars(aPosStart, aPosEnd, aChars, 0); // Create no new buffer
bw.write(aChars, 0, aPosEnd - aPosStart);// This is faster than just copying one byte at the time
}
这确实做了我想要的,并且允许我将我的字符串以大块形式写入任何流。但是,由于在我看来这是一个常见的用例,因此我希望Java有一些通用的方法可以将我的数据从字符串缓冲区传送到另一个流中。
我想念什么吗?
答案 0 :(得分:0)
感谢@Thomas的建议。我最终将我的StringBuffer传递给CharSequenceInputStream,像这样:
/**
* Borrowed from the MongoDB toJSON method for Documents, except we dont
* instantiate the JSON String but return an InputStream instead.
*
* @return a buffer containing the JSON representation of the given document
* @throws org.bson.codecs.configuration.CodecConfigurationException if the
* registry does not contain a codec for the document values.
*/
private InputStream toJson(Document document) {
JsonWriter writer = new JsonWriter(new StringWriter(), new JsonWriterSettings(true));
new DocumentCodec().encode(writer, document,
EncoderContext.builder().isEncodingCollectibleDocument(true).build());
CharSequence result = ((StringWriter) writer.getWriter()).getBuffer();
return new CharSequenceInputStream(result, Charset.forName("UTF-8"));
}