gpt4 book ai didi

java - 为什么 YAMLGenerator 不关闭流取决于配置

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:56:29 25 4
gpt4 key购买 nike

当使用 yaml 工厂创建 objectMapper 时,您可以设置几个配置参数:

   ObjectMapper o = new ObjectMapper(new YAMLFactory());
// o.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
// o.enable(SerializationFeature.CLOSE_CLOSEABLE);

问题是这个配置在 YAML Generator 中被忽略了:

 @Override
public void close() throws IOException
{
if (!isClosed()) {
_emitter.emit(new DocumentEndEvent(null, null, false));
_emitter.emit(new StreamEndEvent(null, null));
super.close();
_writer.close();
}
}

即使在 javadoc 中写了别的东西

void com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.close() throws IOException

Method called to close this generator, so that no more content can be written.

Whether the underlying target (stream, writer) gets closed depends on whether this generator either manages the target (i.e. is the only one with access to the target -- case if caller passes a reference to the resource such as File, but not stream); or has feature Feature.AUTO_CLOSE_TARGET enabled. If either of above is true, the target is also closed. Otherwise (not managing, feature not enabled), target is not closed.

最佳答案

使用 YAML Mapper 而不是 Object Mapper。这对我来说很好。

YAMLMapper yamlMapper = new YAMLMapper(); 
yamlMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
yamlMapper.configure(YAMLGenerator.Feature.WRITE_DOC_START_MARKER, true);
yamlMapper.enable(SerializationFeature.CLOSE_CLOSEABLE);

关于java - 为什么 YAMLGenerator 不关闭流取决于配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50445012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com