杰克逊的课程JsonGenerator有几个名为的例程,writeBinary()它们接受InputStream. 这些例程会将此流提供的数据转换为 Base64 并增量写入,从而节省内存。这很棒。
然而,没有其他例程来JsonGenerator接收InputStream. 我想从 an 获取数据InputStream并使用 a 写入它JsonGenerator,但不将数据转换为 Base64。数据显然需要转义才能成为有效的 JSON。
是的,我可以从自己读取内容InputStream并将它们保存为转换后的字符串,然后让其写入JsonGenerator。但这会将整个内容加载到内存中,我试图避免这种情况。
Jackson 是否支持从不将其InputStream转换为 Base64 的数据写入?
这是我正在寻找的示例:
JsonGenerator jgen = jsonFactory.createJsonGenerator(...)
// This is already supported; it converts the content to Base64.
InputStream is = ...
jgen.writeFieldName("binary");
jgen.writeBinary(is, -1);
// This does not exist and is what I would like to see.
// As noted in the comments, this might have to take in a Charset parameter.
InputStream is2 = ...
jgen.writeFieldName("data");
jgen.writeFromStream(is2, -1);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1046 次 |
| 最近记录: |